Responsive Grid Example
suggest changeApply display: grid to a container, then define its tracks with grid-template-columns or grid-template-rows:
<section class="card-grid">
<article>First</article>
<article>Second</article>
<article>Third</article>
<article>Fourth</article>
</section>
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
gap: 1rem;
}
repeat(auto-fit, ...) creates as many columns as fit. Each column can grow to share the available space but does not become narrower than 16rem; min(16rem, 100%) also prevents overflow when the container itself is narrower than 16rem.
Grid auto-placement puts each child into the next available cell, so ordinary layouts rarely need a placement rule for every item. Place an item explicitly only when the design requires it:
.card-grid > :first-child {
grid-column: 1 / -1;
}
The -1 line refers to the final explicit grid line, making the first card span every explicit column.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents