Flexbox 布局完全指南
Flexbox 是一种强大的 CSS 布局方式,专门用于处理一维布局问题,能够轻松实现居中、分布对齐等常见布局需求。
什么是 Flexbox?
Flexbox(Flexible Box)是 CSS3 中的一种布局模型,它提供了一个更有效的方式来排列、分布和对齐容器中的项目,即使它们的大小未知或动态变化。
基本概念
容器和项目
.container { display: flex; display: inline-flex; }
.container > div { }
|
主轴和交叉轴
- 主轴(Main Axis):Flex 容器的主方向
- 交叉轴(Cross Axis):垂直于主轴的方向
容器属性
display
.flex-container { display: flex; display: inline-flex; }
|
flex-direction
.row { flex-direction: row; flex-direction: row-reverse; flex-direction: column; flex-direction: column-reverse; }
|
flex-wrap
.wrap { flex-wrap: nowrap; flex-wrap: wrap; flex-wrap: wrap-reverse; }
.flex-flow: row wrap;
|
justify-content(主轴对齐)
.container { justify-content: flex-start; justify-content: flex-end; justify-content: center; justify-content: space-between; justify-content: space-around; justify-content: space-evenly; }
|
align-items(交叉轴对齐)
.container { align-items: stretch; align-items: flex-start; align-items: flex-end; align-items: center; align-items: baseline; }
|
align-content(多行对齐)
.container { align-content: flex-start; align-content: flex-end; align-content: center; align-content: space-between; align-content: space-around; align-content: stretch; }
|
项目属性
order
.item1 { order: 2; } .item2 { order: 1; } .item3 { order: 3; }
|
flex-grow
.item1 { flex-grow: 0; } .item2 { flex-grow: 1; } .item3 { flex-grow: 2; }
|
flex-shrink
.item1 { flex-shrink: 1; } .item2 { flex-shrink: 0; } .item3 { flex-shrink: 2; }
|
flex-basis
.item { flex-basis: auto; flex-basis: 100px; flex-basis: 20%; flex-basis: 0; }
|
flex(简写)
.item { flex: none; flex: auto; flex: initial; flex: 1; flex: 0 0 100px; flex: 2 2 200px; }
|
align-self
.item1 { align-self: auto; .item2 { align-self: flex-start; } .item3 { align-self: flex-end; } .item4 { align-self: center; } .item5 { align-self: baseline; } .item6 { align-self: stretch; }
|
实用布局示例
垂直居中
.center { display: flex; justify-content: center; align-items: center; height: 100vh; }
|
三栏布局
.sidebar { flex: 0 0 200px; }
.main { flex: 1; }
.sidebar-right { flex: 0 0 200px; }
.container { display: flex; min-height: 100vh; }
|
卡片布局
.card-container { display: flex; flex-wrap: wrap; gap: 20px; }
.card { flex: 1 1 300px; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
导航栏
.nav { display: flex; justify-content: space-between; align-items: center; padding: 1rem; background: #333; color: white; }
.nav-brand { font-size: 1.5rem; font-weight: bold; }
.nav-links { display: flex; gap: 1rem; }
|
粘性页脚
.site { display: flex; flex-direction: column; min-height: 100vh; }
.site-header { flex: 0 0 auto; }
.site-content { flex: 1 0 auto; }
.site-footer { flex: 0 0 auto; }
|
常见问题和解决方案
项目溢出
.container { display: flex; }
.container { flex-wrap: wrap; }
.item { flex-shrink: 1; min-width: 0; }
.item { min-width: 100px; flex-basis: 100px; }
|
垂直居中失效
.container { display: flex; justify-content: center; align-items: center; min-height: 100vh; }
|
文本溢出处理
.item { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
Flexbox 与 Grid 的比较
| 场景 | 推荐使用 | 原因 |
|---|
| 一维布局(行或列) | Flexbox | 专为单轴设计 |
| 二维布局(行和列) | Grid | 支持网格系统 |
| 组件内部布局 | Flexbox | 简单直接 |
| 页面整体布局 | Grid | 更强大和精确 |
| 内容居中对齐 | Flexbox | 更简单 |
| 复杂网格布局 | Grid | 更适合 |
性能考虑
- 避免过度使用:不是所有布局都需要 Flexbox
- 减少嵌套:避免不必要的嵌套 flex 容器
- 合理使用属性:
flex: 1 比 flex-grow: 1 更简洁 - 浏览器兼容性:考虑旧版浏览器的兼容性
[!tip]
- Flexbox 适合组件级别的布局,Grid 适合页面级别的布局
- 使用
flex: 1 的简写形式更简洁 - 记得设置
min-width: 0 来处理内容溢出问题
参考资料