# 弹性盒布局

**弹性盒布局**（Flexbox，即 CSS 弹性盒布局模块）是一种一维 CSS 布局模型，用于将项目排列成行或列、在项目之间分配空间，并沿两条轴对齐项目。项目是“弹性”的，意思是它们会扩展以填满多余空间，或收缩以适应较小的空间，该模块还包含丰富的对齐能力。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)</sup><sup> • </sup><sup>[2](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout/Flexbox)</sup>

| 关键事实 | 详情 |
|---|---|
| 布局模型 | 一维：每次只在一个维度上进行布局，要么作为行，要么作为列，与二维的 CSS 网格布局相对。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)</sup> |
| 启用方式 | 在父元素上设置 `display: flex` 会使其成为弹性容器，其直接子元素成为弹性项目。<sup>[2](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout/Flexbox)</sup> |
| `flex` 默认值 | 初始值为 `flex-grow: 0`、`flex-shrink: 1`、`flex-basis: auto`；`flex: auto` 等同于 `1 1 auto`，`flex: none` 等同于 `0 0 auto`，`flex: 1` 等同于 `1 1 0`。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/flex)</sup> |
| 对齐默认值 | `align-items` 默认为 `stretch`，因此项目会拉伸到容器的交叉轴尺寸；`justify-content` 默认为 `flex-start`。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)</sup> |
| 最小尺寸 | 默认情况下，弹性项目不会收缩到其 `min-content` 尺寸以下；对于不可滚动的项目，自动最小尺寸基于内容，对于滚动容器则为零。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/flex)</sup><sup> • </sup><sup>[4](https://www.w3.org/TR/css-flexbox-1/)</sup> |
| 规范历史 | 首份工作草案于 2009 年 7 月 23 日发布；候选推荐快照于 2012 年 9 月 18 日和 2018 年 11 月 19 日发布；候选推荐草案于 2025 年 10 月 14 日发布。<sup>[5](https://www.w3.org/standards/history/css-flexbox-1/)</sup> |
| `gap` 支持 | 弹性容器的 `gap` 属性在 Chrome 84、Firefox 63 和 Safari 14.1 中受支持，据 Can I use 报告，全球使用支持率为 97.11%。<sup>[6](https://caniuse.com/flexbox-gap)</sup> |

## 弹性模型

在元素上设置 `display: flex` 会将其变为**弹性容器**，其子元素成为**弹性项目**。一条声明就能产生一行等高列，因为弹性项目的默认值就是为了解决等高列这类常见布局问题而设置的。<sup>[2](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout/Flexbox)</sup>

布局沿两条轴进行。**主轴**沿项目排列的方向延伸，由 `flex-direction` 控制；**交叉轴**与之垂直。项目沿主轴的长度是它的主尺寸，沿交叉轴的长度是它的交叉尺寸。<sup>[2](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout/Flexbox)</sup> 由于弹性盒布局只在一个维度上工作，`flex-wrap` 可以让项目换到新行，但一旦发生换行，每一行都像独立的弹性容器一样运作；在分配空间时，弹性盒布局会忽略项目在其他行中的位置。<sup>[7](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Relationship_with_other_layout_methods)</sup>

## 尺寸控制：flex-grow、flex-shrink 和 flex-basis

`flex` 简写属性控制项目如何共享空间。`flex-basis` 指定在分配任何空闲空间之前项目的初始尺寸；其初始值为 `auto`，即使用项目在主维度上设置的任何尺寸，如果未设置则使用内容尺寸。<sup>[8](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Controlling_flex_item_ratios)</sup> `flex-grow` 决定在分配正空闲空间时，项目相对于其他项目增长多少；如果所有项目具有相同的因子，空闲空间会均匀分配，该值是一个比率，因此给每个项目设置 `400000` 与给每个项目设置 `1` 效果相同。<sup>[8](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Controlling_flex_item_ratios)</sup><sup> • </sup><sup>[2](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout/Flexbox)</sup>

常见的简写值有精确的含义。`flex: initial`（`0 1 auto`）根据项目的宽度或高度确定尺寸，在空间不足时允许其收缩到最小尺寸，且从不增长。`flex: auto`（`1 1 auto`）使项目在两个方向上完全灵活。`flex: none`（`0 0 auto`）创建一个完全不可灵活伸缩的项目。教程式的 `flex: 1` 展开为 `1 1 0`，因此项目从最小尺寸开始，按比例增长以填满容器；在这种情况下，收缩因子 1 是多余的，因为项目从最小尺寸开始。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/flex)</sup><sup> • </sup><sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)</sup>

默认设置保护内容不被压扁：弹性项目不会收缩到其 `min-content` 尺寸以下，即仍能容纳最长单词或固定尺寸元素的最小尺寸。要允许更深的收缩，请在项目上设置 `min-width: 0` 或 `min-height: 0`。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/flex)</sup><sup> • </sup><sup>[4](https://www.w3.org/TR/css-flexbox-1/)</sup>

## 对齐

弹性盒布局提供了真正的双向居中，这是早期 CSS 布局方法无法直接做到的。要居中一个元素，`align-items: center` 在交叉轴上对齐它，`justify-content: center` 在主轴上对齐它。<sup>[9](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Aligning_items)</sup>

主要的对齐属性有：`justify-content` 用于主轴上的所有项目，`align-items` 用于交叉轴上的所有项目，`align-self` 用于交叉轴上的单个项目，`align-content` 用于交叉轴上弹性行之间的空间，仅影响会换行的容器。<sup>[9](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Aligning_items)</sup><sup> • </sup><sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)</sup> `justify-content` 还分配剩余空间：`space-between` 在项目之间均匀分配，`space-around` 给每个项目两端各一半大小的空间，`space-evenly` 给每个项目两端各一个完整大小的空间。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)</sup> `justify-items` 属性在弹性盒布局中被忽略，因此 `place-items` 简写属性在弹性容器中设置对齐但不设置主轴对齐。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)</sup> 项目之间的间隙可以用 `gap`、`column-gap` 和 `row-gap` 创建。<sup>[9](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Aligning_items)</sup>

## 排序与无障碍

`order` 属性改变项目的视觉顺序而不触及源代码。规范指出，重新排序只影响视觉渲染，语音顺序和顺序导航仍基于源代码顺序，并声明作者不得使用 `order` 或 `flex-direction` 的 `*-reverse` 值来替代正确的源代码顺序，因为这会破坏文档的无障碍性。<sup>[4](https://www.w3.org/TR/css-flexbox-1/)</sup>

## 弹性盒布局与 CSS 网格

弹性盒布局与 CSS 网格布局之间的区别在规范本身中就有定义：弹性盒布局是一维的，网格是二维的，同时控制行和列。一个实用的测试是布局是否需要同时按行和列来控制；如果是，网格是更好的选择。弹性盒布局从内容出发向外工作，让内容决定每个项目占多少空间，而网格从布局出发向内工作。<sup>[10](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Grid_layout/Relationship_with_other_layout_methods)</sup> 作为一般规则，如果你发现自己在弹性项目上设置宽度以使换行容器的一行与上一行对齐，二维网格很可能是更好的工具。<sup>[7](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Relationship_with_other_layout_methods)</sup> 没有固定规则说小部件用弹性盒布局、大布局用网格；一个小部件可以是二维的，一个大布局可以是一维的。<sup>[7](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Relationship_with_other_layout_methods)</sup>

许多网格对齐特性最初是在弹性盒布局规范中定义的，该规范首次提供了恰当的对齐控制，并使得在页面上居中一个盒子成为可能；这些属性现在定义在 CSS 盒对齐模块中，并在各种布局模式中通用。<sup>[10](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Grid_layout/Relationship_with_other_layout_methods)</sup>

## 历史与浏览器支持

弹性盒布局模块的首份公开工作草案于 2009 年 7 月 23 日发布，描述了一种为界面设计优化的盒模型，基于 Mozilla 应用程序（如 Firefox）中使用的 XUL 盒模型。<sup>[5](https://www.w3.org/standards/history/css-flexbox-1/)</sup><sup> • </sup><sup>[11](https://www.w3.org/TR/2009/WD-css3-flexbox-20090723/)</sup> 该规范于 2012 年 9 月达到候选推荐快照状态，2018 年 11 月再次达到，截至 2025 年 10 月 14 日仍为候选推荐草案。<sup>[5](https://www.w3.org/standards/history/css-flexbox-1/)</sup> 在过渡期间，浏览器实现了多种不同的语法，旧引擎需要 `display: -webkit-flex` 和 IE10 中的 `display: -ms-flexbox` 等厂商前缀；Safari 是最后一个移除前缀的主要浏览器，于 2015 年在 Safari 9 中完成。<sup>[12](https://mdn2.netlify.app/en-us/docs/web/css/css_flexible_box_layout/backwards_compatibility_of_flexbox/)</sup>

核心布局的支持现在已在现代浏览器中广泛可用，旧版本中的部分支持通常指旧版规范或旧语法。<sup>[13](https://caniuse.com/flexbox)</sup> 弹性容器的 `gap` 属性来得较晚：Chrome 84（2020 年 7 月前后）、Firefox 63 和 Safari 14.1，Can I use 报告全球支持率为 97.11%。<sup>[6](https://caniuse.com/flexbox-gap)</sup>

## 典型用例

弹性盒布局适合一维布局需求，例如在父元素内垂直居中内容、让子元素占据可用宽度或高度的相等份额、以及让多列布局中的各列无论内容如何都等高。<sup>[2](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout/Flexbox)</sup> 常见模式包括导航栏、卡片布局、媒体对象（通过设置 `flex-direction: row-reverse` 翻转）和表单控件，其中标签、文本输入框和按钮位于一个弹性包装器中，输入框被赋予 `flex: 1 1 auto`，使其随可用空间增长和收缩，而按钮和标签则不会随之伸缩。<sup>[14](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Use_cases)</sup>

## References

1. [Basic concepts of flexbox, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts)
2. [Flexbox, Learn web development, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout/Flexbox)
3. [flex, CSS property reference, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/flex)
4. [CSS Flexible Box Layout Module Level 1, W3C](https://www.w3.org/TR/css-flexbox-1/)
5. [CSS Flexible Box Layout Module Level 1 publication history, W3C](https://www.w3.org/standards/history/css-flexbox-1/)
6. [gap property for Flexbox, Can I use](https://caniuse.com/flexbox-gap)
7. [Relationship of flexbox to other layout methods, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Relationship_with_other_layout_methods)
8. [Controlling ratios of flex items along the main axis, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Controlling_flex_item_ratios)
9. [Aligning items in a flex container, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Aligning_items)
10. [Relationship of grid layout to other layout methods, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Grid_layout/Relationship_with_other_layout_methods)
11. [Flexible Box Layout Module, W3C Working Draft, 23 July 2009](https://www.w3.org/TR/2009/WD-css3-flexbox-20090723/)
12. [Backwards Compatibility of Flexbox, MDN Web Docs](https://mdn2.netlify.app/en-us/docs/web/css/css_flexible_box_layout/backwards_compatibility_of_flexbox/)
13. [CSS Flexible Box Layout Module, Can I use](https://caniuse.com/flexbox)
14. [Typical use cases of flexbox, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Use_cases)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies › CSS layout and styling features

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/flexbox

English original: https://www.edgepedia.org/flexbox
