Estimated time: 25–30 minutes
Focus: Build layouts using rows and columns, from card galleries to full-page structures.
Learning goals
By the end of this lesson, you should be able to:
- Create a grid container and understand its rows, columns, tracks and cells.
-
Size tracks with
fr,repeat()andminmax(). - Place items across rows and columns.
- Use named areas to describe a page layout.
- Decide when Grid or Flexbox is the better tool.
1. What is CSS Grid?
CSS Grid is a layout system for arranging items across rows and columns. It’s useful when you want to control both dimensions of a layout—for example, a page with a header, sidebar, main content and footer.
To create a grid, apply display: grid to a parent element. That parent becomes the grid container, and its direct children become grid items.
<div class="layout">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>.layout {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 1rem;
}This creates three equal columns with space between them. Grid affects the container’s direct children; it doesn’t automatically turn every nested element into a grid item.
2. Grid vocabulary
| Term | Meaning |
|---|---|
| Grid container | The element with display: grid. |
| Grid item | A direct child of the grid container. |
| Track | A row or column in the grid. |
| Cell | The space where one row and one column meet. |
| Area | A rectangular section made of one or more cells. |
| Line | A boundary between tracks. Lines are numbered, starting at 1. |
| Gap | Space between rows and columns. |
A grid with two columns has three vertical grid lines: one at the start, one between the columns, and one at the end.
3. Define columns, rows and gaps
Use grid-template-columns to define columns and grid-template-rows to define rows.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
gap: 1rem;
}In this example:
-
The first column is
220pxwide. -
The second column uses
1frto take a share of the remaining space. - The first and last rows size to their content.
- The middle row takes a flexible share of the available space.
-
gapadds space between the tracks.
Use one gap value for both rows and columns, or set them separately:
.grid {
row-gap: 1rem;
column-gap: 1.5rem;
}Quick check 1: If a grid has three columns, how many vertical grid lines does it have?
4. The fr unit
fr means fraction of available space. It’s useful for flexible tracks.
.columns {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}The middle track receives twice the share of each outer track. The tracks divide the available space in the ratio 1:2:1.
You can mix fixed and flexible track sizes:
.layout {
display: grid;
grid-template-columns: 200px 1fr;
}Here, the first column is fixed at 200px; the second fills the remaining available space.
5. repeat() and minmax()
Use repeat() when several tracks use the same sizing rule:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
}That creates three equal columns—equivalent to writing 1fr 1fr 1fr.
minmax() sets a minimum and maximum size for a track:
.grid {
display: grid;
grid-template-columns: repeat(3, minmax(12rem, 1fr));
}Each column is at least 12rem and can grow to share available space.
6. Responsive cards with auto-fit
Grid can automatically fit a changing number of cards into the available space:
<section class="cards">
<article class="card">First lesson</article>
<article class="card">Second lesson</article>
<article class="card">Third lesson</article>
<article class="card">Fourth lesson</article>
</section>.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
}
.card {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.75rem;
}auto-fit lets the browser fit as many columns as will fit, with each column at least 14rem. At narrower widths, the cards naturally move into fewer columns.
If a minimum track size causes overflow on a very narrow screen, you can cap the minimum at the container’s width:
.cards {
display: grid;
gap: 1rem;
grid-template-columns:
repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
}Quick check 2: In grid-template-columns: 1fr 2fr 1fr, how does the middle column compare with either outer column?
7. Place items across tracks
Grid lines let you position items precisely. The end line is the boundary after the area the item occupies.
.featured {
grid-column: 1 / 3;
}This item starts at column line 1 and ends at line 3, so it spans two columns.
You can also use span:
.featured {
grid-column: span 2;
}To make an item span rows:
.tall {
grid-row: 1 / 3;
}Keep in mind that line numbers refer to the grid lines, not the number of columns. grid-column: 2 / 4 covers the space between lines 2 and 4.
8. Name areas to describe a page
Named areas let you write a visual map of your layout in CSS.
<div class="page">
<header class="page-header">Header</header>
<aside class="sidebar">Sidebar</aside>
<main class="main">Main content</main>
<footer class="footer">Footer</footer>
</div>.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
min-height: 100vh;
}
.page-header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.main {
grid-area: main;
}
.footer {
grid-area: footer;
}The strings in grid-template-areas show the layout: the header and footer each span both columns, while the sidebar and main content sit alongside each other.
Area names must match the names used by grid-area, and each named area must form a rectangle.
9. Alignment
Grid offers properties to align items inside their areas or align the grid within its container.
| Property | Applies to | What it does |
|---|---|---|
justify-items / align-items | All items in the container | Aligns items inside their grid areas. |
justify-self / align-self | One item | Aligns an individual item in its area. |
justify-content / align-content | The grid as a whole | Aligns the grid when there is extra space in the container. |
For example:
.card-grid {
display: grid;
align-items: start;
}Grid items stretch by default in many layouts. Setting align-items: start lets them keep their content height instead.
10. Grid or Flexbox?
| Choose Flexbox when… | Choose Grid when… |
|---|---|
| You’re arranging items in one row or column. | You’re controlling rows and columns together. |
| Content size should guide the layout. | You want the layout structure to guide item placement. |
| You’re styling navigation, toolbars or button groups. | You’re creating page layouts, galleries or card grids. |
You can also use both: Grid for the overall page structure and Flexbox inside individual components.
.card {
display: flex;
flex-direction: column;
gap: 0.5rem;
}Common problems
| Problem | What to check |
|---|---|
| Grid properties seem to do nothing | Make sure the parent has display: grid; Grid controls its direct children. |
| An item spans too many or too few columns | Check the grid-line numbers; the ending number is a line, not a track count. |
| Content overflows a track | Check the track’s minimum size, allow text to wrap, or try min-width: 0 on the item. |
| Rows have unexpected heights | Review grid-template-rows and grid-auto-rows. |
| A named area doesn’t appear | Check that the names match and that each area forms a rectangle. |
Exercises
Try these before opening the answer key.
1. Write the CSS for a grid with three equal columns and a gap of 1.5rem.
2. Create two columns: a fixed 250px sidebar and a flexible main area.
3. Give two different ways to make a grid item span two columns.
4. Write a responsive card-grid rule where each column has a minimum width of 16rem.
5. Write a grid-template-areas map for a header spanning the top, a sidebar beside main content, and a footer spanning the bottom.
6. What does grid-column: 2 / 4 do?
7. Give an example of a situation where Flexbox would be a better choice than Grid.
8. Build it. Create a page with a header, sidebar, main area and footer using named grid areas. Add a responsive card grid inside the main area and resize the browser to see how the cards reflow.
9. Experiment. In your browser’s developer tools, enable the Grid overlay. Change the gap, track sizes and an item’s grid-column. Notice how the grid lines and placement change.
Free practice resources
- MDN Web Docs: look up Basic concepts of grid layout and Grid template areas.
- Grid Garden: practise Grid properties through interactive challenges.
- Browser developer tools: use the Grid overlay to inspect tracks, gaps, line numbers and named areas.
Key takeaways
-
display: gridmakes an element a grid container; its direct children become grid items. - Grid is designed for layouts involving rows and columns together.
-
fr,repeat()andminmax()help define flexible tracks. -
auto-fitwithminmax()can create responsive card grids. - Named areas make page layouts easier to read.
- Grid and Flexbox can be combined in the same design.
Quick check 1: Four. A grid with three columns has a line at the beginning, between each pair of columns and at the end.
Quick check 2: The middle column gets twice the share of space of either outer column.
Exercise 1:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1.5rem;
}Exercise 2:
.layout {
display: grid;
grid-template-columns: 250px 1fr;
}Exercise 3: Either grid-column: span 2; or a line-based rule such as grid-column: 1 / 3;.
Exercise 4:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}On very narrow containers, consider using min(100%, 16rem) for the minimum track size to prevent overflow.
Exercise 5:
grid-template-areas:
"header header"
"sidebar main"
"footer footer";Exercise 6: The item starts at column line 2 and ends at line 4, spanning the space between them—columns 2 and 3.
Exercise 7: Flexbox is often a good choice for a navigation bar or toolbar arranged along one row, especially when the items’ content determines their size.
Exercise 8: The page container should use display: grid and grid-template-areas. Give each child a matching grid-area. The card container can use repeat(auto-fit, minmax(...)).
Exercise 9: Changing gap adjusts space between tracks; changing the column definitions changes the tracks; changing grid-column moves or stretches an item across grid lines.