Chapter 12 of 17 · CSS for Beginners — Complete Free Tutorial
Responsive Grid Patterns
The killer feature of CSS Grid for real products: card grids that adapt from one to four columns without a single media query. This chapter covers auto-fit, minmax(), named template areas for page shells, and when breakpoints are still worth adding.
The magic one-liner
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 1.25rem;
}
Expected output:
- Phone (~360 px): 1 column of full width.
- Tablet (~768 px): 2 columns.
- Laptop (~1280 px): 4 columns… however many fit.
The browser measures the container and fits as many 260 px-minimum columns as possible; each stretches to an equal share. No breakpoints, no guessing device widths.
auto-fit vs auto-fill
| Value | When items are fewer than columns |
|---|---|
auto-fit | Empty tracks collapse — remaining cards stretch wide (great for 3 cards on a wide screen) |
auto-fill | Empty tracks keep their width — cards stay their size, leaving a gap at the end |
Rule of thumb: content grids → auto-fit; fixed card sizes → auto-fill.
Page shells with grid-template-areas
.page {
display: grid;
grid-template-columns: 240px minmax(0, 1fr);
grid-template-areas:
"nav header"
"nav main"
"footer footer";
min-height: 100svh;
gap: 1rem;
}
.page-header { grid-area: header; }
.page-nav { grid-area: nav; }
.page-main { grid-area: main; min-width: 0; }
.page-footer { grid-area: footer; }
Named areas make layout self-documenting — you can literally see the shape of the page in the CSS. Expected output: navigation down the left spanning both rows, header and content on the right, footer full-width underneath.
Making the shell responsive
@media (max-width: 800px) {
.page {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"nav"
"main"
"footer";
}
}
Spanning patterns
.grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 1rem;
}
.wide { grid-column: span 2; } /* half row */
.full { grid-column: 1 / -1; } /* whole row */
@media (max-width: 640px) {
.wide { grid-column: span 1; } /* stack on phones */
}
Explicit rows only when needed
/* Masonry-ish uniform grid */
.grid { grid-auto-rows: 220px; }
/* Variable content that must not clip */
.grid { grid-auto-rows: minmax(180px, auto); }
For true masonry (uneven heights packing tightly), CSS still has no native solution — accept equal rows or use a small JS library.
When you still need media queries
- Changing the structure (sidebar below content on phones).
- Type scale steps (bigger headings on bigger screens).
- Density: gutters and padding often grow with the viewport.
- Interaction mode: hover-only affordances need touch fallbacks.
Breakpoint strategy and syntax are the next chapter: Responsive Design & Media Queries.
Common mistakes
minmax(0, …)forgotten in text grids — long words/tokens expand the track and cause horizontal scrolling (the exact bug class fixed on this site).- minmax too small —
minmax(120px, 1fr)crams 6 columns onto a phone; 240–320 px is a sane floor for cards. - Fixed pixel containers — grids inside a
width: 1200pxdiv still overflow smaller screens; use max-width + percentages. - Grid areas without a mobile version — always define the stacked shape.
Practice checklist
- Build a 6-card grid with
auto-fit/minmax(260px, 1fr)— resize the browser and count columns. - Swap
auto-fitforauto-fillwith 2 items and note the difference. - Recreate your site's header/sidebar/footer as a template-areas shell, then stack it at 800 px.
Grid sizes everything in pixels — but pixels are not always the right unit. Next: CSS Units & Values.
Frequently Asked Questions
CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.
The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.
Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.
No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.
Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.
Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.
Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.
Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.
Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.
JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.