Lesson 27 / Layout
CSS Grid
Use CSS Grid to build two-dimensional layouts, card grids, page regions, galleries, feature layouts, and responsive structures.
Course Role
Core
Part of the main course path. Prioritize this before moving to optional polish or deeper references.
Teacher Notes / In-Class Use
Demo Live
- Start with three fixed columns, then toggle the span so students can see grid-column: span 2 visually.
- Switch to responsive auto-fit and connect the generated code to the card grid pattern in the lesson.
Try In Class
- Have students recreate one lab state in their own CSS file.
- Ask students to inspect the lab with DevTools Grid overlay while the visual line toggle is on.
Submit Or Check
- Students should know which element gets display: grid and which children become grid items.
- Students should be able to explain the difference between fixed repeat columns and auto-fit/minmax columns.
Watch For
- Students applying Grid to each card instead of the card parent.
- Students using spans that break the layout at small widths.
Learning Goals
- Explain when Grid is a better fit than Flexbox
- Create columns and rows with `grid-template-columns`, `grid-template-rows`, `fr`, `repeat()`, and `gap`
- Build responsive grids with media queries and `auto-fit` / `minmax()`
- Place and align grid items with `grid-column`, `grid-row`, and alignment properties
Interactive Demo
How to use this demo.
Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.
What To Try
- Change the column count, gap, layout pattern, and span toggle.
- Compare the fixed column pattern with the responsive auto-fit pattern.
What Changes
- The grid preview and generated CSS update together.
- The first item can span columns to model a featured card layout.
What To Notice
- Gap, repeat(), minmax(), and grid-column become visible layout decisions.
- Grid is strongest when the page needs rows and columns working together.
Apply It
- Use the generated CSS as a starting point for a card grid, gallery, or sidebar layout.
Interactive Demo
Grid Layout Lab
Change the layout controls and watch how columns, gaps, spans, and responsive card patterns change the grid.
.grid {
display: grid;
gap: 16px;
grid-template-columns: repeat(3, 1fr);
}
.featured {
grid-column: span 2;
} What to notice
- `gap` changes the space between tracks without adding margins to cards.
- `repeat()` creates a repeated column pattern.
- `grid-column: span 2` lets one item take more space.
Try this
- Switch between fixed columns and the responsive auto-fit pattern.
- Turn off the span and compare the first card to the rest.
- Use the gallery and sidebar patterns to see when Grid changes the structure.
This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source.
Grid Is for Two Dimensions
Flexbox is usually best for arranging items in one direction. Grid is best when rows and columns need to work together.
Use Grid for page layouts, galleries, cards, product listings, dashboards, feature sections, and areas where vertical and horizontal alignment both matter.
Flexbox vs Grid
| Use Flexbox When | Use Grid When |
|---|---|
| Items mainly flow in one direction. | You need rows and columns together. |
| You are aligning nav links, buttons, or small groups. | You are building page regions or card layouts. |
| Content size should influence the layout. | The layout structure should guide the content. |
| You need simple vertical or horizontal alignment. | You need tracks, gaps, spans, or named areas. |
Grid Mental Model
| Term | Meaning |
|---|---|
| Grid container | The parent element with display: grid. |
| Grid item | A direct child of the grid container. Nested grandchildren are not grid items unless their parent is also a grid. |
| Track | A row or column. |
| Cell | One space where a row and column cross. |
| Gap | The space between rows and columns. |
| Grid line | The numbered line at the edge of a row or column. |
Basic Grid
Start by turning the parent into a grid. Then define the columns, rows, and gaps the direct children should use.
.gallery {
display: grid;
gap: 1rem;
grid-template-columns: 1fr 1fr 1fr;
} The `fr` Unit
The fr unit means a share of the available space. Three 1fr columns split the available width into three equal parts.
.three-column-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
.sidebar-layout {
display: grid;
grid-template-columns: 16rem 1fr;
} `repeat()` Shorthand
repeat() lets you write repeated tracks more clearly. These two examples create the same three equal columns.
.gallery-longhand {
grid-template-columns: 1fr 1fr 1fr;
}
.gallery-repeat {
grid-template-columns: repeat(3, 1fr);
} Responsive Card Grid
This pattern creates cards that wrap into as many columns as fit. It is useful for projects, products, articles, image galleries, and resource lists.
.card-grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
} Breaking Down `auto-fit` and `minmax()`
If cards become too narrow, increase the minimum value in minmax(). If too few cards fit, lower the minimum value carefully.
| Part | Meaning |
|---|---|
repeat(...) | Repeats a column pattern. |
auto-fit | Creates as many columns as can fit in the available space. |
minmax(16rem, 1fr) | Each column should be at least 16rem, but can grow to share available space. |
gap: 1rem | Keeps consistent space between cards without adding margins to each card. |
Media Query Strategy
You can also change the number of columns with media queries. This is easier to understand when the layout needs specific breakpoints.
.card-grid {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 48rem) {
.card-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 72rem) {
.card-grid {
grid-template-columns: repeat(3, 1fr);
}
} Placing Items
Grid items can span across columns or rows. This is useful for featured cards, large images, or layouts where one item needs more space.
.project-grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(3, 1fr);
}
.featured-card {
grid-column: span 2;
} Sidebar Layout
A common page pattern is a sidebar beside main content. On small screens, stack the layout into one column.
.page-layout {
display: grid;
gap: 2rem;
grid-template-columns: 1fr;
}
@media (min-width: 64rem) {
.page-layout {
grid-template-columns: 16rem 1fr;
}
} Grid Areas
Grid areas can make a page layout readable in CSS. Use them when the structure is clear and not too complex.
.page {
display: grid;
gap: 1rem;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 16rem 1fr;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; } Alignment in Grid
Grid has alignment tools for items inside their cells and for the whole grid inside the container.
| Property | What It Controls |
|---|---|
justify-items | Horizontal alignment of items inside their grid cells. |
align-items | Vertical alignment of items inside their grid cells. |
justify-content | Horizontal alignment of the whole grid when extra space exists. |
align-content | Vertical alignment of the whole grid when extra space exists. |
.logo-grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(4, 1fr);
align-items: center;
justify-items: center;
} Use DevTools Grid Overlays
Turn on the Grid overlay in DevTools to see columns, rows, line numbers, gaps, and grid areas. It makes abstract layout rules visible.
- Inspect the grid container, not only an individual card.
- Turn on line numbers when using
grid-columnorgrid-row. - Use the overlay to confirm whether gaps and tracks match your plan.
Common Layout Patterns
| Pattern | Grid Feature |
|---|---|
| Card grid | repeat(), gap, and minmax() |
| Sidebar and main content | Two columns with a media query |
| Image gallery | Equal columns and consistent gaps |
| Featured card span | grid-column: span 2 |
| Dashboard layout | Rows, columns, and sometimes grid areas |
Common Mistakes
| Mistake | Fix |
|---|---|
Putting display: grid on the wrong element | The grid container must be the parent of the items you want to arrange. |
| Expecting nested grandchildren to become grid items | Only direct children become grid items. Add another grid to the nested parent if needed. |
| Using fixed pixel columns everywhere | Use fr, percentages, minmax(), and media queries. |
Forgetting gap | Use gap instead of adding margins to every item. |
Making minmax() columns too narrow | Choose a minimum width that preserves readable content. |
| Using Grid for every alignment problem | Use Flexbox for simple one-direction alignment. |
Checkpoint
Before moving on, make sure these feel true.
- I can explain when Grid is a better fit than Flexbox.
- I can build a responsive card grid.
- I can inspect grid tracks and gaps in DevTools.
Practice
- Convert a group of cards into a responsive CSS Grid layout.
- Build a sidebar and main content layout that becomes one column on small screens.
- Create a featured card that spans two columns on larger screens.
- Use both a media-query strategy and an
auto-fit/minmax()strategy, then compare them. - Use DevTools Grid overlays to inspect rows, columns, line numbers, and gaps.