CSS3 弹性盒子布局(Flexbox)是一种强大的布局模式,它提供了一种更加高效的方式来布置、对齐和分配容器中项目之间的空间,即使它们的大小未知或动态变化。Flexbox 是现代 Web 开发中最常用的布局技术之一。
Flexbox 由两个主要组件组成:flex 容器和 flex 项目。
/* 创建 flex 容器 */
.container {
display: flex;
}
/* flex 容器中的直接子元素自动成为 flex 项目 */
.container {
display: flex;
}
.item {
/* 自动成为 flex 项目 */
}
flex-direction 属性决定主轴的方向。
/* 主轴为水平方向,从左到右 */
.row {
flex-direction: row;
}
/* 主轴为水平方向,从右到左 */
.row-reverse {
flex-direction: row-reverse;
}
/* 主轴为垂直方向,从上到下 */
.column {
flex-direction: column;
}
/* 主轴为垂直方向,从下到上 */
.column-reverse {
flex-direction: column-reverse;
}
flex-wrap 属性决定 flex 项目是否换行。
/* 不换行 */
.nowrap {
flex-wrap: nowrap;
}
/* 换行 */
.wrap {
flex-wrap: wrap;
}
/* 反向换行 */
.wrap-reverse {
flex-wrap: wrap-reverse;
}
flex-flow 属性是 flex-direction 和 flex-wrap 的简写属性。
/* 简写形式 */
.container {
flex-flow: row wrap;
}
/* 等价于 */
.container {
flex-direction: row;
flex-wrap: wrap;
}
justify-content 属性定义了项目在主轴上的对齐方式。
/* 左对齐 */
.flex-start {
justify-content: flex-start;
}
/* 右对齐 */
.flex-end {
justify-content: flex-end;
}
/* 居中 */
.center {
justify-content: center;
}
/* 两端对齐,项目之间的间隔相等 */
.space-between {
justify-content: space-between;
}
/* 每个项目两侧的间隔相等 */
.space-around {
justify-content: space-around;
}
/* 每个项目之间的间隔相等 */
.space-evenly {
justify-content: space-evenly;
}
align-items 属性定义项目在交叉轴上的对齐方式。
/* 交叉轴的起点对齐 */
.stretch {
align-items: stretch;
}
/* 交叉轴的终点对齐 */
.flex-end {
align-items: flex-end;
}
/* 交叉轴的中点对齐 */
.center {
align-items: center;
}
/* 项目的第一行文字的基线对齐 */
.baseline {
align-items: baseline;
}
/* 默认值,拉伸以填满容器 */
.stretch {
align-items: stretch;
}
align-content 属性定义了多根轴线的对齐方式。
/* 与交叉轴的起点对齐 */
.flex-start {
align-content: flex-start;
}
/* 与交叉轴的终点对齐 */
.flex-end {
align-content: flex-end;
}
/* 与交叉轴的中点对齐 */
.center {
align-content: center;
}
/* 轴线之间的间隔相等 */
.space-between {
align-content: space-between;
}
/* 每根轴线两侧的间隔相等 */
.space-around {
align-content: space-around;
}
/* 每根轴线之间的间隔相等 */
.space-evenly {
align-content: space-evenly;
}
/* 默认值,轴线占满整个交叉轴 */
.stretch {
align-content: stretch;
}
order 属性定义项目的排列顺序。
/* 数值越小,排列越靠前 */
.item-1 {
order: 1;
}
.item-2 {
order: 2;
}
.item-3 {
order: 3;
}
flex-grow 属性定义项目的放大比例。
/* 默认值为 0,即如果存在剩余空间,也不放大 */
.grow-0 {
flex-grow: 0;
}
/* 所有项目的放大比例都为 1,它们将等分剩余空间 */
.grow-1 {
flex-grow: 1;
}
/* 放大比例为 2,其他项目为 1,该项目将占据更多空间 */
.grow-2 {
flex-grow: 2;
}
flex-shrink 属性定义项目的缩小比例。
/* 默认值为 1,即如果空间不足,该项目将缩小 */
.shrink-1 {
flex-shrink: 1;
}
/* 如果所有项目的缩小比例都为 1,当空间不足时,都将等比例缩小 */
.shrink-all {
flex-shrink: 1;
}
/* 如果一个项目的缩小比例为 0,其他项目都为 1,则空间不足时,前者不缩小 */
.no-shrink {
flex-shrink: 0;
}
flex-basis 属性定义了在分配多余空间之前,项目占据的主轴空间。
/* 默认值为 auto,即项目本来的大小 */
.auto {
flex-basis: auto;
}
/* 固定宽度 */
.fixed {
flex-basis: 200px;
}
/* 百分比宽度 */
.percentage {
flex-basis: 50%;
}
flex 属性是 flex-grow、flex-shrink 和 flex-basis 的简写。
/* 默认值,等同于 flex: 0 1 auto */
.default {
flex: auto;
}
/* 等同于 flex: 1 1 auto */
.flex-one {
flex: 1;
}
/* 等同于 flex: 0 0 auto */
.none {
flex: none;
}
/* 自定义值 */
.custom {
flex: 1 1 200px;
}
align-self 属性允许单个项目有与其他项目不一样的对齐方式。
/* 默认值,继承父元素的 align-items 属性 */
.auto {
align-self: auto;
}
/* 交叉轴的起点对齐 */
.flex-start {
align-self: flex-start;
}
/* 交叉轴的终点对齐 */
.flex-end {
align-self: flex-end;
}
/* 交叉轴的中点对齐 */
.center {
align-self: center;
}
/* 基线对齐 */
.baseline {
align-self: baseline;
}
/* 拉伸以填满容器 */
.stretch {
align-self: stretch;
}
/* 水平居中 */
.horizontal-center {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* 垂直居中 */
.vertical-center {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
/* 三栏布局 */
.three-column {
display: flex;
gap: 20px;
}
.sidebar-left {
flex: 0 0 200px;
}
.content {
flex: 1;
}
.sidebar-right {
flex: 0 0 200px;
}
/* 响应式网格 */
.grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.grid-item {
flex: 1 1 300px;
min-width: 0;
}
/* 导航栏 */
.nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 30px;
background: white;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.nav-links {
display: flex;
gap: 20px;
}
/* 卡片布局 */
.card-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px;
padding: 20px;
background: white;
border-radius: 12px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
/* 表单布局 */
.form-row {
display: flex;
gap: 15px;
margin-bottom: 15px;
}
.form-group {
flex: 1;
}
.form-control {
width: 100%;
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 6px;
}
/* 媒体对象 */
.media {
display: flex;
gap: 15px;
}
.media-image {
flex: 0 0 auto;
}
.media-content {
flex: 1;
}
/* 嵌套 Flexbox */
.outer {
display: flex;
gap: 20px;
}
.inner {
display: flex;
flex-direction: column;
gap: 10px;
}
/* 响应式 Flexbox */
.container {
display: flex;
flex-direction: column;
gap: 20px;
}
@media (min-width: 768px) {
.container {
flex-direction: row;
}
}
/* Flexbox 与 Grid 结合 */
.layout {
display: grid;
grid-template-columns: 1fr;
gap: 20px;
}
@media (min-width: 768px) {
.layout {
grid-template-columns: 250px 1fr;
}
}
.content {
display: flex;
flex-direction: column;
gap: 20px;
}
/* Flexbox 动画 */
.animated {
display: flex;
transition: all 0.3s ease;
}
.animated:hover {
transform: scale(1.05);
}
CSS3 Flexbox 在所有现代浏览器中都有良好的支持:
display: flex:Chrome 29+, Firefox 28+, Safari 9+, Edge 12+, IE 11+-webkit-、-moz-、-ms-(旧版本)/* 推荐 - 合理使用 Flexbox */
.container {
display: flex;
justify-content: center;
align-items: center;
}
/* 不推荐 - 过度使用 Flexbox */
.div {
display: flex;
}
/* 推荐 - 提供降级方案 */
.container {
display: flex;
display: -webkit-flex;
display: -ms-flexbox;
}
/* 不推荐 - 不提供降级方案 */
.container {
display: flex;
}
/* 推荐 - 考虑性能 */
.optimized {
display: flex;
will-change: transform;
}
/* 不推荐 - 影响性能 */
.non-optimized {
display: flex;
filter: blur(10px);
}
/* 推荐 - 保持一致性 */
.flex-container {
display: flex;
gap: 20px;
}
/* 不推荐 - 不一致 */
.container-1 {
display: flex;
margin: -10px;
}
.container-2 {
display: flex;
gap: 20px;
}
CSS3 弹性盒子布局提供了强大的布局能力:
容器属性:
flex-direction:主轴方向flex-wrap:换行控制flex-flow:简写属性justify-content:主轴对齐align-items:交叉轴对齐align-content:多轴线对齐项目属性:
order:排列顺序flex-grow:放大比例flex-shrink:缩小比例flex-basis:基础大小flex:简写属性align-self:单独对齐实际应用:
最佳实践:
优势:
注意事项:
掌握 CSS3 Flexbox,能够让我们创建更加灵活和响应式的布局,大大提升网页的布局质量和开发效率。