Part 1 - The 2D Layout Revolution: Why CSS Grid?
While Flexbox handles one-dimensional layouts (a row OR a column), CSS Grid is a native two-dimensional layout system. It enables developers to design complex web interfaces by organizing elements across both columns and rows simultaneously.
With CSS Grid, you design a blueprint on the parent container, and direct your elements into precise coordinates. This eliminates the need for deeply nested wrapper <div> elements and delivers pristine, semantic markup.
.grid-container {
display: grid; /* or inline-grid */
}
Part 2 - Defining Columns, Rows & The fr Unit
Grid tracks are the rows and columns formed between grid lines. They are defined on the parent container using grid-template-columns and grid-template-rows.
The Fractional Unit (fr)
The fr unit represents a fraction of the available free space in the grid container. Unlike percentages, fr units automatically factor in gutters and explicit pixel tracks!
.layout {
display: grid;
/* Sidebar 250px fixed, Main content absorbs remaining space */
grid-template-columns: 250px 1fr;
gap: 1.5rem;
}
The repeat() Function
Avoid repetitive syntax by using repeat():
.three-column-grid {
display: grid;
/* Creates 3 identical columns each taking 1 fraction */
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
Part 3 - Responsive Grids Without Media Queries (auto-fit vs auto-fill)
One of CSS Grid's most celebrated superpowers is creating fully responsive card layouts without writing a single media query. This is achieved by pairing repeat() with auto-fit or auto-fill and minmax():
.responsive-cards {
display: grid;
/* Cards will be at least 280px, expanding to fill row evenly */
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.5rem;
}
auto-fit vs auto-fill: The Key Difference
- auto-fit: Collapses empty tracks to
0px, letting existing items expand to fill the entire container row width. (Most commonly desired for card grids). - auto-fill: Preserves empty tracks as empty spaces, maintaining the column slots even if there aren't enough items to fill them.
Part 4 - Positioning Items by Grid Lines & Spans
Grid lines are the numbered horizontal and vertical dividers between tracks. An N-column grid has N+1 vertical lines.
/* Positioning a hero banner across multiple columns */
.hero-item {
/* Starts at vertical line 1, ends at line 3 */
grid-column: 1 / 3;
/* Equivalent span notation: */
grid-column: span 2;
/* Spanning all columns regardless of count: */
grid-column: 1 / -1;
}
Part 5 - Named Grid Areas: Visual Layout Coding
grid-template-areas allows you to draw your entire website layout directly in your CSS stylesheet using named ASCII-style strings!
.page-layout {
display: grid;
grid-template-columns: 240px 1fr 200px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar content ads"
"footer footer footer";
min-height: 100vh;
gap: 1rem;
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: content; }
.ads { grid-area: ads; }
footer { grid-area: footer; }
Part 6 - Grid vs Flexbox: When to Use Which?
| Criteria | CSS Grid | CSS Flexbox |
|---|---|---|
| Dimensions | 2D (Rows AND Columns simultaneously) | 1D (Row OR Column) |
| Approach | Layout-First (Container defines grid) | Content-First (Item sizes dictate flow) |
| Best Use Cases | Page skeletons, dashboards, photo galleries | Navbars, form inputs, button rows, chips |
| Overlapping Items | Native and straightforward with line numbers | Requires absolute positioning hacks |
Part 7 - Grid Quick Reference
| Property | Applies To | Example Usage |
|---|---|---|
grid-template-columns |
Container | repeat(3, 1fr) |
grid-template-rows |
Container | auto 1fr auto |
gap |
Container | 1.5rem |
grid-column |
Child Item | 1 / -1 (full width) |
grid-row |
Child Item | span 2 |
grid-area |
Child Item | header |