Skip to content

CSS 网格

Master CSS 网格 for two-dimensional 布局使用行, 列, and template 区域.

你将学到

  • 什么 CSS 网格是 and 如何它 differs 从 Flexbox
  • 定义行 and 列使用网格-template-行 and 网格-template-列
  • Using 网格-template-区域 for visual 布局 naming
  • fr 单元 and responsive 网格模式使用 auto-fit and minmax

概念讲解

CSS Grid

CSS Grid is a two-dimensional layout system. Where Flexbox excels at arranging items in a single row or column, Grid handles both rows and columns simultaneously. This makes it the right choice for page-level layouts (header, sidebar, main, footer), photo galleries, and any design where you need precise control over both axes. Grid and Flexbox are complementary — use Grid for the page structure and Flexbox for components within it.

Defining the Grid

Apply display: 网格 to a container, then define its tracks with 网格-template-列 and 网格-template-行. The fr (fraction) unit distributes available space — 1fr 2fr gives the second column twice the space of the first:

.layout {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
}

This creates a sidebar (250px) and a main area (fills the rest), with three rows.

Grid Template Areas

The most readable way to define a grid layout is 网格-template-区域. You name areas and draw the layout with ASCII-art-like strings:

grid-template-areas:
  "请求头请求头"
  "sidebar 主要"
  "footer footer";

Then assign elements to areas with 网格-区域: 请求头;, 网格-区域: 主要;, etc. The layout reads like a picture of the page — easy to understand and modify.

Responsive Grids with auto-fit

The combination of repeat(), auto-fit, and minmax() creates grids that respond to container size without media queries:

grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

This creates as many columns as fit, each at least 250px wide, stretching to fill remaining space. It is the pattern behind responsive card grids and photo galleries that reflow automatically.

Grid vs. Flexbox

Use Grid when you need control over both rows and columns (page layouts, galleries). Use Flexbox when you are laying out items in one direction (navbars, toolbars, card rows). They work together: a Grid cell can contain a Flex container, and vice versa. You do not have to pick one — you use both.

示例代码

              /* A classic page layout with grid-template-areas */
.page {
  display: grid;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  grid-template-columns: 220px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  gap: 16px;
}

.page > header  { grid-area: header; }
.page > .sidebar { grid-area: sidebar; }
.page > main    { grid-area: main; }
.page > footer  { grid-area: footer; }

/* A responsive photo grid that reflows without media queries */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}
            

page 布局 uses named 区域 for 请求头, sidebar, 主要内容, and footer. sidebar 是 fixed at 220px; 主要 fills REST. gallery uses auto-fit 使用 minmax 到创建 responsive 列那个 reflow automatically as 容器更改宽度.

动手试一试

  • SVG Optimizer

    SVG icons in 网格布局 — optimize 它们 and inspect 它们的 sizing.

  • 镜像颜色 Picker

    选取颜色 for 网格单元格 backgrounds and 边框.

常见错误

错误写法

Using 网格 for 简单行 的项 哪里 Flexbox 会是 simpler.

正确做法

网格 shines for two-dimensional 布局 (行 AND 列). For 单个行 or 列的 项, Flexbox 是 simpler and 更多 appropriate. Use 网格 for page 结构 and galleries; Flexbox for navbars and 组件行.

错误写法

Forgetting gap and using margins on 网格项, causing uneven spacing.

正确做法

Use gap 属性 on 网格容器 for spacing 之间 tracks. 它 handles 两者都行 and 列 gaps cleanly (gap: 16px; or 行-gap/列-gap separately) 不使用 margin collapse issues.

相关代码片段

相关速查表

练习 CSS 网格

6 个练习

练习 CSS 网格

查阅不认识的术语

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

查看术语表

动手做项目

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

查看项目

解读错误信息

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

查看错误