一共有6种flex属性用于调节flex布局
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content
flex-direction
flex-direction属性决定主轴方向,默认横向row
.box {
flex-direction : row | row-reverse | column | column-reverse;
}
row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上端。
column-reverse:主轴为垂直方向,起点在下端。
row
![](https://img-blog.csdnimg.cn/img_convert/d9b8979010a73d21ff1691820a3db80f.png)
row-reverse
![](https://img-blog.csdnimg.cn/img_convert/34b73740c187aaa5e1a13e2a836e82ac.png)
column
![](https://img-blog.csdnimg.cn/img_convert/cb4aab31adc70b60d52bfb3103d19e5d.png)
column-reverse
![](https://img-blog.csdnimg.cn/img_convert/974cd49ea74bf417e8c94bd6b79c1975.png)
flex-wrap
flex-wrap属性指定 flex 元素单行显示还是多行显示。若允许换行,这个属性允许你控制元素的堆叠方向。
.box {
flex-wrap: nowrap | wrap | wrap-reverse;
}
nowrap(默认值):flex 的元素被摆放到到一行,不换行
wrap:元素不够宽度排列后可以换行排列,第一行在上方
wrap-reverse:元素不够宽度排列后可以换行排列,第一行在下方
nowrap
![](https://img-blog.csdnimg.cn/img_convert/7942c86c78a166e5b8a0185ae3f1fb90.png)
原宽度和高度一样的元素在容器内排列不下,宽度被压缩
wrap
![](https://img-blog.csdnimg.cn/img_convert/723f85afed57f3acaba717172698e1d7.png)
第一行在上方
wrap-reverse
![](https://img-blog.csdnimg.cn/img_convert/156de1b5519f94e27def8c3bd217853a.png)
第一行在下方
flex-flow
.box {
flex-flow: <flex-direction> || <flex-wrap>;
}
row nowrap (默认值)
row nowrap
![](https://img-blog.csdnimg.cn/img_convert/cc8f9007117795e87f8a48bfd0f82409.png)
row wrap
![](https://img-blog.csdnimg.cn/img_convert/3472c3e2a3b0dd54e880f1ef99c43b83.png)
column nowrap
![](https://img-blog.csdnimg.cn/img_convert/8da0d2ef8c167ed66f109b288bc6ddd6.png)
column warp
![](https://img-blog.csdnimg.cn/img_convert/ca8450e2971110638f85ae58a514a26b.png)
justify-content
justify-content定义了元素在弹性容器主轴上的对齐方式
.box {
justify-content : flex-start | flex-end | center | 0
}
flex-start(默认值):主轴起点对齐
flex-end:主轴终点对齐
center:主轴居中
space-between:两端对齐(首元素在start位置,末元素在end位置),元素均匀排列,间隔均分
space-around:每个元素周围分布相同的空间,项目之间的间隔比项目与容器的间隔大一倍
space-evenly:每个元素均匀排列,周围的间隙都相等(兼容性不太好)
flex-start
![](https://img-blog.csdnimg.cn/img_convert/ade7157f8763e84232fe22b901f2ecb6.png)
主轴为row时↑
![](https://img-blog.csdnimg.cn/img_convert/9cb9d312ee4e19a348d12d1260697e8d.png)
主轴为column时↑
flex-end
![](https://img-blog.csdnimg.cn/img_convert/1426daf81c023141685cffb24bdac794.png)
主轴为row时↑
![](https://img-blog.csdnimg.cn/img_convert/bca2e5b01d29066d6220867b5dc13521.png)
主轴为column时
center
![](https://img-blog.csdnimg.cn/img_convert/7de7dc6471ec38d805d3d0ac2b28ec02.png)
主轴为row时
![](https://img-blog.csdnimg.cn/img_convert/e16f839dffa386c43924d0427d5905de.png)
主轴为column时
space-between
![](https://img-blog.csdnimg.cn/img_convert/5b67798b6c813eb064cffdeb3a1b941b.png)
主轴为row时
![](https://img-blog.csdnimg.cn/img_convert/91518c37b17a11ffa51bc1593852516b.png)
主轴为column时
space-around
![](https://img-blog.csdnimg.cn/img_convert/80faae504ef048eb44a2dca66c22866d.png)
主轴为row时
![](https://img-blog.csdnimg.cn/img_convert/3304d1913a4bb090ee019d587b9d1df7.png)
主轴为column时
space-evenly
![](https://img-blog.csdnimg.cn/img_convert/56a7ec5695e76e7b0c8dde0d46678e95.png)
主轴为row时
![](https://img-blog.csdnimg.cn/img_convert/177dd5b804b76cb290baf32c4eff2f70.png)
主轴为column时
align-items
justify-content定义了元素在弹性容器交叉轴上的对齐方式
.box {
align-items: flex-start | flex-end | center | baseline | stretch
}
flex-start:交叉轴起点对齐
flex-end:交叉轴终点对齐
center: 交叉轴轴居中
stretch(默认值):如果被排列的项目未设置高度(宽度)或设置为auto,项目将占满整个容器的高度(宽度)
baseline:项目的第一行文字的基线对齐
flex-start
![](https://img-blog.csdnimg.cn/img_convert/bb972c713e1e714552c612ea4b33366a.png)
主轴为row时
![](https://img-blog.csdnimg.cn/img_convert/6473c6d97361a6fcec5b7c3fc201666b.png)
主轴为column时
flex-end
![](https://img-blog.csdnimg.cn/img_convert/9ed7166796732de5f256dad625f9b825.png)
主轴为row时
![](https://img-blog.csdnimg.cn/img_convert/1eb15fc8e19306b57e143f0a34a08d11.png)
主轴为column时
center
![](https://img-blog.csdnimg.cn/img_convert/ac3dba4ccd2c79a63d1e32eef428c0f0.png)
主轴为row时
![](https://img-blog.csdnimg.cn/img_convert/660cfb6eb336d0ea728c0501a2a888ca.png)
主轴为column时
stretch
![](https://img-blog.csdnimg.cn/img_convert/4f88986ed206130e43611edc332da2a3.png)
主轴为row时,未设置height或者设置height为aotu的项目会被拉升至容器边框,此处2和3项目height为auto
![](https://img-blog.csdnimg.cn/img_convert/920e82c8059411b9c1649fed4d2bb8b3.png)
主轴为column时,未设置width或者设置width为aotu的项目会被拉升至容器边框,此处1项目width为auto
baseline
![](https://img-blog.csdnimg.cn/img_convert/bc528d2d00f3715865ab3f03dfd3fd9b.png)
没对齐前
![](https://img-blog.csdnimg.cn/img_convert/f2d1cdefb097f9a4022782815398646f.png)
对齐后
align-content
align-content 定义了多跟轴线的对齐方式,如果只有一根轴线则属性不起作用
.box {
align-content:flex-start | flex-end | center | stretch | space-between | space-around
}
flex-start:交叉轴起点对齐
flex-end:交叉轴终点对齐
center: 交叉轴轴居中
stretch(默认值):如果被排列的项目未设置高度(宽度)或设置为auto,项目将占满整个容器的高度(宽度)
space-between:两端对齐
space-around:均匀分布项目
flex-start
![](https://img-blog.csdnimg.cn/img_convert/7c696419628e087de93d58d0ae6f1c02.png)
flex-end
![](https://img-blog.csdnimg.cn/img_convert/17b5e5b0decd538000780bdd6144f92a.png)
center
![](https://img-blog.csdnimg.cn/img_convert/c4f795f57160372bec8f6c89e4016421.png)
stretch
![](https://img-blog.csdnimg.cn/img_convert/dea64912403d096fcacf18095e3731f4.png)
height设置为auto的4,8,12号盒子拉伸
space-between
![](https://img-blog.csdnimg.cn/img_convert/381ce2b04dc296b17cfde0ff73298996.png)
space-around
![](https://img-blog.csdnimg.cn/img_convert/6ec242fce3f63d09be4d1acb24051b0a.png)