父元素开启display: flex;子元素的float、clear和vertical-align属性将失效,变成行内元素
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
容器的属性flex-direction,属性决定主轴的方向(即项目的排列方向)
row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上沿。
column-reverse:主轴为垂直方向,起点在下沿。
下面示例:row-reverse:主轴为水平方向,起点在右端。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;flex-direction:row-reverse;height: 300px;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:column:主轴为垂直方向,起点在上沿。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;flex-direction:column;height: 300px;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:column-reverse:主轴为垂直方向,起点在下沿。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;flex-direction:column-reverse;height: 300px;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:column-reverse:主轴为垂直方向,起点在下沿。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;flex-direction:column-reverse;height: 300px;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
justify-content属性定义了项目在主轴上的对齐方式。
flex-start(默认值):左对齐
flex-end:右对齐
center: 居中
space-between:两端对齐,项目之间的间隔都相等。
space-evenly:平均分配
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
下面示例:flex-end:右对齐
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;justify-content:flex-end;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:center: 居中
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;justify-content:center;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:space-between:两端对齐,项目之间的间隔都相等。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;justify-content:space-between;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:space-evenly:平均分配
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;justify-content:space-evenly;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;justify-content:space-around;}
.c1 > div{height: 100px; width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
align-items属性定义项目在交叉轴上如何对齐。
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
flex-start:交叉轴的起点对齐。
flex-end:交叉轴的终点对齐。
center:交叉轴的中点对齐。
baseline: 项目的第一行文字的基线对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:flex-start:交叉轴的起点对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;align-items:flex-start;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;height: 80px;line-height: 80px;}
.c3{background: #666;height:160px;line-height: 160px;}
.c4{background: #aaa;}
1
2
3
下面示例:flex-end:交叉轴的终点对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;align-items:flex-end;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;height: 80px;line-height: 80px;}
.c3{background: #666;height:160px;line-height: 160px;}
.c4{background: #aaa;}
1
2
3
下面示例:center:交叉轴的中点对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;align-items:center;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;height: 80px;line-height: 80px;}
.c3{background: #666;height:160px;line-height: 160px;}
.c4{background: #aaa;}
1
2
3
下面示例1:baseline: 项目的第一行文字的基线对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;align-items:baseline;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;height: 200px;line-height:200px;}
.c3{background: #666;height:160px;}
.c4{background: #aaa;}
1
2
3
下面示例2:baseline: 项目的第一行文字的基线(文字的底部)对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;align-items:baseline;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;height: 200px;line-height:200px;font-size: 150px;}
.c3{background: #666;height:160px;}
.c4{background: #aaa;}
1
2
3
flex-wrap属性 默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。。
nowrap(默认):不换行。所有的项目都会在同一行显示
wrap:换行,第一行在上方
wrap-reverse:换行,第一行在下方。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:wrap:换行,第一行在上方
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-wrap:wrap;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:wrap-reverse:换行,第一行在下方。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-wrap:wrap-reverse;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap,意思是左对齐,不换行
下面示例:flex-flow:row-reverse wrap; 右对齐 换行
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-flow:row-reverse wrap;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
align-content 属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
stretch(默认值):轴线占满整个交叉轴
flex-start:与交叉轴的起点对齐。
flex-end:与交叉轴的终点对齐。
center:与交叉轴的中点对齐。
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
下面示例:flex-start:与交叉轴的起点对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-wrap:wrap;align-content:flex-start;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:flex-end:与交叉轴的终点对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-wrap:wrap;align-content:flex-end;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:center:与交叉轴的中点对齐。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-wrap:wrap;align-content:center;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-wrap:wrap;align-content:space-between;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
下面示例:space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;flex-wrap:wrap;align-content:space-around;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;order:2;}
.c3{background: #666;order:1;}
.c4{background: #aaa;order:2;}
1
2
3
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的
align-items属性,如果没有父元素,则等同于stretch。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;align-self:flex-end;}
.c4{background: #aaa;}
1
2
3
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;flex-grow:1;}
.c4{background: #aaa;}
1
2
3
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;flex-grow:1;}
.c3{background: #666;flex-grow:1;}
.c4{background: #aaa;flex-grow:1;}
1
2
3
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;flex-grow:2;}
.c3{background: #666;flex-grow:2;}
.c4{background: #aaa;flex-grow:1;}
1
2
3
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;}
.c3{background: #666;flex-shrink:3;}
.c4{background: #aaa;flex-shrink:2;}
1
2
3
flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
优先级大于width
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;flex-basis:200px;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
.c1{width:100%;margin: 10px auto 20px;display: flex;background: #f1f1f1;height: 300px;}
.c1 > div{width: 100px;text-align: center;line-height: 100px;font-size: 20px;color: #fff;}
.c2{background: #333;flex: 1 1 auto;}
.c3{background: #666;}
.c4{background: #aaa;}
1
2
3