一、Flex布局是什么?
Flex 是 Flexible Box 的缩写,也就是"弹性布局"。
二、容器的属性
有6个,如下:
- flex-direction
- flex-wrap
- flex-flow
- justify-content
- align-items
- align-content
1、flex-direction属性:决定主轴的方向,有4个值
.box {
flex-direction: row | row-reverse | column | column-reverse;
}
row
(默认值):主轴为水平方向,起点在左端。row-reverse
:主轴为水平方向,起点在右端。column
:主轴为垂直方向,起点在上沿。column-reverse
:主轴为垂直方向,起点在下沿。
2、flex-wrap属性:如果一行排列不下,可换行,有3个值
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}
nowrap
(默认):不换行wrap
:换行,第一行在上方wrap-reverse
:换行,第一行在下方
3、flex-flow属性
flex-flow
属性是flex-direction
属性和flex-wrap
属性的简写形式,默认值为row nowrap
.box {
flex-flow: <flex-direction> || <flex-wrap>;
}
4、justify-content
属性
flex-flow
属性定义了项目在主轴上的对齐方式,有5个值
.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
}
flex-start
(默认值):左对齐flex-end
:右对齐center
: 居中space-between
:两端对齐,项目之间的间隔都相等。space-around
:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
具体样式如下:
5、align-items属性
align-items
属性定义项目在交叉轴上的对齐方式,有5个值。
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}
flex-start
:交叉轴的起点对齐。flex-end
:交叉轴的终点对齐。center
:交叉轴的中点对齐。baseline
: 项目的第一行文字的基线对齐。stretch
(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
6、align-content属性
align-content
属性定义了多根轴线的对齐方式,有6个值
flex-start
:与交叉轴的起点对齐。flex-end
:与交叉轴的终点对齐。center
:与交叉轴的中点对齐。space-between
:与交叉轴两端对齐,轴线之间的间隔平均分布。space-around
:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。stretch
(默认值):轴线占满整个交叉轴。