CSS Grid (Concept) & display: grid
CSS Grid (Concept) & display: grid
Level 6 — Layouts — CSS Grid The most powerful layout system in CSS, designed to handle complex, two-dimensional (rows AND columns simultaneously) webpage structures.
1. Prerequisites
- Flexbox (Concept) &
display: flex— Grid uses similar concepts (like Parent/Child relationships andgap), but takes them to the next level. display: blockvsinlinevsinline-block— CSS Grid two-dimensional layout model.
2. Term Category
Layout System (Grid) (Universal Modern Standard .): CSS Grid (Concept) & display: grid is a fundamental concept in this technology stack. Level 6 — Layouts — CSS Grid
3. Explanation
(1) Design Motivation — "Why did we design this?"
Flexbox is incredible, but it is one-dimensional. It is designed to lay things out in a single Row or a single Column. While flex-wrap allows things to wrap to a new line, you cannot strictly control how the items align vertically with the row above them.
The W3C created CSS Grid to handle two-dimensional layouts. With Grid, the Parent container draws a literal invisible grid of intersecting rows and columns over itself. You can then take a Child element and say: "I want you to span from Column 1 to Column 3, and from Row 2 to Row 4."
It is the ultimate tool for defining the overall macro-structure of a webpage (e.g., Header, Sidebar, Main Content, Footer).
(2) Reality Metaphor
Flexbox is like stringing beads on a necklace. You push them all to the left, or spread them out, but they are all stuck on a single string (1D). Grid is like an Excel Spreadsheet or a chessboard. You define exactly how many columns and rows exist, and you can place elements into specific cells (2D).
(3) Code Examples
Building a 3-Column Grid
Grid introduces a brand new CSS unit called the fr (Fractional Unit). 1fr means "take up 1 fraction of the available free space."
<div class="grid-container">
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
</div>
.grid-container {
/* Turn on the Grid engine */
display: grid;
/* Define the Columns:
"I want 3 columns. Each column should take up 1 equal fraction (1fr) of the space." */
grid-template-columns: 1fr 1fr 1fr;
/* Use gap to space the grid cells out! */
gap: 20px;
}
/*
Result: The container draws a 3-column grid.
Card 1 goes in Col 1. Card 2 goes in Col 2. Card 3 goes in Col 3.
Because there are only 3 columns, Grid automatically creates a new row, and Card 4 goes into Col 1 of the new row!
*/
4. Common Mistakes & Pitfalls
Mistake 1: Using Grid when Flexbox is better (and vice-versa)
The mistake: Using Grid to align three buttons in a navigation bar, or using Flexbox to build a complex 12-column magazine layout.
Why it's wrong: While they overlap in capabilities, they have different purposes:
- Use Flexbox for 1D content-first layouts: Navigation bars, aligning an icon next to text, or wrapping a list of tags. You let the size of the content dictate the layout.
- Use Grid for 2D layout-first structures: The main skeleton of the webpage (Header, Sidebar, Main, Footer), or strict photo galleries where everything must perfectly align both vertically and horizontally. You let the layout dictate the size of the content.
Mistake 2: Using CSS Grid for Simple 1D Single-Row Navigation Bars (Over-Engineering)
The mistake: Using CSS Grid to align 4 menu items in a single horizontal navigation row.
Why it's wrong: CSS Grid is designed for 2D layouts (rows AND columns simultaneously). For 1D single-row or single-column layouts, Flexbox is lighter and more appropriate.
Incorrect:
/* Over-engineering 1D navigation bar with CSS Grid */
nav { display: grid; grid-template-columns: repeat(4, 1fr); }
Fix:
/* Use Flexbox for 1D navigation layouts: */
nav { display: flex; gap: 20px; }
Mistake 3: Confusing Explicit Grid Tracks with Implicit Grid Auto-Tracks
The mistake: Defining explicit columns grid-template-columns without configuring grid-auto-rows for dynamic rows.
Why it's wrong: When items exceed explicit grid definitions, CSS Grid generates implicit tracks. Configure grid-auto-rows: minmax(100px, auto) to control implicit row sizes.
Incorrect:
/* Extra dynamic data rows take default height 0 or auto without min height */
Fix:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(150px, auto); /* Implicit row height */
}
5. Practice Exercises
Exercise 1: Establishing a 2D Grid Layout Container
Scenario: An author establishes a two-dimensional CSS Grid layout container for a main application page.
Requirements:
- Apply
display: gridto container. - Define 2D columns and rows.
- Add
gap: 1.5rem.
Answer
Implementation
.page-layout {
display: grid;
grid-template-columns: 18rem 1fr; /* 2 Columns: Sidebar + Main */
grid-template-rows: auto 1fr auto; /* 3 Rows: Header, Content, Footer */
gap: 1.5rem;
min-height: 100vh;
}
Technical Explanation
- The CSS Grid Layout Model: A two-dimensional layout system designed to handle BOTH columns (horizontal) and rows (vertical) simultaneously.
- Grid Container & Grid Items: Declaring
display: gridturns the parent into a grid container and all immediate children into grid items. - Explicit Grid Tracks:
grid-template-columnsandgrid-template-rowsdefine the explicit grid line matrix.
Exercise 2: Defining Explicit Grid Tracks vs Implicit Grid Extensions
Scenario: Demonstrates how extra content creates implicit grid rows automatically.
Requirements:
- Define explicit columns.
- Configure
grid-auto-rows: minmax(10rem, auto)for implicit rows.
Answer
Implementation
.gallery-grid {
display: grid;
grid-template-columns: repeat(3, 1fr); /* Explicit 3-column tracks */
grid-auto-rows: minmax(10rem, auto); /* Implicit rows created for extra overflow items */
gap: 1rem;
}
Technical Explanation
- Explicit Grid: Tracks explicitly defined using
grid-template-columnsandgrid-template-rows. - Implicit Grid: Tracks created automatically by the browser when items overflow explicit row/column definitions.
grid-auto-rowsControl: Controls the default size of automatically created implicit rows (minmax(10rem, auto)).
Exercise 3: Grid Formatting Context Boundaries
Scenario: Explains child element formatting rules inside grid containers.
Requirements:
- Show direct child item behavior inside
display: grid.
Answer
Implementation
.grid-parent > * {
/* Direct children become grid items automatically; floats and inline behaviors are ignored! */
background-color: #ffffff;
padding: 1rem;
}
Technical Explanation
- Grid Formatting Context: Direct child elements become grid items; legacy properties (
float,clear,vertical-align) are completely IGNORED on grid items. - Margin Collapse Elimination: Margins between adjacent grid items NEVER collapse.
- Clean Encapsulated Layout: Isolates internal item layouts from external page flow.
6. Related Terms
- Flexbox (Concept) &
display: flex— The 1D alternative. grid-template-columns/grid-template-rows— Track columns and rows template definitions.gap(Grid Gap) — Spacing between cells.frUnit (Fractional Unit) — The fractional grid track unit.grid-template-areas— Named grid areas.
7. Key Takeaways
display: grid;creates a two-dimensional layout system.- You explicitly define the columns using
grid-template-columns. - The
fr(Fractional Unit) is the secret weapon of CSS Grid, allowing columns to dynamically share available space. - Rule of Thumb: Flexbox is for 1D rows/columns (micro-layout). Grid is for 2D structures (macro-layout).