1.容器的属性
以下6个属性设置在容器上
flex-direction 项目的排列方向
flex-wrap 换行
flex-flow <排列方向> + <换行>
justify-content 项目在主轴上(横向)的对齐方式
align-items 项目在交叉轴(垂直方向)上如何对齐
align-content 对齐内容
1.1 flex-direction 方向
决定主轴的方向(即项目的排列方向)。
四个值
1.flex-direction: row(默认); 水平方向从左往右排列 起点在左端
2.flex-direction: row-reverse; 水平方向从右往左排列 起点在右端
3.flex-direction: column;垂直从上到下排列 起点在下沿
4.flex-direction: column-reverse;垂直方向从下到上排列 起点在下沿
1.row
2.row-reverse
3.column
4.column-reverse
1.2 flex-wrap 换行
默认情况下,项目都排在一条线(又称"横向线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行
三个值
1.flex-wrap:nowrap 不换行(默认)
2.flex-wrap:wrap 换行 第一行在上方
3.flex-wrap:wrap-reverse 第一行在下方
1.nowrap
2.wrap
3.wrap-reverse
1.3 flex-flow 流动
flex-flow 是 flex-direction 方向 + flex-wrap 换行 的简写方式
.box {
flex-flow: <flex-direction> || <flex-wrap>;
}
flex-flow: row nowrap;(默认值 )
1.4 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 平均分布
1.flex-start
2.flex-end
3.center
4.space-between
5.space-around
6.space-evenly
1.5 align-items 垂直对齐项目
定义了项目在主轴上的垂直对齐方式
align-items: flex-start; 交叉轴的起点对齐
align-items: flex-end;交叉轴的终点对齐
align-items: center;交叉轴的中点对齐
align-items: baseline;项目的第一行文字的基线对齐。
align-items: stretch;(默认值)如果项目没有设置高度 将和日期对齐
1.flex-start
2.flex-end
3.center
4.baseline
5.stretch
未设置高度或者高度为auto将屏幕占满
1.6 align-content 属性定义了多根横线的对齐方式。如果项目只有一根横线,该属性不起作用。(也就是需要有多行才能体现行于行之间的距离)
align-content:flex-start:顶部对齐
align-content:flex-end:底部对齐
align-content:center:垂直中间对齐
align-content:space-between:上下贴壁,中间均匀分布
align-content:space-around:每个元素之间的距离固定,所以元素相交距离为2倍,类似元素为“回”字
align-content:space-evenly:均匀分布,元素及border距离相等
align-content:stretch(默认值):轴线占满整个交叉轴。
1.flex-start
2.flex-end
3.center
4.space-between
5.space-around
6.space-evenly
7.stretch(默认值)
2.项目的属性
以下6个属性设置在项目上。
order 顺序
flex-grow 放大比例
flex-shrink 缩小比例
flex-basis 主要成分
flex 弯曲
align-self 对齐自身
2.1 order 顺序
.item {
order: <integer>;
}
属性定义的数字越小越靠前 ,默认为0,可设置负数
2.2 flex-grow 放大比例
.item {
flex-grow: <number>; /* default 0 */
}
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
2.3 flex-shrink 缩小比例
.item {
flex-shrink: <number>; /* default 1 */
}
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
值越大缩小的越厉害
如果设置了flex-shrink就不会溢出
2.4 flex-basis属性 级别比width更高
.item {
flex-basis: <length> | auto; /* default auto */
}
2.5 flex属性
放大默认0 缩小默认1 宽度
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}
该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
2.6 align-self属性 自身特别的对齐方式
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
该属性可能取6个值,除了auto,其他都与align-items属性完全一致。
3.以下是 flex 布局的一些应用
<!-- HTML -->
<div className='father'>
<div className='son'>css</div>
<div className='son'>flex</div>
<div className='son'>布局</div>
<div className='son'>dome</div>
<div className='son'>梁梁</div>
</div>
1.水平居中排列
/* CSS */
.box{
display: flex; *
justify-content: center; *
align-items: center; *
}
//或
.box{
display: flex;// 或inline-flex
justify-content: center;
.son{
align-self: center;
}
}
//或
.box{
display: flex;
place-content: center;
.son{
align-self: center;
}
}
//或
.box{
display: flex;
place-content: center;
place-items: center;
}
2.水平居中垂直排列 多个元素
.box{
display: flex; *
justify-content: center; *
align-items: center; *
flex-direction: column;* 跟水平居中排列代码相同 只需要改变方向
}
属性等价关系
.main{
place-content: center;
place-items: center;
}
//等价于
.main{
align-content: center;
justify-content: center;
align-items: center;
justify-items: center;
}
//多行 垂直水平居中
.main{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
如果只有一个Flex项目时 可以设置margin:auto中水平垂直居中
.father{
display: flex;// 或 inline-flex
.son{
margin: auto;
}
}