display: flex 的元素称为 Flex容器(父容器),容器内直接子元素自动成为 Flex项目(子项)
| 属性名 | 作用说明 | 常用取值 |
|---|---|---|
| display | 开启弹性布局 | flex / inline-flex |
| flex-direction | 设置主轴方向(子项排列方向) | row / row-reverse / column / column-reverse |
| flex-wrap | 子项超出容器时是否换行 | nowrap / wrap / wrap-reverse |
| flex-flow | 复合简写:flex-direction + flex-wrap | row wrap |
| justify-content | 主轴方向对齐方式 | flex-start / flex-end / center / space-between / space-around / space-evenly |
| align-items | 单行子项在交叉轴对齐方式 | stretch / flex-start / flex-end / center / baseline |
| align-content | 多行子项整体在交叉轴对齐(仅换行后生效) | stretch / flex-start / flex-end / center / space-between / space-around |
| gap / row-gap / column-gap | 设置子项之间间距(行列间隙) | 20px / 1rem |
display: flex:块级弹性容器,独占一行display: inline-flex:行内弹性容器,与同行元素并排语法:flex-flow: 方向 换行;
多行内容整体在交叉轴分配空白,取值同justify-content
| 属性名 | 作用说明 | 取值规则 |
|---|---|---|
| flex-grow | 剩余空间分配扩张比例 | 数字,默认0(不扩张) |
| flex-shrink | 空间不足时压缩比例 | 数字,默认1(允许压缩) |
| flex-basis | 子项基准尺寸(主轴初始大小) | auto / px / % / rem |
| flex | 三合一简写:grow shrink basis | 1 / auto / none |
| align-self | 单独修改当前子项交叉轴对齐,覆盖align-items | auto / stretch / center / flex-start / flex-end |
| order | 控制子项排列顺序,数字越小越靠前 | 整数,默认0 |
容器有剩余空白时,按所有子项grow总和分配空间;0=不占多余空间
容器宽度不够时,按shrink比例压缩;0=禁止压缩
主轴方向初始尺寸,优先级高于width/height;auto默认取元素自身宽高
flex: 1 → flex:1 1 0%; 均分剩余空间flex: none → flex:0 0 auto; 固定尺寸,不扩不缩flex: auto → flex:1 1 auto; 自适应扩张压缩单个子项自定义交叉轴位置,优先级高于父容器align-items
order:-1 排最前;order:10 排最后;同数字保持原顺序
display:flex; justify-content:center; align-items:center;flex:1;flex:none;flex-flow:column;