HTML & CSS · Beginner · 15 min
Make a grid add columns automatically
Let the available width determine how many workshop cards fit in each row.
Give each column a minimum width
Use minmax(180px, 1fr) to keep a column at least 180px wide and let it share extra space. Put that size in repeat() to apply it to several columns. A fixed number repeats the same number of columns at every width.
.workshop-grid {
display: grid;
grid-template-columns: repeat(2, minmax(180px, 1fr));
gap: 16px;
}Expected appearance
Two columns, each at least 180px wide. The grid needs at least 376px including its gap.
Let the grid decide how many columns fit
Use auto-fit in repeat() to create as many columns as the available space can hold. Empty columns collapse so the occupied columns can stretch. In this example, the 180px minimum works at the lesson widths. A container narrower than 180px would need a smaller minimum.
.workshop-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}Expected appearance
320px: 1 column 520px: 2 columns 768px: 3 columns
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Edit the page content and include CSS in a style element. The preview supports a limited set of HTML elements. Scripts, external resources, navigation to other sites, and form submission are disabled.
Build and check
Keep all four cards and the 16px gap. In .workshop-grid, add grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)). The grid should fit with one column at 320px, two at 520px, and three at 768px.
Use repeat() with auto-fit as the first argument and minmax(180px, 1fr) as the second.
Show worked solution
<style>
.workshops { max-width: 720px; margin: 1rem auto; font-family: sans-serif; line-height: 1.5; }
.workshop-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.workshop-grid article { padding: 0.75rem; border: 1px solid #64748b; overflow-wrap: anywhere; }
</style>
<main class="workshops">
<h1>Library workshops</h1>
<div class="workshop-grid">
<article><h2>Drawing</h2><p>Sketch everyday objects.</p></article>
<article><h2>Reading</h2><p>Discuss a short story.</p></article>
<article><h2>Writing</h2><p>Write a scene from a prompt.</p></article>
<article><h2>Research</h2><p>Find sources for a project.</p></article>
</div>
</main>The grid fits as many columns as the available width allows. Each column starts at 180px and can grow to share leftover space. The 16px gaps count toward the space needed.
Page preview
Run to preview your page. Check tests the task requirements, including the requested screen widths. Check results do not replace keyboard and visual review.
Try it yourself
- Check the grid at narrow and wide widths. Count how many cards sit on the first row.
- Temporarily remove two cards and preview at a wide width. The remaining cards stretch because auto-fit collapses the unused column. Restore the cards before checking again.