Layouts Reinvented – CSS Grid Tutorial
April 12, 2017
CSS Grid reached stable status in 2017, unlocking direct control over rows and columns without float or flex hacks.
Core ideas
- A grid container establishes rows and columns.
- Child elements become grid items you can place precisely.
- Gaps create gutters without extra markup.
- Media queries or the
repeat()
function adapt layouts easily.
Basic grid
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: masonry;
gap: 1rem;
}
<section class="gallery">
<img src="img1.jpg" alt="…" />
<img src="img2.jpg" alt="…" />
<img src="img3.jpg" alt="…" />
<!-- more -->
</section>
repeat(3, 1fr)
creates three equal columns.gap
adds uniform spacing.
Named areas
.article {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 250px 1fr;
gap: 1rem;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
<article class="article">
<header class="header">Header</header>
<aside class="sidebar">Sidebar</aside>
<section class="main">Main content</section>
<footer class="footer">Footer</footer>
</article>
The template string describes layout visually.
Auto‑placing cards
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
Cards wrap and stretch to fill available space, ideal for responsive dashboards.
Aligning and spacing
justify-items
controls horizontal alignment inside cells.align-items
handles vertical alignment.justify-content
andalign-content
distribute the entire grid if track sizes do not fill the container.
.centered {
display: grid;
place-items: center; /* shorthand for align-items + justify-items */
}
Grid + Flexbox
Use Grid for overall page structure, then Flexbox inside items for one‑dimensional alignment, such as centering content within a card.
Browser support checklist
- Works in modern Chrome, Firefox, Safari, and Edge released in 2017.
- Use
@supports (display: grid)
to progressively enhance older browsers. - Fallbacks: fall back to flex layouts or single‑column flow when Grid unsupported.
Next steps
- Experiment with
grid-auto-flow: dense
for masonry effects. - Combine Grid with
minmax()
andauto-fit
to create fluid layouts. - Use fragment identifiers like
grid-column: 1 / -1
to span full width when needed.
CSS Grid frees layouts from the limitations of floats and complex media queries, giving you direct, powerful tools for two‑dimensional design.
Recent posts
- At-Least-Once vs. Exactly-Once - Understanding Message Delivery Guarantees
June 12, 2025
Learn about message delivery guarantees in distributed systems. Understand why most production systems implement at-least-once delivery with idempotency rather than attempting exactly-once delivery.
- How Idempotency Saves Your API from Chaos
June 11, 2025
Learn how to implement idempotency in your APIs to prevent duplicate actions and ensure data consistency. Includes practical examples with Supabase and Node.js.
- Vibe Coding ‑ Notes from the First Try
June 6, 2025
Quick lessons from spinning up a new blog with an AI pair‑programmer and Cursor.