🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# flex 多行容器交叉轴项目对齐 ## 1. `align-content`属性 - 该属性仅适用于: 多行容器 - 多行容器中, 交叉轴会有多个项目, 剩余空间在项目之间分配才有意义 | 序号 | 属性值 | 描述 | | ---- | --------------- | ------------------------------------------------ | | 1 | `stretch`默认 | 项目拉伸占据整个交叉轴 | | 1 | `flex-start` | 所有项目与交叉轴起始线(顶部)对齐 | | 2 | `flex-end` | 所有项目与交叉轴终止线对齐 | | 3 | `center` | 所有项目与交叉轴中间线对齐: 居中对齐 | | 4 | `space-between` | 两端对齐: 剩余空间在头尾项目之外的项目间平均分配 | | 5 | `space-around` | 分散对齐: 剩余空间在每个项目二侧平均分配 | | 6 | `space-evenly` | 平均对齐: 剩余空间在每个项目之间平均分配 | > 提示: 多行容器中通过设置`flex-wrap: wrap | wrap-reverse`实现 --- ## 2. 示例 ![](https://img.kancloud.cn/69/84/6984039e84c9abaaf1f049531a2425e8_410x245.jpg) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>flex 多行容器交叉轴项目对齐</title> <style> /* 容器尺寸 */ .container { width: 300px; height: 150px; } /* flex容器 */ .container { display: flex; } /* 多行容器 */ .container { flex-wrap: wrap; } /* flex 多行容器交叉轴项目对齐*/ /* 当多行容器中交叉轴方向上存在剩余空间时, 该属性才有意义 */ /* align-content属性仅适用于: 多行容器 */ .container { /* 默认: 项目拉伸占据整个交叉轴 */ /* 你看到项目在交叉轴下部有空白,是因为当前项目设置了固定高度 */ align-content: stretch; /* 所有项目与交叉轴起始线(顶部)对齐 */ align-content: flex-start; /* 所有项目与交叉轴终止线对齐 */ align-content: flex-end; /* 所有项目与交叉轴中间线对齐: 居中对齐 */ align-content: center; /* 多行容器中, 交叉轴上可能有多个项目, 剩余空间的分配才有意义*/ /* 两端对齐 */ align-content: space-between; /* 分散对齐 */ align-content: space-around; /* 平均对齐 */ align-content: space-evenly; } /* flex项目 */ .item { width: 50px; height: 50px; background-color: lightcyan; font-size: 1.5rem; } </style> </head> <body> <!-- 填充更多项目使之产生换行 --> <div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> <div class="item">7</div> <div class="item">8</div> <div class="item">9</div> </div> </body> </html> ```