css的flex主要由以下两个标签组成:
- container(容器)
- items(项目)
container样式
-
display:flex或display:inline-flex,表明使用flex布局。
.container{
display: flex;
}
flex与inline-flex的区别和block与inline-block的区别类似:占据一整行还是一行可以由多个。
-
流动方向(flex-direction)
流动方向表明item的布局
- flex-direction:row 从左往右横向排列(默认)
- flex-direction:row-reverse 从左往右横向排列
- flex-direction:column 从上到下排列;
- flex-direction:column-reverse 从下到上排列
-
换行(flex-wrap)
- flex-wrap:nowrap 不换行,默认,具有弹性
- flex-wrap:wrap 换行
- flex-wrap:wrap-reverse 从下往上换行
-
主轴对称方式(justify-content)
- justify-content:flex-start 左对齐
- justify-content:flex-end 右对齐
- justify-content:center 居中对齐
- justify-content:space-between 左右靠边,中间间隙一样
- justify-content:space-around 左右间隙相等
- justify-content:space-evenly 所有间隙相等(包括最左和最右)
-
次轴对称方式(align-items)
- align-items:flex-start 上对齐
- align-items:flex-end 下对齐
- align-items:center 中间对齐
- align-items:streth 拉长,等高(默认)
-
多行分布(align-content)
- align-content:flex-start 从上到下
- align-content:flex-end 从下到上
- align-content:center 居中
- align-content:stretch 填满高度
- align-content:space-between 上下靠边居中
- align-content:space-around 两边间距相等对齐
item样式
-
排序(order)
默认为0,从小到大排列
-
空间分配
- flex-grow:1; 平均分空间,按比例分配每个item空间
- flex-shrink:1; 在像素不够时,先把哪个变窄,可写0,防止变窄
-
上下对齐
- align-self:flex-end; 靠下
- align-self:flex-end; 靠上