CSS Flexbox 是现代 Web 开发中的首选布局系统。无论是构建导航栏、居中内容还是创建复杂的响应式布局,Flexbox 都能使其简单直观。本完整 CSS Flexbox 指南涵盖了从基础到高级模式的所有内容。
1. 什么是 Flexbox 以及何时使用
Flexbox(弹性盒布局)是一种一维布局模型,用于在容器内分配空间和对齐项目。与处理二维的 CSS Grid 不同,Flexbox 一次沿一个轴工作。
/* Use Flexbox when you need: */
/* 1. One-dimensional alignment (row OR column) */
.toolbar { display: flex; gap: 8px; }
/* 2. Content-driven sizing */
.tabs { display: flex; }
.tab { flex: 1; } /* equal-width tabs */
/* 3. Dynamic ordering */
.card { display: flex; flex-direction: column; }
.card-footer { order: 3; margin-top: auto; }
/* Use CSS Grid instead when you need: */
/* - Two-dimensional control (rows AND columns) */
/* - Layout-driven sizing (template areas) */
/* - Complex page-level layouts */2. Flex 容器:父元素
一切从 flex 容器开始。在元素上设置 display: flex 会将其直接子元素变为 flex 项目。
/* Block-level flex container */
.container {
display: flex;
}
/* Inline-level flex container */
.inline-container {
display: inline-flex;
}
/* flex-direction: controls the main axis direction */
.row { flex-direction: row; } /* left to right (default) */
.row-rev { flex-direction: row-reverse; } /* right to left */
.col { flex-direction: column; } /* top to bottom */
.col-rev { flex-direction: column-reverse; } /* bottom to top */
/* flex-flow: shorthand for direction + wrap */
.container {
flex-flow: row wrap;
/* same as: flex-direction: row; flex-wrap: wrap; */
}3. 主轴与交叉轴
理解两个轴是掌握 Flexbox 的关键。主轴沿着 flex-direction 方向运行,交叉轴与之垂直。
/*
* flex-direction: row (default)
* ┌──────────────────────────────────────────┐
* │ ← ← ← ← ← ← MAIN AXIS → → → → → → → │
* │ │
* │ ↑ ┌────┐ ┌────┐ ┌────┐ │
* │ │ │ 1 │ │ 2 │ │ 3 │ CROSS │
* │ │ └────┘ └────┘ └────┘ AXIS │
* │ ↓ │
* └──────────────────────────────────────────┘
*
* flex-direction: column
* ┌──────────────────┐
* │ ←CROSS AXIS→ │
* │ │
* │ ↑ ┌────────┐ │
* │ │ │ 1 │ │
* │ │ └────────┘ │
* │ │ ┌────────┐ │ MAIN
* │ │ │ 2 │ │ AXIS
* │ │ └────────┘ │
* │ │ ┌────────┐ │
* │ ↓ │ 3 │ │
* │ └────────┘ │
* └──────────────────┘
*
* KEY INSIGHT:
* - justify-content = main axis
* - align-items = cross axis
* - When flex-direction changes, the axes swap!
*/4. justify-content:沿主轴对齐
justify-content 控制 flex 项目在主轴上的分布方式。
.container {
display: flex;
/* Pack items to the start (default) */
justify-content: flex-start;
/* [1][2][3] */
/* Center items */
justify-content: center;
/* [1][2][3] */
/* Pack items to the end */
justify-content: flex-end;
/* [1][2][3] */
/* Equal space between items */
justify-content: space-between;
/* [1] [2] [3] */
/* Equal space around items */
justify-content: space-around;
/* [1] [2] [3] */
/* Equal space everywhere */
justify-content: space-evenly;
/* [1] [2] [3] */
}5. align-items:沿交叉轴对齐
align-items 控制 flex 项目在交叉轴上的定位方式。
.container {
display: flex;
height: 200px;
align-items: stretch; /* Items fill container height (default) */
align-items: flex-start; /* Items at the top */
align-items: center; /* Items vertically centered */
align-items: flex-end; /* Items at the bottom */
align-items: baseline; /* Items aligned by text baseline */
}
/* Override for a single item */
.special-item {
align-self: center; /* This item centered, others follow container */
}
/* align-content: controls spacing between LINES (only with wrap) */
.multi-line {
display: flex;
flex-wrap: wrap;
align-content: flex-start; /* lines packed to top */
align-content: center; /* lines centered */
align-content: space-between; /* equal space between lines */
}6. Flex 项目属性:grow、shrink、basis
三个 flex 项目属性控制项目如何共享可用空间。理解这些对于响应式布局至关重要。
/* flex-grow: how much an item GROWS when extra space exists */
.item { flex-grow: 0; } /* default: don't grow */
.item { flex-grow: 1; } /* grow to fill available space */
.item { flex-grow: 2; } /* grow twice as much as flex-grow: 1 items */
/* flex-shrink: how much an item SHRINKS when space is tight */
.item { flex-shrink: 1; } /* default: shrink proportionally */
.item { flex-shrink: 0; } /* never shrink (fixed size) */
/* flex-basis: the STARTING SIZE before grow/shrink */
.item { flex-basis: auto; } /* use content width (default) */
.item { flex-basis: 0; } /* start from zero, grow equally */
.item { flex-basis: 200px; } /* start from 200px */
.item { flex-basis: 25%; } /* start from 25% of container *//* flex shorthand (recommended) */
.item { flex: 1; }
/* = flex-grow: 1, flex-shrink: 1, flex-basis: 0% */
/* Items grow equally from zero — equal widths */
.item { flex: auto; }
/* = flex-grow: 1, flex-shrink: 1, flex-basis: auto */
/* Items grow from content size — proportional widths */
.item { flex: none; }
/* = flex-grow: 0, flex-shrink: 0, flex-basis: auto */
/* Items are inflexible — use content width only */
.item { flex: 0 0 200px; }
/* = flex-grow: 0, flex-shrink: 0, flex-basis: 200px */
/* Fixed 200px item that never grows or shrinks */| Shorthand | Equivalent | Use Case |
|---|---|---|
flex: 1 | 1 1 0% | Equal-width columns |
flex: auto | 1 1 auto | Proportional to content |
flex: none | 0 0 auto | Fixed content size |
flex: 0 0 200px | 0 0 200px | Fixed pixel width |
flex: 1 0 300px | 1 0 300px | Responsive card (min 300px) |
7. Flex Wrap 和多行布局
默认情况下,flex 项目被挤在一行。flex-wrap 允许项目在空间不足时换行。
.container {
display: flex;
flex-wrap: nowrap; /* default — single line, may overflow */
flex-wrap: wrap; /* wrap to next line when needed */
flex-wrap: wrap-reverse; /* wrap in reverse order */
}
/* Practical example: responsive card grid */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.card {
flex: 1 1 300px; /* grow, shrink, min-width 300px */
max-width: 400px; /* cap maximum width */
}
/* Result:
* Wide screen: [card] [card] [card] [card]
* Medium: [card] [card] [card]
* [card]
* Narrow: [card]
* [card]
* [card]
* [card]
*/8. gap 属性
gap 属性在 flex 项目之间添加间距,而不在外边缘添加边距。
.container {
display: flex;
flex-wrap: wrap;
gap: 16px; /* equal gap in both directions */
gap: 20px 12px; /* row-gap: 20px, column-gap: 12px */
row-gap: 20px; /* vertical spacing only */
column-gap: 12px; /* horizontal spacing only */
}
/* Why gap is better than margins: */
/* Old approach (margins) - awkward edge spacing */
.item { margin: 8px; }
/* Creates 8px margin on ALL sides, including outer edges */
/* Need negative margins on container to compensate */
/* Modern approach (gap) - clean spacing */
.container { display: flex; gap: 16px; }
/* Space ONLY between items, not on outer edges */9. 常见 Flexbox 布局模式
以下是实际项目中最常用的 Flexbox 模式,均可直接用于生产环境。
Perfect Centering
/* Center anything horizontally and vertically */
.center-everything {
display: flex;
justify-content: center;
align-items: center;
height: 100vh; /* or any fixed height */
}
/* Alternative: margin auto trick */
.parent { display: flex; height: 100vh; }
.child { margin: auto; } /* centers in both axes */Navigation Bar
/* Logo left, links right */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 24px;
height: 64px;
}
/* Logo left, links center, actions right */
.navbar-3-section {
display: flex;
align-items: center;
}
.navbar-3-section .logo { flex: 1; }
.navbar-3-section .links { flex: 0; display: flex; gap: 24px; }
.navbar-3-section .actions { flex: 1; display: flex; justify-content: flex-end; }Sticky Footer
/* Footer stays at bottom even with short content */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
header { flex-shrink: 0; } /* fixed height */
main { flex: 1; } /* fills all remaining space */
footer { flex-shrink: 0; } /* fixed height, stays at bottom */Sidebar Layout
/* Fixed sidebar + fluid main content */
.layout {
display: flex;
min-height: 100vh;
}
.sidebar {
flex: 0 0 280px; /* fixed 280px, never grow or shrink */
}
.main-content {
flex: 1; /* takes remaining space */
min-width: 0; /* prevents overflow */
}
/* Responsive: stack on mobile */
@media (max-width: 768px) {
.layout { flex-direction: column; }
.sidebar { flex-basis: auto; }
}Equal-Height Cards
/* Cards in a row, all same height, button at bottom */
.card-row {
display: flex;
gap: 24px;
}
.card {
flex: 1;
display: flex;
flex-direction: column;
}
.card-body {
flex: 1; /* fills remaining space, pushing footer down */
}
.card-footer {
margin-top: auto; /* always at the bottom */
}10. Flexbox 与 CSS Grid:何时使用
Flexbox 和 CSS Grid 是互补的工具。选择取决于布局是一维还是二维的。
| Feature | Flexbox | CSS Grid |
|---|---|---|
| Dimensions | One (row OR column) | Two (rows AND columns) |
| Sizing | Content-driven | Layout-driven |
| Best for | Components, navbars, centering | Page layouts, dashboards |
| Item placement | Sequential (order property) | Any cell (grid-area) |
| Gap support | Yes (modern browsers) | Yes (all grid browsers) |
/* Use BOTH together for best results: */
/* Grid for page layout */
.page {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 250px 1fr;
}
/* Flexbox for components inside grid areas */
.header {
grid-area: header;
display: flex; /* Flexbox inside Grid */
justify-content: space-between;
align-items: center;
}常见问题
如何用 Flexbox 居中 div?
父元素设置 display: flex; justify-content: center; align-items: center; height: 100vh。这是 CSS 中最简单可靠的居中方法。
flex: 1 和 flex: auto 有什么区别?
flex: 1 从零基础等比增长,flex: auto 从内容大小增长。等宽列用 flex: 1。
为什么 flex 项目溢出容器?
flex 项目默认有 min-width: auto。添加 min-width: 0 来修复溢出。
Flexbox 可以做整页布局吗?
可以。body 设置 display: flex; flex-direction: column; min-height: 100vh。主内容设 flex: 1。
所有浏览器都支持 Flexbox 吗?
是的,Flexbox 全球浏览器支持率超过 99%。gap 属性需要较新的浏览器版本。
TL;DR
display: flexon the parent activates Flexboxjustify-content= main axis alignmentalign-items= cross axis alignmentflex: 1= equal-width items that fill available spaceflex-wrap: wrap= responsive multi-line layoutsgap= clean spacing between items (no outer margins)- Use
min-width: 0to fix overflow issues - Flexbox for components, CSS Grid for page layouts
- Combine both for the best results
Flexbox 是每个前端开发者的必备技能。使用我们的工具来实验布局。