Skip to content

CSS Flexbox

学习 CSS Flexbox for one-dimensional 布局 — align, distribute, and 顺序项.

你将学到

  • 什么 Flexbox 是 and 当到 use 它 over other 布局方法
  • 容器属性: flex-方向, justify-内容, align-项, gap
  • 项属性: flex-grow, flex-shrink, flex-basis, align-self
  • 如何 中心任何事使用 three 行的 Flexbox

概念讲解

CSS Flexbox

Flexbox (Flexible Box Layout) is a CSS layout system designed for arranging elements in one dimension — either a row or a column. It solves the problems that hacks like floats and inline-block were pressed into solving for decades: equal-height columns, vertical centering, distributing space between items, and reordering elements. If you learn one layout tool in CSS, make it Flexbox.

The Container and Its Items

Apply display: flex to a parent element (the flex container), and its direct children become flex items. The container controls the overall arrangement; the items can override their own behavior. This parent-child relationship is the core of how Flexbox works.

Container Properties

  • flex-方向 — 行 (default), 列, 行-reverse, 列-reverse. Sets the main axis.
  • justify-内容 — aligns items along the main axis: flex-开始, 中心, 空间-之间, 空间-围绕, 空间-evenly.
  • align-项 — aligns items along the cross axis: stretch (default), flex-开始, 中心, baseline.
  • flex-wrap — nowrap (default) or wrap. Lets items wrap to the next line instead of shrinking.
  • gap — space between items, without needing margins. gap: 16px is cleaner than margin tricks.

Item Properties

  • flex-grow — how much the item grows to fill free space (0 = don't grow).
  • flex-shrink — how much it shrinks when space is tight (1 = shrink equally).
  • flex-basis — the ideal size before growing or shrinking (like a starting width).
  • align-self — overrides the container's align-项 for this one item.

The shorthand flex: 1 sets grow=1, shrink=1, basis=0 — the most common pattern for equal-width items that fill the container.

Centering Anything

The classic CSS problem — "如何做 I 中心某事?" — has a three-line Flexbox answer:

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

This centers a child both horizontally and vertically, regardless of its size. Before Flexbox, this required negative margins, table cells, or absolute positioning hacks.

示例代码

              /* A responsive card row that wraps and centers */
.card-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: stretch;
  gap: 20px;
}

.card-row .card {
  flex: 1 1 250px;  /* grow, shrink, basis */
  min-width: 0;     /* prevent overflow */
}

/* A navbar: logo on the left, links on the right */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 24px;
}

.navbar .links {
  display: flex;
  gap: 16px;
}

/* A sidebar + main content layout */
.layout {
  display: flex;
  gap: 24px;
}
.layout .sidebar { flex: 0 0 240px; }
.layout .main { flex: 1; }
            

card-行 wraps cards and centers 它们使用 gap for spacing. flex: 1 1 250px lets cards grow and shrink 使用 250px 开始大小. navbar uses 空间-之间到 推送 logo and 链接 apart. 布局 gives sidebar fixed 240px and lets 主要 fill REST.

动手试一试

  • SVG Optimizer

    SVG icons 是经常排列使用 Flexbox — optimize and inspect 它们.

  • 镜像颜色 Picker

    选取颜色 for 你的 flex 容器 backgrounds and 项边框.

常见错误

错误写法

Using justify-内容 for vertical centering 当 flex-方向是 列.

正确做法

justify-内容工作 on 主要 axis. 如果 flex-方向是 列, 主要 axis 是 vertical, 所以 justify-内容 controls vertical alignment and align-项 controls horizontal. 总是 know 哪个 axis 是哪个.

错误写法

Forgetting 最小值-宽度: 0 on flex 项, causing 文本到 overflow.

正确做法

Flex 项有 默认最小值-宽度的 auto, 哪个 prevents 它们从 shrinking below 它们的内容大小. 设置最小值-宽度: 0 (or 最小值-高度: 0 for 列) 到允许 shrinking and prevent overflow.

相关代码片段

相关速查表

练习 CSS Flexbox

7 个练习

练习 CSS Flexbox

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误