CSS Grid Architecture
Unlike Flexbox, which is primarily a one-dimensional layout system (rows or columns), CSS Grid Layout is a powerful two-dimensional layout engine. It enables developers to align elements along both rows and columns simultaneously with precise control over track sizing, placement, and spatial distribution.
1. Grid Terminology & Dual-Axis Modelβ
To build layouts effectively with CSS Grid, you must understand its core structural components:
Grid Container
βββββββββββββββββββββββββββββββββββββββββββ
β Col Line 1 Col Line 2 β
ββββ Line 1 βββββββββββββ¬β βββββββββββ β
β β Grid Cell β Grid Cell β β Row Track
ββββ Line 2 βββββββββββββΌββββββββββββ€ β
β β Grid Cell β Grid Cell β β
ββββ Line 3 βββββββββββββ΄ββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββ
Column Track
- Grid Container: The parent element defined with
display: gridordisplay: inline-grid. - Grid Item: Direct child elements inside a grid container.
- Grid Line: The horizontal and vertical dividing lines that separate tracks (numbered starting at
1). - Grid Track: The space between two adjacent grid lines (a row or column).
- Grid Cell: The single intersection unit of a row track and a column track.
- Grid Area: Any rectangular space bounded by four grid lines containing one or more grid cells.
2. Track Sizing & Fractional Units (fr)β
Grid tracks are defined on the container using grid-template-columns and grid-template-rows.
CSS Grid introduces the Fractional Unit (fr), which represents a fraction of the available free space in the grid container after fixed tracks and gaps are computed.
.container {
display: grid;
/* 3 Columns: 200px fixed, remaining space split 1:2 */
grid-template-columns: 200px 1fr 2fr;
/* 2 Rows: Fixed 80px top row, flexible bottom row */
grid-template-rows: 80px 1fr;
gap: 1rem;
}
The repeat() and minmax() Functionsβ
repeat(count, track_size): Replicates track patterns without manually typing values.minmax(min, max): Sets a flexible size range for grid tracks so they respond fluidly to viewport changes.
/* Creates 4 equal columns of at least 150px, expanding up to 1fr */
.grid-fluid {
display: grid;
grid-template-columns: repeat(4, minmax(150px, 1fr));
}
3. Responsive Auto-Placement: auto-fill vs auto-fitβ
Combining repeat(), minmax(), and automatic track repetition allows you to build responsive grids without writing media queries:
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
| Keyword | Behavior When Extra Space Exists |
|---|---|
auto-fill | Fills the row with as many tracks as possible. If extra space remains, it keeps empty tracks in the row. |
auto-fit | Fits existing tracks into the row. If extra space remains, it collapses empty tracks to 0px and stretches remaining items to fill the row. |
4. Grid Template Areasβ
Grid Template Areas allow you to map out page layouts using intuitive ASCII-art string representations directly in your CSS:
.layout-container {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
/* Assigning Children to Defined Areas */
.site-header { grid-area: header; }
.site-sidebar { grid-area: sidebar; }
.site-main { grid-area: main; }
.site-footer { grid-area: footer; }
Interactive Playground: Grid Track Architectureβ
Experiment with two-dimensional grid layouts, item spanning, and area assignments in the live preview component below:
Summary Reference Tableβ
| Property / Syntax | Applied To | Description |
|---|---|---|
display: grid | Container | Activates the 2D grid layout context |
grid-template-columns | Container | Defines explicit column track sizes and quantities |
grid-template-rows | Container | Defines explicit row track sizes and quantities |
**gap / grid-gap** | Container | Sets horizontal and vertical gutters between tracks |
grid-column: 1 / -1 | Item | Spans an item from line 1 to the last explicit line |
grid-area | Item | Assigns an item to a named area defined in grid-template-areas |