Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。
任何一个容器都可以指定为flex布局:
.box{ display: flex;}
行内元素也可以使用 Flex 布局:
.box{ display: inline-flex;}
Webkit 内核的浏览器,必须加上-webkit前缀:
-webkit
.box{ display: -webkit-flex; /* Safari */ display: flex;}
??Flex布局与传统布局对比:
传统布局:
FLex布局:
??Flex布局原理:
--通过给父元素添加flex属性来控制子元素的位置和排列方式
??注意:设为 Flex 布局以后,子元素的float、clear和vertical-align属性将失效。
float
clear
vertical-align
采用 Flex 布局的元素,称为 Flex 容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为 Flex 项目(flex item),简称"项目"。
容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end、
main start
main end
cross start
cross end、
项目默认沿主轴排列,单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size
main size
cross size
1)flex-direction:设置主轴方向
2)justify-content:设置主轴上的子元素排列方式
3)flex-wrap:设置子元素是否换行
4)align-content:设置侧轴上子元素排列方式(多行)
5)align-items:设置侧轴上子元素排列方式(单行)
6)flex-flow:复合属性,同时设置了flex-direction和flex-wrap
flex-direction属性决定主轴的方向(即项目的排列方向)
flex-direction
语法:
.box { flex-direction: row | row-reverse | column | column-reverse;}
属性值:
row
row-reverse
column
column-reverse
完整示例:
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; /* 设置容器内部元素排列方向 */ flex-direction: row; } .box{ width: 120px; height: 120px; } .red{ background-color: red; } .blue{ background-color: blue; } .green{ background-color: green; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> </div> </body></html>
示例效果:
默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
flex-wrap
.box{ flex-wrap: nowrap | wrap | wrap-reverse;}
nowrap
wrap
wrap-reverse
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; /* 设置容器内部元素排列方向 */ flex-direction: row; /* 设置容器内部元素如何换行 */ flex-wrap: nowrap|wrap|wrap-reverse; } .box{ width: 120px; height: 120px; } .red{ background-color: red; } .blue{ background-color: blue; } .green{ background-color: green; } .yellow{ background-color: yellow; } .orange{ background-color: orange; } .pink{ background-color: pink; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> <div class="box yellow">4</div> <div class="box orange">5</div> <div class="box pink">6</div> </div> </body></html>
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap
flex-flow
row nowrap
.box { flex-flow: <flex-direction> || <flex-wrap>;}
.box{ flex-direction: row; flex-wrap: wrap-reverse; /* 等同于 */ flex-flow:row wrap-reverse }
justify-content属性定义了项目在主轴上的对齐方式
justify-content
.box { justify-content: flex-start | flex-end | center | space-between | space-around;}
flex-start
flex-end
center
space-between
space-around
space-evenly
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; /* 设置容器内部元素排列方向 */ flex-direction: row; /* 设置容器内部元素如何换行 */ flex-wrap: wrap; /* 定义项目在主轴上的对齐方式 */ justify-content: flex-start | flex-end | center | space-between | space-around; } .box{ width: 120px; height: 120px; } .red{ background-color: red; } .blue{ background-color: blue; } .green{ background-color: green; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> </div> </body></html>
align-items属性定义项目在交叉轴上如何对齐
align-items
.box { align-items: flex-start | flex-end | center | baseline | stretch;}
baseline
stretch
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; /* 设置容器内部元素排列方向 */ flex-direction: row; /* 设置容器内部元素如何换行 */ flex-wrap: wrap; /* 定义项目在交叉轴上的对齐方式 */ align-items: flex-start | flex-end | center | baseline | stretch; } .box{ width: 120px; height: 120px; } .red{ background-color: red; } .blue{ background-color: blue; } .green{ background-color: green; } /* .yellow{ background-color: yellow; } .orange{ background-color: orange; } .pink{ background-color: pink; } */ </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> <!-- <div class="box yellow">4</div> <div class="box orange">5</div> <div class="box pink">6</div> --> </div> </body></html>
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用
align-content
.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch;}
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; /* 设置容器内部元素排列方向 */ flex-direction: row; /* 设置容器内部元素如何换行 */ flex-wrap: wrap; /* 定义多根轴线的对齐方式(只有一行不起作用) */ align-content: flex-start | flex-end | center | space-between | space-around | stretch; } .box{ width: 120px; height: 120px; } .red{ background-color: red; font-size: 25px; } .blue{ background-color: blue; font-size: 30px; } .green{ background-color: green; font-size: 45px; } .yellow{ background-color: yellow; } .orange{ background-color: orange; } .pink{ background-color: pink; } </style> </head> <body> <div class="container"> <div class="box red">A</div> <div class="box blue">贰</div> <div class="box green">3</div> <div class="box yellow">4</div> <div class="box orange">5</div> <div class="box pink">6</div> </div> </body></html>
order
flex-grow
flex-shrink
flex-basis
flex
align-self
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0
.item { order: <integer>;}
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; /* 设置容器内部元素排列方向 */ flex-direction: row; /* 设置容器内部元素如何换行 */ flex-wrap: wrap; } .box{ width: 120px; height: 120px; } .red{ background-color: red; /* order数值越小项目越靠前 */ order: 2; } .blue{ background-color: blue; order: 0; } .green{ background-color: green; order: 1; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> </div> </body></html>
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大
.item { flex-grow: <number>; /* default 0 */}
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; /* 设置容器内部元素排列方向 */ flex-direction: row; /* 设置容器内部元素如何换行 */ flex-wrap: wrap; } .box{ width: 120px; height: 120px; font-size: 50px; } .red{ /* flex-grow:定义项目的放大比例,默认为0 */ background-color: red; flex-grow: 1; /* 红色将剩余空间占据 */ } .blue{ background-color: blue; } .green{ background-color: green; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> </div> </body></html>
总结:
flex-shrink属性定义项目的缩小比例,默认值为1,即如果空间不足,则当前项目缩小
.item { flex-shrink: <number>; /* default 1 */}
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; } .box{ width: 230px; height: 120px; font-size: 50px; } .red{ background-color: red; } .blue{ background-color: blue; /* 定义项目的缩小比例,值为0则不缩小 */ flex-shrink: 0; } .green{ background-color: green; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> </div> </body></html>
蓝色div设置flex-shrink属性为0,其他div默认为1,空间不足时该蓝色div不会被压缩
flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小
auto
设置元素固定或自动空间的占比!
.item { flex-basis: <length> | auto; /* default auto */}
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; } .box{ width: 120px; height: 120px; font-size: 50px; } .red{ background-color: red; /* 设置元素固定空间占比 */ flex-basis: 15rem; } .blue{ background-color: blue; } .green{ background-color: green; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> </div> </body></html>
设置红色div剩余空间占比为15rem,也可以设置为px,这样就可以不用占据所有剩余空间
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选
0 1 auto
.item { flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]}
一般放大比例直接设置:
.item { flex: 1}
该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
1 1 auto
0 0 auto
建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
align-self属性允许 单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。
默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
.item { align-self: auto | flex-start | flex-end | center | baseline | stretch;}
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> .container{ border: 1px solid #000000; width: 500px; height: 600px; margin: 0 auto; /* 定义flex容器 */ display: flex; } .box{ width: 120px; height: 120px; font-size: 50px; } .red{ background-color: red; align-self: flex-end; } .blue{ background-color: blue; } .green{ background-color: green; } </style> </head> <body> <div class="container"> <div class="box red">1</div> <div class="box blue">2</div> <div class="box green">3</div> </div> </body></html>
设置红色div的align-self属性为flex-end,则它将在交叉轴的终点对齐
该属性可能取6个值,除了auto,其他都与align-items属性完全一致
参考文章:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool
本文来自博客园,作者:不知名前端李小白,转载请注明原文链接:https://www.cnblogs.com/libo-web/p/15705585.html
原文链接:http://www.cnblogs.com/libo-web/p/15705585.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728