CSS··10 分钟阅读

CSS Flexbox 完整指南 — 对齐、分布与布局

掌握 CSS Flexbox:flex-direction、justify-content、align-items、gap 与换行。无需 float 技巧即可搭建导航栏、卡片行与居中布局。

CSS FlexboxFlexbox 教程学习 Flexbox弹性布局CSS 布局教程

CSS Flexbox Layout Explained

Flexbox 是一维布局系统 — 适合导航栏、工具栏、卡片行和垂直居中。它已取代大多数 UI 场景中的 float 技巧。

Why CSS Flexbox Matters

Flexbox is the default tool for component-level layout in modern CSS — navbars, toolbars, and card rows all depend on it.

Master justify-content and align-items on the parent, then flex-grow on children — that covers 90% of real Flexbox use cases.

CSS Flexbox — Key Ideas You Must Know

  • 在父容器设置 display: flex
  • flex-direction: row | column
  • justify-content — 主轴对齐
  • align-items — 交叉轴对齐
  • gap 替代 margin hack
  • flex: 1 让子项伸展

How to Use CSS Flexbox Step by Step

  1. 在容器上设置 display:flex
  2. 选择 row 或 column
  3. 对齐与分布
  4. 用 gap 控制间距
  5. 用 flex:1 让元素增长

CSS Flexbox Code Example

CSS
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

Practice CSS Flexbox with Free Lessons

跟随 Sturdee 的 CSS 布局教程,用 Flexbox 重做导航栏。

Tip: Free interactive lesson: /tutorials/css/css_layout — practice CSS Flexbox with runnable code on Sturdee.

CSS Flexbox 完整指南 — FAQ

Frequently Asked Questions

Flexbox 还是 Grid?+

一维布局(行或列)用 Flexbox;二维页面布局用 Grid。