博客列表 >Flex项目属性的用法 - 九期线上班

Flex项目属性的用法 - 九期线上班

fkkf467
fkkf467原创
2019年11月08日 13:18:44658浏览

1. 设置弹性元素的增长因子(flex-grow)

设置项目的放大比例, 默认为0: 不放大,哪怕轴上有剩余空间
CSS语法: flex-grow: number|initial|inherit;
属性值:

  • number:一个数字,规定项目将相对于其他灵活的项目进行扩展的量。默认值是 0
  • initial:设置该属性为它的默认值
  • inherit:从父元素继承该属性

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>设置弹性元素的增长因子</title>
    6. <style>
    7. /*弹性容器通用样式*/
    8. .container {
    9. border: 2px dashed greenyellow;
    10. margin: 15px;
    11. background-color: grey;
    12. width: 550px;
    13. }
    14. /*弹性元素通用样式*/
    15. .item {
    16. box-sizing: border-box;
    17. border: 1px solid red;
    18. padding: 20px;
    19. background-color: lightpink;
    20. width: 100px;
    21. }
    22. /*块级弹性容器*/
    23. .flex {
    24. display: flex;
    25. }
    26. /*默认值为0*/
    27. .demo01 > .item {
    28. flex-grow: 0;
    29. }
    30. /*第一个与第三个增长量的属性可以省略*/
    31. .demo02 > .item:first-of-type {
    32. /*flex-grow: 0;*/
    33. }
    34. .demo02 > .item:nth-of-type(2) {
    35. flex-grow: 1;
    36. }
    37. .demo02 > .item:last-of-type {
    38. /*flex-grow: 0;*/
    39. }
    40. .demo03 > .item:first-of-type {
    41. flex-grow: 1;
    42. }
    43. .demo03 > .item:nth-of-type(2) {
    44. flex-grow: 2;
    45. }
    46. .demo03 > .item:last-of-type {
    47. flex-grow: 3;
    48. }
    49. /*
    50. 计算步骤:
    51. 1. 获取基本变量
    52. 1.1 可分配空间:550px - (100px + 100px +100px) = 250px
    53. 1.2 增长因子:第一个: 1, 第二个: 2, 第三个: 3
    54. 1.3 增长因子之和: 1 + 2 + 3 = 6
    55. 2. 计算增长比例
    56. 2.1 计算公式:增长比例 = 增长因子 / 增长因子之和
    57. 2.2 第一个元素:1 / 6 = 0.167
    58. 2.3 第二个元素:2 / 6 = 0.333
    59. 2.4 第三个元素:3 / 6 = 0.5
    60. 3. 计算每个元素的增长量
    61. 3.1 第一个元素:250px * 0.167 = 41.75px
    62. 3.2 第二个元素:250px * 0.333 = 83.25px
    63. 3.3 第三个元素:250px * 0.5 = 125px
    64. 4. 计算每个元素的最终宽度
    65. 4.1 第一个元素:100px + 41.75px = 141.75px
    66. 4.2 第二个元素:100px + 83.25px = 183.25px
    67. 4.3 第三个元素:100px + 125px = 225px
    68. */
    69. .demo04 > .item:first-of-type {
    70. flex-grow: 0.3;
    71. }
    72. .demo04 > .item:nth-of-type(2) {
    73. flex-grow: 0.1;
    74. }
    75. .demo04 > .item:last-of-type {
    76. flex-grow: 0.6;
    77. }
    78. .demo05 > .item:first-of-type {
    79. width: 75px;
    80. flex-grow: 2;
    81. }
    82. .demo05 > .item:nth-of-type(2) {
    83. width: 150px;
    84. flex-grow: 3;
    85. }
    86. .demo05 > .item:last-of-type {
    87. width: 165px;
    88. flex-grow: 7;
    89. }
    90. /*
    91. 计算步骤:
    92. 1. 获取基本变量
    93. 1.1 可分配空间:550px - (75px + 150px + 165px) = 160px
    94. 1.2 增长因子:第一个: 2, 第二个: 3, 第三个: 7
    95. 1.3 增长因子之和: 2 + 3 + 7 = 12
    96. 2. 计算增长比例
    97. 2.1 计算公式:增长比例 = 增长因子 / 增长因子之和
    98. 2.2 第一个元素:2 / 12 = 0.167
    99. 2.3 第二个元素:3 / 12 = 0.25
    100. 2.4 第三个元素:7 / 12 = 0.583
    101. 3. 计算每个元素的增长量
    102. 3.1 第一个元素:160px * 0.167 = 26.72px
    103. 3.2 第二个元素:160px * 0.25 = 40px
    104. 3.3 第三个元素:160px * 0.583 = 93.28px
    105. 4. 计算每个元素的最终宽度
    106. 4.1 第一个元素:75px + 26.72px = 101.72px
    107. 4.2 第二个元素:150px + 40px = 190px
    108. 4.3 第三个元素:165px + 93.28px = 258.28px
    109. */
    110. </style>
    111. </head>
    112. <body>
    113. <h1>设置弹性元素的增长因子</h1>
    114. <h3>1. 所有弹性元素不增长,以原始宽度显示,增长因子为:0(默认)</h3>
    115. <div class="container flex demo01">
    116. <span class="item">item1</span>
    117. <span class="item">item2</span>
    118. <span class="item">item3</span>
    119. </div>
    120. <h3>2. 将全部的剩余空间分配给指定弹性元素</h3>
    121. <div class="container flex demo02">
    122. <span class="item">item1</span>
    123. <span class="item">item2</span>
    124. <span class="item">item3</span>
    125. </div>
    126. <h3>3. 全部剩余空间按增长因子在不同弹性元素间分配</h3>
    127. <div class="container flex demo03">
    128. <span class="item">item1</span>
    129. <span class="item">item2</span>
    130. <span class="item">item3</span>
    131. </div>
    132. <h3>4. 增长因子支持小数, 因为是按增长比例分配</h3>
    133. <div class="container flex demo04">
    134. <span class="item">item1</span>
    135. <span class="item">item2</span>
    136. <span class="item">item3</span>
    137. </div>
    138. <h3>5. 每个弹性元素宽度不同时, 同样适用以上分配规律</h3>
    139. <div class="container flex demo05">
    140. <span class="item">item1</span>
    141. <span class="item">item2</span>
    142. <span class="item">item3</span>
    143. </div>
    144. </body>
    145. </html>

    2. 设置弹性元素的缩减因子(flex-shrink)

    设置了项目的缩小比例,默认为1, 即空间不足时, 自动缩小填充
    CSS语法:flex-shrink: number|initial|inherit;
    属性值:

    • number:一个数字,规定项目将相对于其他灵活的项目进行收缩的量。默认值是 1
    • initial:设置该属性为它的默认值
    • inherit:从父元素继承该属性
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>设置弹性元素的缩减因子</title>
  6. <style>
  7. /*弹性容器通用样式*/
  8. .container {
  9. border: 2px dashed greenyellow;
  10. margin: 15px;
  11. background-color: grey;
  12. width: 550px;
  13. }
  14. /*弹性元素通用样式*/
  15. .item {
  16. box-sizing: border-box;
  17. border: 1px solid red;
  18. padding: 20px;
  19. background-color: lightpink;
  20. width: 250px;
  21. }
  22. /*块级弹性容器*/
  23. .flex {
  24. display: flex;
  25. }
  26. /*所有元素不缩减,以原始宽度显示*/
  27. /*设置缩减因子为:0*/
  28. .demo01 > .item {
  29. flex-shrink: 0;
  30. }
  31. /*
  32. 所有元素自动缩放适应容器宽度且不换行显示
  33. 设置缩减因子: 1 (默认值/初始值)
  34. */
  35. .demo02 > .item {
  36. flex-shrink: 1;
  37. }
  38. .demo03 > .item:first-of-type {
  39. flex-shrink: 1;
  40. }
  41. .demo03 > .item:nth-of-type(2) {
  42. flex-shrink: 2;
  43. }
  44. .demo03 > .item:last-of-type {
  45. flex-shrink: 3;
  46. }
  47. /*
  48. 计算步骤
  49. 1 获取基本变量
  50. 1.1 多余待缩放空间 ( 250px + 250px + 250px ) - 550px = 200px
  51. 1.2 各元素缩减因子:第一个:1,第二个:2,第三个:3
  52. 1.3 缩减因子之和:1 + 2 + 3 = 6
  53. 2 计算缩减比例
  54. 1.1 缩减比例 = 缩减因子 / 缩减因子之和
  55. 1.2 第一个元素缩减比例:1 / 6 = 0.167
  56. 1.3 第二个元素缩减比例:2 / 6 = 0.333
  57. 1.4 第三个元素缩减比例:3 / 6 = 0.5
  58. 3 计算每个元素的缩减
  59. 3.1 第一个元素:200px * 0.167 = 33.4px
  60. 3.2 第二个元素:200px * 0.333 = 66.6px
  61. 3.3 第三个元素:200px * 0.5 = 100px
  62. 4 计算每个元素的最终宽度
  63. 4.1 第一个元素:250px - 33.4px = 216.6px
  64. 4.2 第二个元素:250px - 66.6px = 183.4px
  65. 4.3 第三个元素:250px - 100px = 150px
  66. */
  67. .demo04 > .item:first-of-type {
  68. flex-shrink: 0.2;
  69. }
  70. .demo04 > .item:nth-of-type(2) {
  71. flex-shrink: 0.4;
  72. }
  73. .demo04 > .item:last-of-type {
  74. flex-shrink: 0.4;
  75. }
  76. .demo05 > .item:first-of-type {
  77. width: 175px;
  78. flex-shrink: 2;
  79. }
  80. .demo05 > .item:nth-of-type(2) {
  81. width: 225px;
  82. flex-shrink: 2;
  83. }
  84. .demo05 > .item:last-of-type {
  85. width: 410px;
  86. flex-shrink: 5;
  87. }
  88. /*
  89. 元素总宽度:175px + 225px + 410px = 810px
  90. 容器宽度550px, 于是有260px的等缩减空间, 需要在每个元素中进行摊派
  91. 1. 计算缩减因子的缩小比例:等待缩减空间 / 每个弹性元素的宽度与缩减因子乘积的总和
  92. 缩减因子的缩小比例:260 / ((175*2)+(225*2)+(410*5)) = 260 / 2850 = 0.091
  93. 2. 计算每个元素的缩减量:元素宽度*(缩减因子 * 缩减因子的缩减比例)
  94. 第一个元素: 175 * ( 2 * 0.091) = 31.85px
  95. 第二个元素: 225 * ( 2 * 0.091) = 40.95px
  96. 第三个元素: 410 * ( 5 * 0.091) = 186.55px
  97. 3. 计算每个元素最终宽度
  98. 第一个元素: 175 - 31.85 = 143.15px
  99. 第二个元素: 225 - 40.95 = 184.05px
  100. 第三个元素: 410 - 186.55 = 223.45px
  101. */
  102. </style>
  103. </head>
  104. <body>
  105. <h1>设置弹性元素的缩减因子</h1>
  106. <h3>1. 所有弹性元素不缩减,以原始宽度显示,缩减因子为: 0</h3>
  107. <div class="container flex demo01">
  108. <span class="item">item1</span>
  109. <span class="item">item2</span>
  110. <span class="item">item3</span>
  111. </div>
  112. <h3>2. 所有弹性元素自适应容器宽度且不换行,缩减因子: 1 (默认)</h3>
  113. <div class="container flex demo02">
  114. <span class="item">item1</span>
  115. <span class="item">item2</span>
  116. <span class="item">item3</span>
  117. </div>
  118. <h3>3. 当三个弹性元素的缩减因子不相等时</h3>
  119. <div class="container flex demo03">
  120. <span class="item">item1</span>
  121. <span class="item">item2</span>
  122. <span class="item">item3</span>
  123. </div>
  124. <h3>4. 缩减因子也可以是小数,只要大于0就可以</h3>
  125. <div class="container flex demo04">
  126. <span class="item">item1</span>
  127. <span class="item">item2</span>
  128. <span class="item">item3</span>
  129. </div>
  130. <h3>5. 当每个弹性元素宽度不一样时, 完全是另一道风景线</h3>
  131. <div class="container flex demo05">
  132. <span class="item">item1</span>
  133. <span class="item">item2</span>
  134. <span class="item">item3</span>
  135. </div>
  136. </body>
  137. </html>

3. 设置弹性元素的基准尺寸(flex-basis)

定义了在计算分配主轴上剩余空间之前, 项目占据的主轴空间(main size)
CSS语法:flex-basis: number|auto|initial|inherit;
属性值

  • number:一个长度单位或者一个百分比,规定灵活项目的初始长度
  • auto:默认值。长度等于灵活项目的长度。如果该项目未指定长度,则长度将根据内容决定
  • initial:设置该属性为它的默认值
  • inherit:从父元素继承该属性
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>设置弹性元素的基准尺寸</title>
  6. <style>
  7. /*弹性容器通用样式*/
  8. .container {
  9. border: 2px dashed greenyellow;
  10. margin: 15px;
  11. background-color: grey;
  12. width: 550px;
  13. }
  14. /*弹性元素通用样式*/
  15. .item {
  16. box-sizing: border-box;
  17. border: 1px solid red;
  18. padding: 20px;
  19. background-color: lightpink;
  20. }
  21. /*块级弹性容器*/
  22. .flex {
  23. display: flex;
  24. }
  25. /*在未设置弹性元素宽度时, 以内容宽度显示*/
  26. .demo01 > .item {
  27. flex-basis: content;
  28. }
  29. /*存在自定义宽度时,以该宽度显示*/
  30. .demo02 > .item {
  31. width: 100px;
  32. }
  33. /*如果元素设置了宽度,则以该宽度显示*/
  34. /*如果元素的宽度也是auto,或者没有定义, 就按内容宽度content显示*/
  35. .demo03 > .item {
  36. flex-basis: auto;
  37. }
  38. /*当元素存在自定义宽度与基准宽度时, 以基准宽度为准*/
  39. .demo04 > .item {
  40. width: 120px;
  41. flex-basis: 150px;
  42. }
  43. .demo05 > :first-of-type {
  44. flex-basis: 20%;
  45. }
  46. .demo05 > :nth-of-type(2) {
  47. flex-basis: 30%;
  48. }
  49. .demo05 > :last-of-type {
  50. flex-basis: 50%;
  51. }
  52. </style>
  53. </head>
  54. <body>
  55. <h1>flex-basis:设置弹性元素的基准尺寸</h1>
  56. <h3>1: 在未设置弹性元素宽度时, 以内容宽度显示</h3>
  57. <div class="container flex demo01">
  58. <span class="item">item1</span>
  59. <span class="item">item2</span>
  60. <span class="item">item3</span>
  61. </div>
  62. <h3>2: 存在自定义宽度时,以该宽度显示</h3>
  63. <div class="container flex demo02">
  64. <span class="item">item1</span>
  65. <span class="item">item2</span>
  66. <span class="item">item3</span>
  67. </div>
  68. <h3>3: 自动状态下,由浏览器根据预设值自行判定</h3>
  69. <div class="container flex demo03">
  70. <span class="item">item1</span>
  71. <span class="item">item2</span>
  72. <span class="item">item3</span>
  73. </div>
  74. <h3>4: 当元素存在自定义宽度与基准宽度时, 以基准宽度为准</h3>
  75. <div class="container flex demo04">
  76. <span class="item">item1</span>
  77. <span class="item">item2</span>
  78. <span class="item">item3</span>
  79. </div>
  80. <h3>5: 元素基准宽度支持百分比设置</h3>
  81. <div class="container flex demo05">
  82. <span class="item">item1</span>
  83. <span class="item">item2</span>
  84. <span class="item">item3</span>
  85. </div>
  86. </body>
  87. </html>

4. 简化弹性元素的基本设置(flex)

flex属性是前面flex-grow,flex-shrink和flex-basis属性的简写
CSS语法:flex: flex-grow flex-shrink flex-basis|auto|initial|inherit;
属性值:

  • flex-grow:一个数字,规定项目将相对于其他灵活的项目进行扩展的量
  • flex-shrink:一个数字,规定项目将相对于其他灵活的项目进行收缩的量
  • flex-basis:项目的长度。合法值:”auto”、”inherit” 或一个后跟 “%”、”px”、”em” 或任何其他长度单位的数字
  • auto:与 1 1 auto 相同
  • none:与0 0 auto 相同
  • initial:设置该属性为它的默认值,即为 0 1 auto
  • inherit:从父元素继承该属性
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>简化弹性元素的基本设置</title>
  6. <style>
  7. /*弹性容器通用样式*/
  8. .container {
  9. border: 2px dashed greenyellow;
  10. margin: 15px;
  11. background-color: grey;
  12. width: 550px;
  13. }
  14. /*弹性元素通用样式*/
  15. .item {
  16. box-sizing: border-box;
  17. border: 1px solid red;
  18. padding: 20px;
  19. background-color: lightpink;
  20. }
  21. /*块级弹性容器*/
  22. .flex {
  23. display: flex;
  24. }
  25. /*根据weight,height计算,并且允许缩减大小适应容器*/
  26. .demo01 > .item {
  27. width: 100px;
  28. height: 60px;
  29. flex: initial;
  30. /*等价于默认值*/
  31. /*flex: 0 1 auto;*/
  32. }
  33. .demo02 > .item {
  34. width: 100px;
  35. height: 60px;
  36. flex: auto;
  37. /*等价于*/
  38. /*flex: 1 1 auto;*/
  39. }
  40. .demo03 > .item {
  41. width: 100px;
  42. height: 60px;
  43. flex: none;
  44. /*等价于: */
  45. /*flex: 0 0 auto;*/
  46. }
  47. .demo04 > .item {
  48. width: 100px;
  49. height: 60px;
  50. flex: 1;
  51. /*等价于*/
  52. /*flex-grow: 1;*/
  53. /*等价于:*/
  54. /*flex: 1 1 auto;*/
  55. }
  56. .demo05 > .item {
  57. width: 100px;
  58. height: 60px;
  59. flex: 1 0 200px;
  60. }
  61. .demo06 > .item {
  62. width: 100px;
  63. height: 60px;
  64. }
  65. .demo06 > .item:first-of-type {
  66. flex: 1 1 20%;
  67. }
  68. /*因为第一个元素设置了可增长,所以20%没用*/
  69. </style>
  70. </head>
  71. <body>
  72. <h1>简化弹性元素的基本设置</h1>
  73. <h3>1: 根据宽度计算,允许缩减适应容器</h3>
  74. <div class="container flex demo01">
  75. <span class="item">item1</span>
  76. <span class="item">item2</span>
  77. <span class="item">item3</span>
  78. </div>
  79. <h3>2: 根据宽度计算,元素完全弹性以适应容器</h3>
  80. <div class="container flex demo02">
  81. <span class="item">item1</span>
  82. <span class="item">item2</span>
  83. <span class="item">item3</span>
  84. </div>
  85. <h3>3: 元素完全失去弹性, 以原始大小呈现</h3>
  86. <div class="container flex demo03">
  87. <span class="item">item1</span>
  88. <span class="item">item2</span>
  89. <span class="item">item3</span>
  90. </div>
  91. <h3>4: 一个数值表示增长因子,其他值默认:flex: 1 1 auto</h3>
  92. <div class="container flex demo04">
  93. <span class="item">item1</span>
  94. <span class="item">item2</span>
  95. <span class="item">item3</span>
  96. </div>
  97. <h3>5: 第三个数有具体数值时,以它为计算标准</h3>
  98. <div class="container flex demo05">
  99. <span class="item">item1</span>
  100. <span class="item">item2</span>
  101. <span class="item">item3</span>
  102. </div>
  103. <h3>6: 单独设置某一个元素弹性大小</h3>
  104. <div class="container flex demo06">
  105. <span class="item">item1</span>
  106. <span class="item">item2</span>
  107. <span class="item">item3</span>
  108. </div>
  109. </body>
  110. </html>

5. 单独设置元素在交叉轴上排列方式(align-self)

该属性允许单个项目有与其它项目不一样的对齐方式, 可覆盖掉容器的flex-items
CSS 语法:align-self: auto|stretch|center|flex-start|flex-end|baseline|initial|inherit;
属性值:

  • auto:默认值。元素继承了它的父容器的 align-items 属性。如果没有父容器则为 “stretch”
  • stretch:元素被拉伸以适应容器
  • center:元素位于容器的中心
  • flex-start:元素位于容器的开头
  • flex-end:元素位于容器的结尾
  • baseline:元素位于容器的基线上
    initial:设置该属性为它的默认值
  • inherit:从父元素继承该属性
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>单独设置元素在交叉轴上排列方式</title>
  6. <style>
  7. .container {
  8. border: 2px dashed greenyellow;
  9. margin: 15px;
  10. background-color: grey;
  11. width: 800px;
  12. height: 500px;
  13. /*以主轴垂直为例进行演示*/
  14. flex-flow: column nowrap;
  15. /*默认元素紧贴终止线排列*/
  16. align-items: flex-end;
  17. }
  18. .item {
  19. box-sizing: border-box;
  20. border: 1px solid red;
  21. padding: 20px;
  22. background-color: lightpink;
  23. width: 120px;
  24. height: 60px;
  25. }
  26. .flex {
  27. display: flex;
  28. }
  29. .item:first-of-type {
  30. align-self: auto;
  31. background-color: lightpink;
  32. }
  33. .item:nth-of-type(2) {
  34. width: auto;
  35. align-self: stretch;
  36. background-color: lightseagreen;
  37. }
  38. .item:nth-of-type(3) {
  39. align-self: center;
  40. background-color: lightslategrey;
  41. }
  42. .item:nth-of-type(4) {
  43. align-self: flex-start;
  44. background-color: lightcyan;
  45. }
  46. .item:nth-last-of-type(3) {
  47. align-self: flex-end;
  48. background-color: lightgreen;
  49. }
  50. .item:nth-last-of-type(2) {
  51. align-self: baseline;
  52. background-color: lightblue;
  53. }
  54. .item:last-of-type {
  55. align-self: initial;
  56. background-color: olivedrab;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <h1>单独设置元素在交叉轴上排列方式</h1>
  62. <div class="container flex">
  63. <span class="item">auto</span>
  64. <span class="item">stretch</span>
  65. <span class="item">center</span>
  66. <span class="item">flex-start</span>
  67. <span class="item">flex-end</span>
  68. <span class="item">baseline</span>
  69. <span class="item">initial</span>
  70. </div>
  71. </body>
  72. </html>

6. 设置元素的排列顺序(order)

定义项目的排列顺序。数值越小,排列越靠前,默认为0
CSS语法:order: number|initial|inherit;
属性值:

  • number:默认值是 0。规定灵活项目的顺序
  • initial:设置该属性为它的默认值
  • inherit:从父元素继承该属性
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>设置元素的排列顺序</title>
  6. <style>
  7. .container {
  8. border: 2px dashed red;
  9. background-color: #ededed;
  10. margin: 20px;
  11. display: flex;
  12. width: 600px;
  13. justify-content: center;
  14. }
  15. .item {
  16. width: 100px;
  17. height: 60px;
  18. box-sizing: border-box;
  19. border: 1px solid lightgreen;
  20. padding: 20px;
  21. background-color: yellow;
  22. margin: 0 20px;
  23. }
  24. .item:first-of-type {
  25. order: 1;
  26. }
  27. .item:nth-of-type(2) {
  28. order: 3;
  29. }
  30. .item:nth-last-of-type(2) {
  31. order: 2;
  32. }
  33. .item:last-of-type {
  34. order: 0;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <h1>设置元素的排列顺序</h1>
  40. <div class="container">
  41. <span class="item">item1</span>
  42. <span class="item">item2</span>
  43. <span class="item">item3</span>
  44. <span class="item">item4</span>
  45. </div>
  46. </body>
  47. </html>

7. flex实现圣杯布局

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>flex实现圣杯布局</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. body {
  12. display: flex;
  13. flex-flow: column nowrap;
  14. height: 100vh;
  15. }
  16. header, footer {
  17. height: 60px;
  18. background-color: lightblue;
  19. }
  20. main {
  21. display: flex;
  22. flex: 1;
  23. }
  24. main > article {
  25. flex: 1;
  26. background-color: lightgreen;
  27. }
  28. main > aside {
  29. width: 200px;
  30. background-color: lightseagreen;
  31. }
  32. main > aside:first-of-type {
  33. order: -1;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <header>顶部</header>
  39. <main>
  40. <article>主体内容区</article>
  41. <aside>左侧</aside>
  42. <aside>右侧</aside>
  43. </main>
  44. <footer>底部</footer>
  45. </body>
  46. </html>

8. 仿小米商城首页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>仿小米商城首页</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. a {
  12. text-decoration: none;
  13. color: white;
  14. }
  15. li {
  16. list-style-type: none;
  17. }
  18. body {
  19. min-width: 1280px;
  20. height: 100vh;
  21. }
  22. header {
  23. height: 40px;
  24. background-color: #333333;
  25. box-sizing: border-box;
  26. }
  27. .container {
  28. box-sizing: border-box;
  29. margin: 0 auto;
  30. width: 1226px;
  31. height: 40px;
  32. display: flex;
  33. justify-content: space-between;
  34. align-items: center;
  35. font-size: 12px;
  36. }
  37. .container > a {
  38. color: #B0B0B0;
  39. border-right: 1px solid #424242;
  40. padding-right: 10px;
  41. }
  42. .container > a:nth-last-of-type(5) {
  43. margin-right: 210px;
  44. border-right: none;
  45. }
  46. .container > a:nth-last-of-type(2) {
  47. border-right: none;
  48. margin-right: 10px;
  49. }
  50. .container > a:last-of-type {
  51. padding-right: 0;
  52. border-right: none;
  53. width: 120px;
  54. height: 40px;
  55. box-sizing: border-box;
  56. display: flex;
  57. justify-content: center;
  58. align-items: center;
  59. background-color: #424242;
  60. }
  61. .container > a:hover {
  62. color: white;
  63. }
  64. .container > a:last-of-type:hover {
  65. background: white;
  66. color: #FF832F;
  67. cursor: pointer;
  68. }
  69. nav {
  70. margin: 25px auto;
  71. width: 1226px;
  72. height: 55px;
  73. display: flex;
  74. justify-content: space-between;
  75. align-items: center;
  76. box-sizing: border-box;
  77. }
  78. nav a {
  79. color: #333333;
  80. font-size: 16px;
  81. }
  82. nav > a:first-of-type > img {
  83. width: 55px;
  84. height: 55px;
  85. }
  86. nav > form {
  87. width: 296px;
  88. height: 50px;
  89. box-sizing: border-box;
  90. display: flex;
  91. }
  92. nav > form > input {
  93. box-sizing: border-box;
  94. flex: 1;
  95. padding: 0 10px;
  96. border: 1px solid #ededed;
  97. border-right: none;
  98. }
  99. nav > form > label {
  100. box-sizing: border-box;
  101. width: 52px;
  102. display: flex;
  103. align-items: center;
  104. justify-content: center;
  105. border: 1px solid #ededed;
  106. }
  107. nav > form > label:hover {
  108. background: #FF6700;
  109. color: white;
  110. cursor: pointer;
  111. }
  112. nav > div {
  113. margin-left: 50px;
  114. }
  115. nav > div > a {
  116. margin: 0 5px;
  117. }
  118. nav > div > a:hover {
  119. color: #FF832F;
  120. }
  121. main {
  122. width: 1226px;
  123. margin: 0 auto;
  124. }
  125. main > article:first-of-type {
  126. display: flex;
  127. box-sizing: border-box;
  128. }
  129. main > article:first-of-type > ul {
  130. background: #8B9DA3;
  131. box-sizing: border-box;
  132. width: 234px;
  133. height: 460px;
  134. display: flex;
  135. flex-flow: column nowrap;
  136. justify-content: center;
  137. }
  138. main > article:first-of-type > ul > li {
  139. box-sizing: border-box;
  140. width: 234px;
  141. height: 42px;
  142. padding: 0 30px;
  143. display: flex;
  144. justify-content: space-between;
  145. align-items: center;
  146. cursor: pointer;
  147. }
  148. main > article:first-of-type > ul > li:hover {
  149. background: #FF6700;
  150. }
  151. main > article:first-of-type > ul > li > span {
  152. color: #ededed;
  153. }
  154. main > article:first-of-type > img {
  155. width: 992px;
  156. height: 460px;
  157. }
  158. main > article:nth-of-type(2) {
  159. display: flex;
  160. box-sizing: border-box;
  161. margin: 20px 0;
  162. justify-content: space-between;
  163. }
  164. main > article:nth-of-type(2) > div {
  165. width: 234px;
  166. height: 170px;
  167. box-sizing: border-box;
  168. background: #5F5750;
  169. padding: 5px;
  170. display: flex;
  171. flex-flow: row wrap;
  172. justify-content: center;
  173. align-items: center;
  174. }
  175. main > article:nth-of-type(2) > div li a {
  176. display: flex;
  177. flex-flow: column nowrap;
  178. align-items: center;
  179. box-sizing: border-box;
  180. margin: 10px;
  181. }
  182. main > article:nth-of-type(2) > div img {
  183. width: 24px;
  184. }
  185. main > article:nth-of-type(2) > div > ul {
  186. display: flex;
  187. }
  188. main > article:nth-of-type(2) > div > ul > li > a > span {
  189. font-size: 12px;
  190. color: #CFCCCA;
  191. margin-top: 3px;
  192. }
  193. main > article:nth-of-type(2) > div > ul > li > a > span:hover {
  194. color: white;
  195. }
  196. main > article:nth-of-type(2) > a {
  197. box-sizing: border-box;
  198. height: 170px;
  199. }
  200. main > article:nth-of-type(2) > a > img {
  201. width: 316px;
  202. height: 170px;
  203. }
  204. main > article:nth-of-type(2) > a:hover {
  205. box-shadow: 0 0 3px #757575;
  206. }
  207. .page-main {
  208. background: #F5F5F5;
  209. height: 600px;
  210. }
  211. .recommend > h2 {
  212. margin-top: 20px;
  213. color: #333333;
  214. font-size: 22px;
  215. font-weight: normal;
  216. }
  217. .recommend {
  218. width: 1226px;
  219. margin: 0 auto;
  220. display: flex;
  221. flex-flow: column nowrap;
  222. }
  223. .recommend > ul {
  224. display: flex;
  225. justify-content: space-between;
  226. box-sizing: border-box;
  227. margin: 20px 0;
  228. }
  229. .recommend > ul > li > a {
  230. box-sizing: border-box;
  231. width: 234px;
  232. height: 340px;
  233. display: flex;
  234. flex-flow: column nowrap;
  235. align-items: center;
  236. justify-content: center;
  237. background: white;
  238. }
  239. .recommend > ul > li:first-of-type > a {
  240. background: #F1EDED;
  241. border-top: 1px solid red;
  242. }
  243. .recommend > ul > li:nth-of-type(2) > a {
  244. border-top: 1px solid #FFAC13;
  245. }
  246. .recommend > ul > li:nth-of-type(3) > a {
  247. border-top: 1px solid #83C44E;
  248. }
  249. .recommend > ul > li:nth-last-of-type(2) > a {
  250. border-top: 1px solid #2196F3;
  251. }
  252. .recommend > ul > li:last-of-type > a {
  253. border-top: 1px solid #E53935;
  254. }
  255. .recommend > ul > li > a > img {
  256. width: 160px;
  257. height: 165.6px;
  258. }
  259. .recommend > ul > li > a > h3 {
  260. margin: 20px 0 5px 0;
  261. color: black;
  262. font-size: 14px;
  263. font-weight: normal;
  264. }
  265. .recommend > ul > li > a > span {
  266. font-size: 12px;
  267. color: #b0b0b0;
  268. margin-bottom: 15px;
  269. }
  270. .recommend > ul > li > a > p > span {
  271. color: #ff6709;
  272. font-size: 14px;
  273. }
  274. .recommend > ul > li > a > p > del {
  275. margin: 5px;
  276. color: #b0b0b0;
  277. font-size: 14px;
  278. }
  279. .recommend > ul > li:first-of-type > a > img {
  280. width: 34px;
  281. height: 53px;
  282. margin: 30px 0;
  283. }
  284. .recommend > ul > li:first-of-type > a > h3 {
  285. font-size: 21px;
  286. color: #ef3a3b;
  287. }
  288. .recommend > ul > li:first-of-type > a > span {
  289. color: rgba(0, 0, 0, .54);
  290. font-size: 15px;
  291. }
  292. .recommend > ul > li:first-of-type > a > p > span {
  293. width: 46px;
  294. height: 46px;
  295. box-sizing: border-box;
  296. background: #605751;
  297. font-size: 24px;
  298. color: #fff;
  299. line-height: 46px;
  300. padding: 6px;
  301. }
  302. .recommend > ul > li:first-of-type > a > p > i {
  303. color: black;
  304. font-size: 20px;
  305. font-style: normal;
  306. margin: 0 5px;
  307. }
  308. .footer1 {
  309. width: 1226px;
  310. height: 80px;
  311. margin: 0 auto;
  312. box-sizing: border-box;
  313. display: flex;
  314. justify-content: space-around;
  315. align-items: center;
  316. border-bottom: 1px solid #E0E0E0;
  317. }
  318. .footer1 > a {
  319. color: #616161;
  320. font-size: 16px;
  321. }
  322. .footer1 > a:hover {
  323. color: #FF6E0B;
  324. }
  325. .footer1 > span {
  326. color: #E0E0E0;
  327. }
  328. .footer2 {
  329. width: 1226px;
  330. height: 192px;
  331. margin: 0 auto;
  332. box-sizing: border-box;
  333. display: flex;
  334. align-items: center;
  335. }
  336. .footer2 > dl {
  337. width: 160px;
  338. height: 112px;
  339. box-sizing: border-box;
  340. }
  341. .footer2 > dl > dt {
  342. font-size: 14px;
  343. margin: 0 0 26px 0;
  344. }
  345. .footer2 > dl > dd {
  346. font-size: 12px;
  347. margin-top: 10px;
  348. }
  349. .footer2 > dl > dd > a {
  350. color: #7F7F7F;
  351. }
  352. .footer2 > dl > dd > a:hover {
  353. color: #FF832F;
  354. }
  355. .footer2 > div {
  356. width: 252px;
  357. height: 112px;
  358. box-sizing: border-box;
  359. border-left: 1px solid #E0E0E0;
  360. margin-left: 15px;
  361. display: flex;
  362. flex-flow: column nowrap;
  363. align-items: center;
  364. }
  365. .footer2 > div > h3 {
  366. font-weight: normal;
  367. font-size: 22px;
  368. color: #FF6700;
  369. margin-bottom: 5px;
  370. }
  371. .footer2 > div > p, span {
  372. font-size: 12px;
  373. color: #616161;
  374. }
  375. .footer2 > div > span {
  376. margin: 2px 0 16px 0;
  377. }
  378. .footer2 > div > button {
  379. width: 120px;
  380. height: 30px;
  381. box-sizing: border-box;
  382. border: 1px solid #FF6700;
  383. font-size: 12px;
  384. color: #FF6700;
  385. background: white;
  386. cursor: pointer;
  387. }
  388. .footer2 > div > button:hover {
  389. background: #FF6700;
  390. color: white;
  391. }
  392. .footer3 {
  393. height: 248px;
  394. background: #FAFAFA;
  395. }
  396. .footer3 > div {
  397. width: 1226px;
  398. height: 200px;
  399. margin: 0 auto;
  400. display: flex;
  401. align-items: center;
  402. }
  403. .footer3 > div > img {
  404. width: 57px;
  405. height: 57px;
  406. align-self: flex-start;
  407. margin-top: 30px;
  408. }
  409. .message {
  410. margin-left: 10px;
  411. }
  412. .message > div:first-of-type > a{
  413. font-size: 12px;
  414. color: #757575;
  415. margin-right: 5px;
  416. }
  417. .message > div:first-of-type > a:hover {
  418. color: #FF832F;
  419. }
  420. .message > p {
  421. font-size: 12px;
  422. color: #B0B0B0;
  423. margin: 2px 0;
  424. }
  425. .message > div:last-of-type > img:first-of-type {
  426. width: 83px;
  427. height: 28px;
  428. }
  429. </style>
  430. </head>
  431. <body>
  432. <header>
  433. <div class="container">
  434. <a href="">小米商城</a>
  435. <a href="">MIUI</a>
  436. <a href="">IoT</a>
  437. <a href="">云服务</a>
  438. <a href="">金融</a>
  439. <a href="">有品</a>
  440. <a href="">小爱开放平台</a>
  441. <a href="">企业团购</a>
  442. <a href="">资历证照</a>
  443. <a href="">协议规则</a>
  444. <a href="">下载app</a>
  445. <a href="">Select Location</a>
  446. <a href="">登录</a>
  447. <a href="">注册</a>
  448. <a href="">消息通知</a>
  449. <a>购物车(0)</a>
  450. </div>
  451. </header>
  452. <nav>
  453. <a href=""><img src="static/images/mi_logo.png" alt=""></a>
  454. <div>
  455. <a href="">小米手机</a>
  456. <a href="">Redmi 红米</a>
  457. <a href="">电视</a>
  458. <a href="">笔记本</a>
  459. <a href="">家电</a>
  460. <a href="">路由器</a>
  461. <a href="">智能硬件</a>
  462. <a href="">服务</a>
  463. <a href="">社区</a>
  464. </div>
  465. <form action="" method="get">
  466. <input type="text" name="search" id="search">
  467. <label for="search">搜索</label>
  468. </form>
  469. </nav>
  470. <main>
  471. <article>
  472. <ul>
  473. <li><a href="">手机 电话卡</a><span>&gt</span></li>
  474. <li><a href="">电视 盒子</a><span>&gt</span></li>
  475. <li><a href="">笔记本 平板</a><span>&gt</span></li>
  476. <li><a href="">家电 插线板</a><span>&gt</span></li>
  477. <li><a href="">出行 穿戴</a><span>&gt</span></li>
  478. <li><a href="">智能 路由器</a><span>&gt</span></li>
  479. <li><a href="">电源 配件</a><span>&gt</span></li>
  480. <li><a href="">健康 儿童</a><span>&gt</span></li>
  481. <li><a href="">耳机 音箱</a><span>&gt</span></li>
  482. <li><a href="">生活 箱包</a><span>&gt</span></li>
  483. </ul>
  484. <img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/ecac78f468f95f59996ab7978c1de8e3.jpg?thumb=1&w=1533&h=575&f=webp&q=90"
  485. alt="">
  486. </article>
  487. <article>
  488. <div>
  489. <ul>
  490. <li>
  491. <a href="">
  492. <img src="static/images/d1.png" alt="">
  493. <span>小米秒杀</span>
  494. </a>
  495. </li>
  496. <li>
  497. <a href="">
  498. <img src="static/images/d2.png" alt="">
  499. <span>企业团购</span>
  500. </a>
  501. </li>
  502. <li>
  503. <a href="">
  504. <img src="static/images/d3.png" alt="">
  505. <span>F码通道</span>
  506. </a>
  507. </li>
  508. </ul>
  509. <ul>
  510. <li>
  511. <a href="">
  512. <img src="static/images/d4.png" alt="">
  513. <span>米粉卡</span>
  514. </a>
  515. </li>
  516. <li>
  517. <a href="">
  518. <img src="static/images/d5.png" alt="">
  519. <span>以旧换新</span>
  520. </a>
  521. </li>
  522. <li>
  523. <a href="">
  524. <img src="static/images/d6.png" alt="">
  525. <span>话费充值</span>
  526. </a>
  527. </li>
  528. </ul>
  529. </div>
  530. <a href=""><img src="static/images/redmi.jpg" alt=""></a>
  531. <a href=""><img src="static/images/mi9.jpg" alt=""></a>
  532. <a href=""><img src="static/images/huan4.jpg" alt=""></a>
  533. </article>
  534. </main>
  535. <div class="page-main">
  536. <div class="recommend">
  537. <h2>小米闪购</h2>
  538. <ul>
  539. <li>
  540. <a href="">
  541. <h3>00:00 场</h3>
  542. <img src="static/images/light.png" alt="">
  543. <span>距离结束还有</span>
  544. <p><span>07</span><i>:</i><span>35</span><i>:</i><span>46</span></p>
  545. </a>
  546. </li>
  547. <li>
  548. <a href="">
  549. <img src="static/images/m1.jpg" alt="">
  550. <h3>小米AI音箱</h3>
  551. <span>听音乐、语音遥控家电</span>
  552. <p><span>229元</span>
  553. <del>299元</del>
  554. </p>
  555. </a>
  556. </li>
  557. <li>
  558. <a href="">
  559. <img src="static/images/m3.jpg" alt="">
  560. <h3>小米小爱音箱 万能遥控版 黑色</h3>
  561. <span>传统家电秒变智能</span>
  562. <p><span>99元</span>
  563. <del>199元</del>
  564. </p>
  565. </a>
  566. </li>
  567. <li>
  568. <a href="">
  569. <img src="static/images/m2.jpg" alt="">
  570. <h3>Redmi Note 7 6GB+64GB</h3>
  571. <span>4800万拍照千元机</span>
  572. <p><span>1049元</span>
  573. <del>1099元</del>
  574. </p>
  575. </a>
  576. </li>
  577. <li>
  578. <a href="">
  579. <img src="static/images/m4.png" alt="">
  580. <h3>Redmi Note 8 Pro</h3>
  581. <span>6400万全场景四摄</span>
  582. <p><span>1299元</span>
  583. <del>1399元</del>
  584. </p>
  585. </a>
  586. </li>
  587. </ul>
  588. <img src="static/images/m5.png" alt="">
  589. </div>
  590. </div>
  591. <footer>
  592. <div class="footer1">
  593. <a href="">预约维修服务</a>
  594. <span>|</span>
  595. <a href="">7天无理由退货</a>
  596. <span>|</span>
  597. <a href="">15天免费换货</a>
  598. <span>|</span>
  599. <a href="">满150元包邮</a>
  600. <span>|</span>
  601. <a href="">520余家售后网点</a>
  602. </div>
  603. <div class="footer2">
  604. <dl>
  605. <dt>帮助中心</dt>
  606. <dd><a href="">账户管理</a></dd>
  607. <dd><a href="">购物指南</a></dd>
  608. <dd><a href="">订单操作</a></dd>
  609. </dl>
  610. <dl>
  611. <dt>服务支持</dt>
  612. <dd><a href="">售后政策</a></dd>
  613. <dd><a href="">自助服务</a></dd>
  614. <dd><a href="">相关下载</a></dd>
  615. </dl>
  616. <dl>
  617. <dt>线下门店</dt>
  618. <dd><a href="">小米之家</a></dd>
  619. <dd><a href="">服务网点</a></dd>
  620. <dd><a href="">授权体验店</a></dd>
  621. </dl>
  622. <dl>
  623. <dt>关于小米</dt>
  624. <dd><a href="">了解小米</a></dd>
  625. <dd><a href="">加入小米</a></dd>
  626. <dd><a href="">投资者关系</a></dd>
  627. </dl>
  628. <dl>
  629. <dt>关注我们</dt>
  630. <dd><a href="">新浪微博</a></dd>
  631. <dd><a href="">官方微信</a></dd>
  632. <dd><a href="">联系我们</a></dd>
  633. </dl>
  634. <dl>
  635. <dt>特色服务</dt>
  636. <dd><a href="">F码通道</a></dd>
  637. <dd><a href="">礼物码</a></dd>
  638. <dd><a href="">防伪查询</a></dd>
  639. </dl>
  640. <div>
  641. <h3>400-100-5678</h3>
  642. <p>周一至周日 8:00-18:00</p>
  643. <span>(仅收市话费)</span>
  644. <button>人工客服</button>
  645. </div>
  646. </div>
  647. <div class="footer3">
  648. <div>
  649. <img src="static/images/mi_logo.png" alt="">
  650. <div class="message">
  651. <div>
  652. <a href="">小米商城</a><span>|</span>
  653. <a href="">MIUI</a><span>|</span>
  654. <a href="">米家</a><span>|</span>
  655. <a href="">米聊</a><span>|</span>
  656. <a href="">多看</a><span>|</span>
  657. <a href="">游戏</a><span>|</span>
  658. <a href="">路由器</a><span>|</span>
  659. <a href="">米粉卡</a><span>|</span>
  660. <a href="">政企服务</a><span>|</span>
  661. <a href="">小米天猫店</a><span>|</span>
  662. <a href="">小米集团隐私</a><span>|</span>
  663. <a href="">小米公司儿童信息保护规则</a><span>|</span>
  664. <a href="">小米商城隐私政策</a><span>|</span>
  665. <a href="">小米商城用户协议</a><span>|</span>
  666. <a href="">问题反馈</a><span>|</span>
  667. <a href="">廉政举报</a><span>|</span>
  668. <a href="">诚信合规</a><span>|</span>
  669. <a href="">Select Location</a>
  670. </div>
  671. <p>
  672. © mi.com 京ICP证110507号 京ICP备10046444号 京公网安备11010802020134号 京网文[2017]1530-131号<br>
  673. (京)网械平台备字(2018)第00005号 互联网药品信息服务资格证 (京) -非经营性-2014-0090 营业执照 医疗器械公告<br>
  674. 增值电信业务许可证 网络食品经营备案(京)【2018】WLSPJYBAHF-12 食品经营许可证<br>
  675. 违法和不良信息举报电话:185-0130-1238 知识产权侵权投诉 本网站所列数据,除特殊说明,所有数据均出自我司实验室测试
  676. </p>
  677. <div>
  678. <img src="static/images/fd1.png" alt="">
  679. <img src="static/images/fd2.png" alt="">
  680. <img src="static/images/fd3.png" alt="">
  681. <img src="static/images/fd4.png" alt="">
  682. <img src="static/images/fd5.png" alt="">
  683. </div>
  684. </div>
  685. </div>
  686. </div>
  687. </footer>
  688. </body>
  689. </html>




9. 总结

通过对flex的学习,掌握了flex的全部属性,并通过实例和模仿现成网页加深了对flex的认知和理解。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议