1.媒体查询,移动优先与 PC 优先

1.1 移动优先案例解析
案例代码如下:

  1. <body>
  2. <!-- 媒体: 屏幕, 打印机 -->
  3. <!-- 查询: 查询当前的屏幕的宽度变化 -->
  4. <!-- 布局前提: 宽度受限, 而高度不受限的空间内进行布局 -->
  5. <button class="btn small">按钮1</button>
  6. <button class="btn middle">按钮2</button>
  7. <button class="btn large">按钮3</button>
  8. </body>

布局样式代码:

  1. <style>
  2. html {
  3. font-size: 10px;
  4. }
  5. /* 按钮基本样式 */
  6. .btn {
  7. background-color: seagreen;
  8. color: white;
  9. border: none;
  10. outline: none;
  11. }
  12. .btn:hover {
  13. cursor: pointer;
  14. opacity: 0.8;
  15. transition: 0.3s;
  16. padding: 0.4rem 0.8rem;
  17. }
  18. /* 小按钮 */
  19. .btn.small {
  20. /* 1.2rem = 12px */
  21. font-size: 1.2rem;
  22. }
  23. /* 中按钮 */
  24. .btn.middle {
  25. /* 1.6rem = 16px */
  26. font-size: 1.6rem;
  27. }
  28. /* 中按钮 */
  29. .btn.large {
  30. /* 1.8rem = 18px */
  31. font-size: 1.8rem;
  32. }
  33. /* 只要动态的修改rem , 就可以实现动态的改变按钮大小 */
  34. /* 移动优先: 从最小的屏幕开始进行适配 */
  35. /* < 375px */
  36. @media (max-width: 374px) {
  37. html {
  38. font-size: 12px;
  39. }
  40. }
  41. /* 375px - 414px */
  42. @media (min-width: 375px) and (max-width: 413px) {
  43. html {
  44. font-size: 14px;
  45. }
  46. }
  47. /* > 414px */
  48. @media (min-width: 414px) {
  49. html {
  50. font-size: 16px;
  51. }
  52. }
  53. </style>

1.2 PC 优先案例解析

案例代码如下:

  1. <body>
  2. <button class="btn small">按钮1</button>
  3. <button class="btn middle">按钮2</button>
  4. <button class="btn large">按钮3</button>
  5. </body>

布局样式代码:

  1. <style>
  2. html {
  3. font-size: 10px;
  4. }
  5. /* 按钮基本样式 */
  6. .btn {
  7. background-color: seagreen;
  8. color: white;
  9. border: none;
  10. outline: none;
  11. }
  12. .btn:hover {
  13. cursor: pointer;
  14. opacity: 0.8;
  15. transition: 0.3s;
  16. padding: 0.4rem 0.8rem;
  17. }
  18. /* 小按钮 */
  19. .btn.small {
  20. /* 1.2rem = 12px */
  21. font-size: 1.2rem;
  22. }
  23. /* 中按钮 */
  24. .btn.middle {
  25. /* 1.6rem = 16px */
  26. font-size: 1.6rem;
  27. }
  28. /* 大按钮 */
  29. .btn.large {
  30. /* 1.8rem = 18px */
  31. font-size: 1.8rem;
  32. }
  33. /* PC优先: 从最大的屏幕开始进行适配 */
  34. /* > 1920px */
  35. @media (min-width: 1920px) {
  36. html {
  37. font-size: 16px;
  38. }
  39. }
  40. /* 1025px - 1919px */
  41. @media (min-width: 1025px) and (max-width: 1919px) {
  42. html {
  43. font-size: 14px;
  44. }
  45. }
  46. /* < 1024px */
  47. @media (max-width: 1024px) {
  48. html {
  49. font-size: 12px;
  50. }
  51. }
  52. </style>

2.定位与定位元素

2.1 静态定位:(position: static)文档流方式,显示顺序与书写顺序一致,完全由浏览器控制,根据元素在 html 文档中的顺序,也是没有定位。

2.2 相对定位:(position: relative)相对定位元素仍然在文档流中,所占空间不释放,只有相对原位置进行了偏移,指在自己原来的位置上进行偏移变化。
相对定位实例演示:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>相对定位:相对于自身原始位置的偏移</title>
  8. </head>
  9. <body>
  10. <div class="box father">
  11. <div class="box child first"></div>
  12. <div class="box child second"></div>
  13. </div>
  14. </body>
  15. </html>
  16. <style>
  17. body {
  18. border: 1px solid #000;
  19. }
  20. .box {
  21. border: 1px solid #000;
  22. }
  23. .box.father {
  24. width: 400px;
  25. height: 400px;
  26. background-color: lightcyan;
  27. }
  28. .box.child {
  29. width: 100px;
  30. height: 100px;
  31. background-color: yellow;
  32. }
  33. .box.child.first {
  34. background-color: red;
  35. position: relative;
  36. top: 30px;
  37. left: 30px;
  38. }
  39. </style>

效果图示例:(相对于自己本身的偏移)

2.3 绝对定位:(position: absolute)不再相对于自身, 而是相对于它的包含块,定位元素: position 不能是 static 就可以了,如果绝对定位元素, 一直向上,找不到可定位的父级元素,就相对于 body 或者 html。
绝对定位实例演示:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>定位与定位元素</title>
  8. </head>
  9. <body>
  10. <div class="box father">
  11. <div class="box child first"></div>
  12. <div class="box child second"></div>
  13. </div>
  14. </body>
  15. </html>
  16. <style>
  17. body {
  18. border: 1px solid #000;
  19. }
  20. .box {
  21. border: 1px solid #000;
  22. }
  23. .box.father {
  24. width: 400px;
  25. height: 400px;
  26. background-color: lightcyan;
  27. }
  28. .box.child {
  29. width: 100px;
  30. height: 100px;
  31. background-color: yellow;
  32. }
  33. .box.child.first {
  34. background-color: red;
  35. position: relative;
  36. top: 30px;
  37. left: 30px;
  38. position: absolute;
  39. }
  40. .box.father {
  41. /* 设置定位参考父级/包含块,只要不是static 就可以了 */
  42. position: relative;
  43. }
  44. </style>

效果图示例:(相对于包含块的偏移)

2.4 固定定位: (position: fixed)是绝对定位的子集, 只不过他的定位包含块是固定的,永远是 body
登录页面实例演示:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>固定定位演示代码</title>
  8. </head>
  9. <body>
  10. <header>
  11. <h2 class="title">固定定位实例</h2>
  12. <!-- js点击事件暂未理解的,后续再做案例分析 -->
  13. <button onclick="document.querySelector('.modal').style.display='block'">登录</button>
  14. </header>
  15. <!-- 模态框 -->
  16. <div class="modal">
  17. <!-- 1. 半透明的遮罩 -->
  18. <!-- 点击遮罩,关闭表单 -->
  19. <div class="modal-bg" onclick="this.parentNode.style.display='none'"></div>
  20. <!-- 2. 弹层表单 -->
  21. <form action="" class="modal-form">
  22. <fieldset style="display: grid; gap: 1em">
  23. <legend>用户登录</legend>
  24. <input type="email" name="email" placeholder="guding@php.cn" />
  25. <input type="password" name="password" placeholder="不少于6位" />
  26. <button>登录</button>
  27. </fieldset>
  28. </form>
  29. </div>
  30. <style>
  31. /* 初始化 */
  32. * {
  33. margin: 0;
  34. padding: 0;
  35. box-sizing: border-box;
  36. }
  37. /* 头部样式采用flex容器布局开始 */
  38. header {
  39. background-color: lightseagreen;
  40. padding: 0.5em 1em;
  41. display: flex;
  42. }
  43. /* 标题logo */
  44. header .title {
  45. font-weight: lighter;
  46. font-style: italic;
  47. color: white;
  48. text-shadow: 1px 1px 1px #555;
  49. letter-spacing: 1px;
  50. }
  51. /* 登录按钮 */
  52. header button {
  53. margin-left: auto;
  54. width: 5em;
  55. border: none;
  56. border-radius: 0.5em;
  57. }
  58. header button:hover {
  59. cursor: pointer;
  60. background-color: coral;
  61. color: white;
  62. box-shadow: 0 0 5px #fff;
  63. transition: 0.3s;
  64. }
  65. /* 模态框表单 */
  66. .modal .modal-form fieldset {
  67. background-color: lightcyan;
  68. border: none;
  69. padding: 2em;
  70. box-shadow: 0 0 5px #888;
  71. }
  72. /* 模态框表单标题 */
  73. .modal .modal-form fieldset legend {
  74. padding: 5px 1em;
  75. background-color: rebeccapurple;
  76. color: white;
  77. }
  78. .modal .modal-form {
  79. /* 固定定位 */
  80. position: fixed;
  81. /* 顶部要留出头部导航的位置,top:距离顶部的多少 */
  82. top: 10em;
  83. /* 左右相等,将表单挤到正中间,固定在中部 */
  84. left: 20em;
  85. right: 20em;
  86. }
  87. /* 半透明的遮罩 */
  88. .modal .modal-bg {
  89. /* 固定布局,定位空间在整个屏幕 */
  90. position: fixed;
  91. /* 屏幕视口的四个顶点的坐标,全部为0时候是覆盖整个页面 */
  92. top: 0;
  93. left: 0;
  94. right: 0;
  95. bottom: 0;
  96. /* 背景半透明,前三个数据为rgb的参数,第四个参数为透明度 */
  97. background-color: rgb(0, 0, 0, 0.5);
  98. }
  99. /* 初始化时将模态框弹层隐藏 */
  100. .modal {
  101. display: none;
  102. }
  103. </style>
  104. </body>
  105. </html>

效果图示例:(相对于 body/html 的偏移)

3.flex 常用属性

3.1 flex-flow 的常用属性值

常用参数参数的值代码说明
flex-flowrow wrapflex-flow: row wrap左对齐允许换行
flex-flowrow-reverse wrapflex-flow: row-reverse wrap右对齐允许换行
flex-flowrow nowrapflex-flow: row nowrap左对齐不允许换行
flex-flowrow-reverse nowrapflex-flow: row-reverse nowrap右对齐不允许换行

项目属性实例 1:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>flex项目属性实例1</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="item">item1</div>
  12. <div class="item">item2</div>
  13. <div class="item">item3</div>
  14. </div>
  15. <style>
  16. * {
  17. padding: 0;
  18. margin: 0;
  19. box-sizing: border-box;
  20. }
  21. html {
  22. font-size: 10px;
  23. }
  24. .container {
  25. display: flex;
  26. height: 20em;
  27. border: 1px solid;
  28. }
  29. .container > .item {
  30. width: 10em;
  31. padding: 1em;
  32. background-color: red;
  33. border: 1px solid;
  34. }
  35. .container {
  36. /* 左对齐允许换行 */
  37. flex-flow: row wrap;
  38. /* 右对齐允许换行 */
  39. flex-flow: row-reverse wrap;
  40. /* 左对齐不允许换行 */
  41. flex-flow: row nowrap;
  42. /* 右对齐,不允许换行 */
  43. flex-flow: row-reverse nowrap;
  44. }
  45. </style>
  46. </body>
  47. </html>

效果图 1:左对齐允许换行


效果图 2:右对齐允许换行

效果图 3:左对齐不允许换行

效果图 4:右对齐不允许换行

3.2 place-content 的常用属性值(项目在容器主轴上剩余空间的分配)

常用参数参数的值代码说明
place-contentstartplace-content: start项目左对齐分配
place-contentcenterplace-content: center项目居中分配
place-contentendplace-content: end项目右对齐分配
place-contentspace-betweenplace-content: space-between项目两端分配
place-contentspace-aroundplace-content: space-around项目分散分配
place-contentspace-evenlyplace-content: space-evenly项目平均分配

项目属性实例:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>flex项目属性实例2</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="item">item1</div>
  12. <div class="item">item2</div>
  13. <div class="item">item3</div>
  14. </div>
  15. <style>
  16. * {
  17. padding: 0;
  18. margin: 0;
  19. box-sizing: border-box;
  20. }
  21. html {
  22. font-size: 10px;
  23. }
  24. .container {
  25. display: flex;
  26. height: 20em;
  27. border: 1px solid;
  28. }
  29. .container > .item {
  30. width: 10em;
  31. padding: 1em;
  32. background-color: red;
  33. border: 1px solid;
  34. }
  35. .container {
  36. /* 项目左对齐分配,允许收缩 */
  37. place-content: start;
  38. /* 项目居中分配,允许收缩 */
  39. place-content: center;
  40. /* 项目右对齐分配,允许收缩 */
  41. place-content: end;
  42. /* 项目两端分配,允许收缩 */
  43. place-content: space-between;
  44. /* 项目分散分配,首尾两段间隔与中间不一样,允许收缩 */
  45. place-content: space-around;
  46. /* 项目平均分配,首尾两段间隔与中间都一样,允许收缩 */
  47. place-content: space-evenly;
  48. }
  49. </style>
  50. </body>
  51. </html>

效果图 1:项目左对齐分配

效果图 2:项目居中对齐分配,允许收缩

效果图 3:项目右对齐分配(和flow的右对齐顺序不同)

效果图 4:项目两端分配,允许收缩

效果图 5:项目分散对齐

效果图 6:项目平均分配

3.3 place-items 的常用属性值(项目在交叉轴上的对齐,因交叉轴无剩余空间概念,所以无法再分配)

常用参数参数的值代码说明
place-itemsstartplace-items: start向上排列
place-itemscenterplace-items: center居中排列
place-itemsendplace-items: end向下排列
place-itemsstretchplace-items: stretch拉伸占满

交叉轴属性实例演示:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>flex项目属性实例</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="item">item1</div>
  12. <div class="item">item2</div>
  13. <div class="item">item3</div>
  14. </div>
  15. <style>
  16. * {
  17. padding: 0;
  18. margin: 0;
  19. box-sizing: border-box;
  20. }
  21. html {
  22. font-size: 10px;
  23. }
  24. .container {
  25. display: flex;
  26. height: 20em;
  27. border: 1px solid;
  28. }
  29. .container > .item {
  30. width: 10em;
  31. padding: 1em;
  32. background-color: red;
  33. border: 1px solid;
  34. }
  35. .container {
  36. /* 向上对齐 */
  37. place-items: start;
  38. /* 居中对齐 */
  39. place-items: center;
  40. /* 向下对齐 */
  41. place-items: end;
  42. /* 拉伸占满 */
  43. place-items: stretch;
  44. }
  45. </style>
  46. </body>
  47. </html>

交叉轴的实例图
效果图 1:项目向上排列

效果图 2:项目居中排列

效果图 3:项目向下排列

效果图 4:项目拉伸占满

3.4 flex 的三个值解析(是否放大,是否缩小,计算宽度)
其中 flex 计算宽度的权重大小为 max-width > 计算宽度 > width

代码简化说明
flex: 0 1 autoflex: initial默认值,禁止放大允许缩小
flex: 1 1 autoflex: auto允许放大缩小,完全响应式
flex: 0 0 autoflex: none用于 PC 端完全失去响应式
flex:1flex-grow:1允许放大缩小,完全响应式
flex: 1 300px;允许缩小,可以放大到 300px,然后固定。

3.5 flex 内的 order 值
每个项目的默认顺序是 0,order 的值越大,越靠后。

更多相关文章

  1. Android(安卓)AIDL服务学习笔记
  2. Android项目结构和AndroidManifest.xml
  3. android基础知识12:android自动化测试04—Robotium:实例(上)
  4. 项目从MYSQL迁移至MARIADB教程
  5. 在Android上调用OpenCV 2.4.10库函数
  6. 转-Android(安卓)Studio *.jar 与 *.aar 的生成与*.aar导入项目
  7. 从零开始的 Android(安卓)新项目 - 收藏集 - 掘金
  8. 在Android里完美实现基站和WIFI定位
  9. Android周末 第一周-图灵聊天对话机器人小项目

随机推荐

  1. Android属性动画设置中心点
  2. Android Initialization Process ---- An
  3. Android 4.4以后设置状态栏颜色
  4. 2013.12.03 ——— android onPrepareOpt
  5. “android studio 运行程序提示Applicati
  6. android 开源项目推荐
  7. Android 导入多个外部静态链接库, 进行动
  8. 小知识:android
  9. Android Wear Preview- 创建通知(Creating
  10. Could not find com.android.tools.build