博客列表 >flex布局知识

flex布局知识

ROC-Y
ROC-Y原创
2020年04月13日 02:06:17568浏览

一、 display属性

  • flex ,创建flex块级容器
  • inline-flex, 创建行内容器

  1. <style type="text/css">
  2. .container {
  3. width: 400px;
  4. height: 500px;
  5. display: flex;
  6. }
  7. #div51 {
  8. background-color: red;
  9. width: 200px;
  10. height: 150px;
  11. }
  12. #div52 {
  13. background-color: green;
  14. width: 200px;
  15. height: 150px;
  16. }
  17. #div53 {
  18. background-color: orange;
  19. width: 300px;
  20. height: 150px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="container">
  26. <div id="div51">AAAAAAAAAAAAAAA</div>
  27. <div id="div52">BBBBBBBBBBBBBBB</div>
  28. <div id="div53">CCCCCCCCCCCCCCC</div>
  29. </div>
  30. </body>

二、flex 容器主轴方向flex-direction属性

序号 属性值 描述
1 row默认值 主轴水平: 起始线居中,项目从左到右显示
2 row-reverse 主轴水平:起始线居右, 项目从右向左显示
3 column 主轴垂直: 起始线居上,项目从上向下显示
4 column-reverse 主轴垂直: 起始线居下,项目从下向上显示
  • row

    1. <style type="text/css">
    2. .container {
    3. width: 400px;
    4. height: 500px;
    5. display: flex;
    6. flex-direction: row;
    7. }

  • row-reverse

  • column-reverse

三、flex容器主轴项目换行flex-wrap属性

1.nowrap默认值 , 项目不换行: 单行容器

2.wrap, 项目换行: 多行容器,第一行在上方

3.wrap-reverse , 项目换行: 多行容器,第一行在下方

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style type="text/css">
  8. .container {
  9. width: 400px;
  10. height: 500px;
  11. display: flex;
  12. flex-direction: row;
  13. flex-wrap: wrap-reverse;
  14. }
  15. #div51 {
  16. background-color: red;
  17. width: 200px;
  18. height: 150px;
  19. }
  20. #div52 {
  21. background-color: green;
  22. width: 200px;
  23. height: 150px;
  24. }
  25. #div53 {
  26. background-color: orange;
  27. width: 300px;
  28. height: 150px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="container">
  34. <div id="div51">AAAAAAAAAAAAAAA</div>
  35. <div id="div52">BBBBBBBBBBBBBBB</div>
  36. <div id="div53">CCCCCCCCCCCCCCC</div>
  37. </div>
  38. </body>
  39. </html>

四、flex 容器主轴与项目换行简写

1. flex-flow属性

  • 以后推荐只用它
  • flex-flow是属性flex-directionflex-wrap的简写
  • 语法: flex-flow: flex-direction flex-wrap
属性值 描述
row nowrap默认值 主轴水平, 项目不换行
  1. <style type="text/css">
  2. .container {
  3. width: 400px;
  4. height: 500px;
  5. display: flex;
  6. flex-flow: column wrap-reverse;
  7. }

五、flex 容器主轴项目对齐

1. justify-content属性

当容器中主轴方向上存在剩余空间时, 该属性才有意义

序号 属性值 描述
1 flex-start默认 所有项目与主轴起始线对齐
2 flex-end 所有项目与主轴终止线对齐
3 center 所有项目与主轴中间线对齐: 居中对齐
4 space-between 两端对齐: 剩余空间在头尾项目之外的项目间平均分配
5 space-around 分散对齐: 剩余空间在每个项目二侧平均分配
6 space-evenly 平均对齐: 剩余空间在每个项目之间平均分配
  1. <style type="text/css">
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. justify-content: center;
  7. }

  1. <style type="text/css">
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. justify-content: space-around;
  7. }

  1. <style type="text/css">
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. /* justify-content: space-around; */
  7. justify-content: space-between;
  8. }

  1. <style type="text/css">
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. /* justify-content: space-around; */
  7. justify-content: space-evenly;
  8. }

六、flex 容器交叉轴项目对齐

1. align-items属性

  • 该属性仅适用于: 单行容器
  • 当容器中交叉轴方向上存在剩余空间时, 该属性才有意义
序号 属性值 描述
1 flex-start默认 与交叉轴起始线对齐
2 flex-end 与交叉轴终止线对齐
3 center 与交叉轴中间线对齐: 居中对齐



七、flex 多行容器交叉轴项目对齐

1. align-content属性

  • 该属性仅适用于: 多行容器
  • 多行容器中, 交叉轴会有多个项目, 剩余空间在项目之间分配才有意义

    提示: 多行容器中通过设置flex-wrap: wrap | wrap-reverse实现

序号 属性值 描述
1 stretch默认 项目拉伸占据整个交叉轴
1 flex-start 所有项目与交叉轴起始线(顶部)对齐
2 flex-end 所有项目与交叉轴终止线对齐
3 center 所有项目与交叉轴中间线对齐: 居中对齐
4 space-between 两端对齐: 剩余空间在头尾项目之外的项目间平均分配
5 space-around 分散对齐: 剩余空间在每个项目二侧平均分配
6 space-evenly 平均对齐: 剩余空间在每个项目之间平均分配
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style type="text/css">
  8. .container {
  9. width: 500px;
  10. height: 300px;
  11. display: flex;
  12. flex-flow: column wrap;
  13. align-content: center;
  14. }
  15. .div51 {
  16. background-color: greenyellow;
  17. width: 100px;
  18. height: 50px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="div51">AAAA</div>
  25. <div class="div51">BBBB</div>
  26. <div class="div51">CCCC</div>
  27. <div class="div51">DDDD</div>
  28. <div class="div51">EEEE</div>
  29. <div class="div51">FFFF</div>
  30. <div class="div51">GGGG</div>
  31. <div class="div51">HHHH</div>
  32. <div class="div51">JJJJ</div>
  33. </div>
  34. </body>
  35. </html>







八、order属性,调整块位置

  1. <style type="text/css">
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. }
  7. .div51 {
  8. background-color: greenyellow;
  9. width: 100px;
  10. height: 50px;
  11. }
  12. .div51:first-of-type {
  13. order: 4;
  14. }
  15. .div51:nth-of-type(2) {
  16. order: 3;
  17. }
  18. </style>

九、flex 项目交叉轴单独对齐

1. align-self属性

  • 该属性可覆盖容器的align-items, 用以自定义某个项目的对齐方式
序号 属性值 描述
1 auto默认值 继承 align-items 属性值
2 flex-start 与交叉轴起始线对齐
3 flex-end 与交叉轴终止线对齐
4 center 与交叉轴中间线对齐: 居中对齐
5 stretch 在交叉轴方向上拉伸
6 baseline 与基线对齐(与内容相关用得极少)
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style type="text/css">
  8. .container {
  9. width: 500px;
  10. height: 300px;
  11. display: flex;
  12. }
  13. .div51 {
  14. background-color: greenyellow;
  15. width: 100px;
  16. height: 50px;
  17. }
  18. .div51:first-of-type {
  19. align-self: stretch;
  20. }
  21. .div51:nth-of-type(2) {
  22. align-self: flex-end;
  23. }
  24. .div51:nth-of-type(3) {
  25. align-self: center;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="container">
  31. <div class="div51">AAAA</div>
  32. <div class="div51">BBBB</div>
  33. <div class="div51">CCCC</div>
  34. <div class="div51">DDDD</div>
  35. </div>
  36. </body>
  37. </html

十、flex 项目放大因子

1. flex-grow属性

  • 在容器主轴上存在剩余空间时, flex-grow才有意义
  • 该属性的值,称为放大因子, 常见的属性值如下:
    /(500-400)/(2+2+1)2+100=140*/
序号 属性值 描述
1 0默认值 不放大,保持初始值
2 initial 设置默认值,与0等效
3 n 放大因子: 正数
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style type="text/css">
  8. .container {
  9. width: 500px;
  10. height: 300px;
  11. display: flex;
  12. }
  13. .div51 {
  14. background-color: greenyellow;
  15. width: 100px;
  16. height: 50px;
  17. /* 允许放大 */
  18. flex-grow: 0;
  19. }
  20. .div51:first-of-type {
  21. flex-grow: 2;
  22. /*(500-400)/(2+2+1)*2+100=140*/
  23. }
  24. .div51:nth-of-type(2) {
  25. flex-grow: 2;
  26. }
  27. .div51:nth-of-type(3) {
  28. flex-grow: 1;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="container">
  34. <div class="div51">AAAA</div>
  35. <div class="div51">BBBB</div>
  36. <div class="div51">CCCC</div>
  37. <div class="div51">DDDD</div>
  38. </div>
  39. </body>
  40. </html>

十一、flex 项目收缩因子

1. flex-shrink属性

  • 当容器主轴 “空间不足” 且 “禁止换行” 时, flex-shrink才有意义
  • 该属性的值,称为收缩因子, 常见的属性值如下:
序号 属性值 描述
1 1默认值 允许项目收缩
2 initial 设置初始默认值,与 1 等效
3 0 禁止收缩,保持原始尺寸
4 n 收缩因子: 正数
  1. <style type="text/css">
  2. .container {
  3. width: 200px;
  4. height: 300px;
  5. display: flex;
  6. flex-flow: row nowrap;
  7. }
  8. .div51 {
  9. background-color: greenyellow;
  10. width: 100px;
  11. height: 50px;
  12. }
  13. .div51:first-of-type {
  14. flex-shrink: 3;
  15. }
  16. .div51:nth-of-type(2) {
  17. flex-shrink: 2;
  18. }
  19. .div51:nth-of-type(3) {
  20. flex-shrink: 1;
  21. }
  22. </style>

十二、flex 项目计算尺寸

1. flex-basis属性

  • 在分配多余空间之前,项目占据的主轴空间
  • 浏览器根据这个属性,计算主轴是否有多余空间
  • 该属性会覆盖项目原始大小(width/height)
  • 该属性会被项目的min-width/min-height值覆盖
序号 属性值 描述
1 auto 默认值: 项目原来的大小
2 px 像素
3 % 百分比
  1. <style type="text/css">
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. flex-flow: row wrap;
  7. }
  8. .div51 {
  9. background-color: greenyellow;
  10. width: 100px;
  11. height: 50px;
  12. flex-basis: 200px;
  13. /* max-width: 优先级大于flex-basis,故此处150生效; */
  14. max-width: 150px;
  15. }
  16. </style>

十三、flex 项目缩放的简写

1. flex属性

  • 项目放大,缩小与计算尺寸,对于项目非常重要,也很常用
  • 每次都要写这三个属性,非常的麻烦,且没有必要
  • flex属性,可以将以上三个属性进行简化:
  • 语法: flex: flex-grow flex-shrink flex-basis

  • 示例初始状态:

    1.1 三值语法

序号 属性值 描述
1 第一个值: 整数 flex-grow
2 第二个值: 整数 flex-shrink
3 第三个值: 有效宽度 flex-basis

举例:

序号 案例 描述
1 flex: 0 1 auto 默认值: 不放大,可收缩, 初始宽度
2 flex: 1 1 auto 项目自动放大或收缩适应容器
3 flex: 0 0 100px 按计算大小填充到容器中

  1. <style type="text/css">
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. flex-flow: row nowrap;
  7. }
  8. .div51 {
  9. background-color: greenyellow;
  10. width: 100px;
  11. height: 50px;
  12. }
  13. .div51:first-of-type {
  14. background-color: greenyellow;
  15. width: 400px;
  16. height: 50px;
  17. flex: 0 5 auto;
  18. }
  19. .div51:nth-of-type(2) {
  20. background-color: pink;
  21. width: 50px;
  22. height: 50px;
  23. }
  24. .div51:nth-of-type(3) {
  25. background-color: blue;
  26. width: 200px;
  27. height: 50px;
  28. }
  29. </style>

1.2 双值语法

序号 属性值 描述
1 第一个值: 整数 flex-grow
3 第二个值: 有效宽度 flex-basis

举例:

案例 描述
flex: 0 180px 禁止放大,按计算大小填充到容器中

1.3 单值语法

序号 属性值 描述
1 整数 flex-grow
2 有效宽度 flex-basis
3 关键字 `initial auto none`

举例:

序号 案例 描述
1 flex: 1 flex: 1 1 auto
2 flex: 180px flex: 1 1 180px
3 initial flex: 0 1 auto
4 auto flex: 1 1 auto
5 none flex: 0 0 auto

  1. <style>
  2. .container {
  3. width: 500px;
  4. height: 300px;
  5. display: flex;
  6. flex-flow: row nowrap;
  7. }
  8. .div51 {
  9. background-color: greenyellow;
  10. width: 100px;
  11. height: 50px;
  12. }
  13. .div51:first-of-type {
  14. background-color: greenyellow;
  15. width: 400px;
  16. height: 50px;
  17. flex: 1;
  18. }
  19. .div51:nth-of-type(2) {
  20. background-color: pink;
  21. width: 50px;
  22. height: 50px;
  23. flex: 200px;
  24. }
  25. .div51:nth-of-type(3) {
  26. background-color: blue;
  27. width: 200px;
  28. height: 50px;
  29. }
  30. </style>

推荐使用flex, 就像推荐使用flex-grow设置主轴与换行一样

总结

  • flex容器水平排列时候,容纳不下,块大小会压缩,默认不换行。
  • flex-grow放大因子计算,(剩余空间) / (因子之和) * (对应块因子数)+ 块原大小 = 对应块新大小
  • flex-basis优先级优先级: 项目大小 < flex-basis < min-width/height
  • 属性很多,多用才能在实际使用的时候选择最优属性。
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议