Skip to content
Created: December 25, 2015

Complete summary of CSS Grid layout syntax, including structured explanation + common examples

🧩 1. What is Grid layout? ​

Grid (grid layout) is a powerful two-dimensional layout system provided by CSS3. The position of elements can be precisely controlled in both row and column directions.

📌 Comparison:

  • Flex → One-dimensional layout (only rows or columns can be controlled)
  • Grid → 2D layout (rows + columns)

📐 2. Basic structure ​

::: demo [vanilla]

html
<html>
  <div class="grid-container">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
  </div>
</html>
<style>
.grid-container {
  border: 1px solid red;
  display: grid; /* 开启网格布局 */
  grid-template-columns: 100px 200px auto;
  grid-template-rows: 80px 80px;
  gap: 10px; /* 行列间距 */
}
.item{
  border: 1px solid black;
}
</style>

:::

🧱 3. Core grammar ​

1️⃣ display: grid | inline-grid ​

Turn on grid layout. inline-grid indicates that the container is an inline element.

2️⃣ grid-template-columns / grid-template-rows ​

Define the dimensions of columns and rows.

css
grid-template-columns: 100px 200px auto;
grid-template-rows: 100px 100px;

✅ Commonly used units:

UnitDescription
px / % / emFixed or relative size
fr"Proportional" unit (most commonly used)
autoAutomatically adjust based on content
minmax(min, max)Set the minimum and maximum range

Example:

css
grid-template-columns: 1fr 2fr 1fr;

Represents 3 columns with a width ratio of 1:2:1.

3️⃣ gap / row-gap / column-gap ​

Define the spacing between rows and columns.

css
gap: 10px; /* 行列间距 */
row-gap: 20px;
column-gap: 15px;

4️⃣ grid-template-areas ​

Define regional layout (strong semantics, suitable for complex page structures).

css
.grid-container {
  display: grid;
  grid-template-columns: 100px 1fr 100px;
  grid-template-rows: 60px 1fr 60px;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

5️⃣ grid-auto-rows / grid-auto-columns ​

Size used to automatically generate additional rows or columns when content exceeds the defined template rows/columns.

css
grid-auto-rows: 100px;

6️⃣ grid-auto-flow ​

Controls the direction of the automatic placement algorithm.

css
grid-auto-flow: row | column | row dense | column dense;
  • row (default): left to right, top to bottom.
  • column: from top to bottom, left to right.
  • dense: fill gaps (may disrupt the order).

📦 4. Grid Item positioning syntax ​

1️⃣ Positioning based on row and column numbers ​

css
.item1 {
  grid-column-start: 1;
  grid-column-end: 3; /* 跨两列 */
  grid-row-start: 1;
  grid-row-end: 2;
}

Abbreviated form:

css
.item1 {
  grid-column: 1 / 3;
  grid-row: 1 / 2;
}

2️⃣ Cross rows/cross columns ​

css
.item {
  grid-column: span 2; /* 跨两列 */
  grid-row: span 3;    /* 跨三行 */
}

3️⃣ Automatic alignment ​

PropertiesScopeValues
justify-itemsAlign the contents of each cell horizontallystart / center / end / stretch
align-itemsAlign the contents of each cell verticallySame as above
place-itemsAbbreviationalign-items justify-items
css
.grid-container {
  place-items: center; /* 水平 + 垂直都居中 */
}

4️⃣ Align the entire grid content ​

PropertiesScopeValues
justify-contentHorizontal alignment of the entire grid within the containerstart / center / end / space-between / space-around
align-contentVertical alignment of the entire grid within the containerSame as above
place-contentAbbreviationalign-content justify-content

5️⃣ Single sub-item alignment (covering global) ​

css
.item1 {
  justify-self: end;
  align-self: center;
}

Or abbreviated:

css
.item1 {
  place-self: center end;
}

🧮 5. repeat() function ​

Simplified duplicate column definition:

css
grid-template-columns: repeat(3, 1fr);

Equivalent to:

css
grid-template-columns: 1fr 1fr 1fr;

Mixed writing is also supported:

css
grid-template-columns: 100px repeat(2, 1fr) 200px;

🧠 6. auto-fit / auto-fill responsive layout ​

css
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}

✅ Difference:

  • auto-fill: Fill as many columns as possible (spaces are still taken)
  • auto-fit: Automatically fold empty columns (adaptive filling)

🧭 7. Naming lines (naming rows) ​

css
grid-template-columns: [left] 1fr [middle] 2fr [right];
.item {
  grid-column: left / right; /* 使用命名行定位 */
}

🧰 8. Practical cases: common layouts ​

✅ Three-column layout (middle adaptive) ​

css
.grid-container {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
}

✅ Waterfall flow layout (dense filling) ​

css
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, 200px);
  grid-auto-rows: 150px;
  grid-auto-flow: dense;
}

⚡ 9. Comparison summary between Grid and Flex ​

FeaturesFlexGrid
Layout dimensionsOne-dimensional (rows/columns)Two-dimensional (rows + columns)
AlignmentMain axis/cross axisRow/column two dimensions
Content AlignmentFlexibleAccurate
Responsive layoutNeeds nestingOne step
Usage scenariosPartial and intra-component layoutOverall page layout

🧭 10. Structured summary (mind map) ​

CSS Grid 布局
 ├── display: grid / inline-grid
 ├── 行列定义
 │   ├── grid-template-columns / rows
 │   ├── repeat() / minmax()
 │   ├── auto-fit / auto-fill
 │   └── gap / row-gap / column-gap
 ├── 自动布局
 │   ├── grid-auto-flow
 │   ├── grid-auto-rows / columns
 ├── 区域布局
 │   ├── grid-template-areas
 │   ├── grid-area(子项)
 ├── 对齐控制
 │   ├── justify-items / align-items / place-items
 │   ├── justify-content / align-content / place-content
 │   └── justify-self / align-self / place-self
 ├── 子项定位
 │   ├── grid-row / grid-column
 │   └── span / 命名线
 ├── 函数
 │   ├── repeat()
 │   ├── minmax()
 │   ├── fit-content()
 └── 实战应用
     ├── 三列布局
     ├── 瀑布流
     ├── 自适应卡片网格
     └── Dashboard布局