Why CSS Grid Changed Everything
Before CSS Grid, developers cobbled together layouts using floats, inline-blocks, and eventually Flexbox. Each solution had limitations. CSS Grid arrived as the first layout system actually designed for two-dimensional layouts—rows AND columns simultaneously. In 2026, Grid support is universal and there's no reason not to use it.
Grid Basics: The Core Concepts
CSS Grid works by designating a container element as a grid, then placing child items within that grid.
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto;
gap: 20px;
}
Key terminology:
- Grid container: The element with
display: grid - Grid item: Direct children of the grid container
- Grid line: The dividing lines that make up the grid structure
- Grid track: The space between two grid lines (a row or column)
- Grid cell: The intersection of a row and column
- Grid area: A rectangular space made up of grid cells
The fr Unit: Fractional Space
The fr unit is Grid's superpower. It represents a fraction of the available space:
/* Three equal columns */
grid-template-columns: 1fr 1fr 1fr;
/* Left sidebar + main content */
grid-template-columns: 250px 1fr;
/* Ratio: sidebar gets 1/4, content gets 3/4 */
grid-template-columns: 1fr 3fr;
repeat() and minmax(): Responsive Grids
Combine repeat() with minmax() and auto-fill to create responsive grids without media queries:
/* Auto-fill columns, each at least 250px, max 1fr */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
This single line creates a responsive grid that automatically adjusts the number of columns based on available space. Cards will never shrink below 250px, and they fill the available width evenly. No media queries needed.
Placing Items Explicitly
Use grid-column and grid-row to place items precisely:
.hero {
grid-column: 1 / 3; /* span from line 1 to line 3 */
grid-row: 1 / 2;
}
/* Using span keyword */
.feature {
grid-column: span 2; /* span 2 columns from current position */
}
Named Grid Areas: The Most Readable Layout Code
Named areas make your grid layout readable like a diagram:
.layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-template-columns: 250px 1fr 1fr;
grid-template-rows: 60px 1fr 80px;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
The grid-template-areas property visually represents your layout. It's immediately clear what goes where—no mental arithmetic needed.
Grid Auto-Placement
Items without explicit placement are placed automatically. Control this behavior with grid-auto-flow:
/* Fill row by row (default) */
grid-auto-flow: row;
/* Fill column by column */
grid-auto-flow: column;
/* Dense packing: fill gaps when possible */
grid-auto-flow: row dense;
The dense keyword is useful for masonry-like layouts where you want items to fill gaps left by larger items.
Alignment in Grid
Grid provides powerful alignment control in both dimensions:
/* Align all items within their cells */
.container {
justify-items: center; /* horizontal */
align-items: center; /* vertical */
}
/* Align the entire grid within the container */
.container {
justify-content: space-between;
align-content: center;
}
/* Override alignment for a single item */
.special {
justify-self: end;
align-self: start;
}
Grid vs Flexbox: When to Use Which
The most common question: Grid or Flexbox?
- Use Grid for two-dimensional layouts (rows AND columns matter)
- Use Flexbox for one-dimensional layouts (either a row OR a column)
- Common pattern: Grid for page layout, Flexbox for components within that layout
They're complementary, not competing. A grid item can itself be a flex container.
Practical Example: Card Grid
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 24px;
padding: 24px;
}
.card {
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
This creates a fully responsive card grid that works from mobile to wide desktop screens with no media queries.
Conclusion
CSS Grid is one of those rare tools that, once mastered, makes you wonder how you ever built layouts without it. Start with display: grid and grid-template-columns, learn the fr unit and repeat(auto-fill, minmax()) pattern, and then explore named areas for complex layouts. Grid support is universal in 2026—there's no barrier to using it today.
