Sub topic 10
Arrange project cards with CSS Grid
This chapter assumes you have a page with semantic sections, a linked stylesheet, and a working grasp of the box model from earlier chapters. You will lay out the project cards in the Projects section with CSS Grid.
Why Grid for cards
Flexbox arranges items along one axis. Grid arranges items in rows and columns at the same time. Project cards are a good fit for Grid because every card in a row should line up with the cards above and below it. A grid container defines the tracks (the columns and rows), and the browser places each child into a cell.
Add the markup
Inside the Projects section, wrap the cards in a container with the class project-list. Each card is an article with its own heading.
<section id="projects">
<h2>Projects</h2>
<div class="project-list">
<article>
<h3>Weather Widget</h3>
<p>A small app that shows the forecast for your city.</p>
</article>
<article>
<h3>Recipe Box</h3>
<p>A searchable list of family recipes.</p>
</article>
<article>
<h3>Trail Map</h3>
<p>An interactive map of local hiking trails.</p>
</article>
</div>
</section>Define the grid
Setting display: grid on the container makes its children grid items. The grid-template-columns property describes the columns, and gap sets the space between cells.
#projects .project-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1.5rem;
}Read the columns line by line:
repeat(...)repeats a column definition instead of you writing it several times.minmax(16rem, 1fr)gives each column a minimum width of 16rem and lets it grow to share the free space equally. The unitfris a fraction of the available space.auto-fittells the browser to create as many columns as fit, then stretch them to fill the row.
The result is that a wide screen shows three or more cards per row, a tablet shows two, and a phone shows one. You wrote no breakpoints for this.
Add a media query for padding
A media query applies rules only when a condition is true. Here it adds more side padding to main once the viewport is at least 48rem wide.
@media (min-width: 48rem) {
main {
padding-inline: 2rem;
}
}Here the grid adapts by itself, and the media query only adjusts spacing. The next chapters cover responsive design in more detail.
Style the cards
The grid places the cards, but you still style each one with the box model.
.project-list article {
padding: 1rem;
border: 1px solid #d0d7de;
border-radius: 0.5rem;
}Because the stylesheet sets box-sizing: border-box, the padding and border stay inside each column and do not cause overflow.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Cards stack in one column and ignore your columns | `display: grid` is missing or the selector does not match | Check the class name and the selector |
| Horizontal scrollbar on small phones | The minimum in `minmax()` is wider than the screen | Lower the minimum or use `min(16rem, 100%)` |
| Gaps look uneven | You used margins on cards as well as `gap` | Remove the margins and use only `gap` |
| One lonely card stretches across the row | `auto-fit` collapses empty tracks | Use `auto-fill` to keep empty columns |
Exercise
What does repeat(auto-fit, minmax(16rem, 1fr)) do, and why does it reduce the need for media queries?
Show answer
It creates as many columns as fit in the container, each at least 16rem wide, and stretches them to share the leftover space equally. As the screen narrows, columns drop out and cards move to new rows, so the column count adapts without breakpoints.
Exercise
A learner's grid causes a horizontal scrollbar on a 320px-wide phone. Name one cause and one fix.
Show answer
The 16rem minimum (256px at the default font size) plus the page padding can exceed the screen width. A fix is to use minmax(min(16rem, 100%), 1fr) so a column never grows wider than its container.