Learn HTML and CSS: A grid that chooses its own columns
Minmax and auto-fit
Gus looks at the website on his phone, on the laptop at reception and on the big screen in the clubhouse. "On the phone the pitches are too narrow, on the big screen too wide." A grid can choose for itself how many columns fit.
As many columns as fit
minmax(100px, 1fr) is a column that's at least 100px wide and at most one part of the free space. Put that in repeat with auto-fit instead of a number, and the grid chooses for itself how many columns fit: repeat(auto-fit, minmax(100px, 1fr)).
On a phone two or three fit, on a laptop all six, without a single media query. auto-fill does the same, but keeps empty columns when there are too few items. With auto-fit they drop away and the items fill the whole width.
Change the width under the page, or tap phone or laptop, and watch the columns follow.
.field { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 8px;}Try it yourself
Put the cards side by side when there's room, each at least 200px wide and with 12px of space. Together they always fill the whole width.
/* Your CSS goes here. */<!DOCTYPE html><html lang="en"><head> <title>The Lazy Hammock</title> <link rel="stylesheet" href="basis.css"> <link rel="stylesheet" href="style.css"></head><body> <header> <img src="tent.svg" alt="" width="40" height="40"> <h1>The Lazy Hammock</h1> <nav> <a href="index.html">Home</a> <a href="pitches.html">Pitches</a> <a href="prices.html">Prices</a> <a href="book.html">Book</a> </nav> </header> <main> <p class="slogan">Camping among the pines in Bala. Peace and quiet guaranteed.</p> <section class="field"> <div class="pitch tent">Tent</div> <div class="pitch caravan">Caravan</div> <div class="pitch camper">Camper</div> <div class="pitch cabin">Cabin</div> <div class="pitch tent">Tent</div> <div class="pitch hammock">Hammock</div> </section> <section class="cards"> <article class="card"> <h2>Showers</h2> <p>Hot showers and a washing-up area, open all day.</p> </article> <article class="card"> <h2>Shop</h2> <p>Fresh bread every morning from 8 am.</p> </article> <article class="card"> <h2>Lake</h2> <p>Swimming in summer, fishing all year round.</p> </article> </section> </main> <footer> <p>1 Hammock Lane, Bala LL23 7SR</p> </footer></body></html>body { margin: 0; font-family: system-ui, sans-serif; background: #f5f2e8; color: #1f2a22;}header { padding: 12px 16px; background: #2d4a3a; color: #ffffff;}header h1 { margin: 0; font-size: 1.5rem;}nav a { padding: 6px 10px; border-radius: 999px; color: #ffffff; text-decoration: none;}main { padding: 0 16px 24px;}.slogan { margin: 16px 0 0;}.field { margin: 16px 0; padding: 9px; border-radius: 18px; background-color: #bcd6a0; background-image: repeating-linear-gradient(90deg, rgb(255 255 255 / 0.13) 0 20px, transparent 20px 40px); box-shadow: inset 0 0 0 1px rgb(45 74 58 / 0.16);}/* A little tent, caravan or cabin on every pitch: only for the look. */.pitch { position: relative; margin: 3px; padding: 27px 4px 9px; border-radius: 12px; background: #fffdf6; box-shadow: 0 1px 2px rgb(31 42 34 / 0.12), 0 6px 14px -8px rgb(31 42 34 / 0.4); font-size: 0.875rem; font-weight: 700; line-height: 1.25; text-align: center;}.pitch::before { content: ""; position: absolute; top: 9px; left: 50%; width: 16px; height: 12px; margin-left: -8px; background: #e07a3f; clip-path: polygon(50% 0, 100% 100%, 0 100%);}.caravan::before { width: 18px; margin-left: -9px; border-radius: 5px 5px 3px 3px; background: #3d8c95; clip-path: none;}.camper::before { width: 20px; margin-left: -10px; background: #4f7cac; clip-path: polygon(0 15%, 68% 15%, 100% 55%, 100% 100%, 0 100%);}.cabin::before { background: #9c6b3f; clip-path: polygon(50% 0, 100% 45%, 100% 100%, 0 100%, 0 45%);}.hammock::before { top: 11px; width: 20px; height: 7px; margin-left: -10px; border-bottom: 3px solid #c99a2e; border-radius: 0 0 10px 10px; background: none; clip-path: none;}.reception::before { width: 14px; height: 14px; margin-left: -7px; border-radius: 50%; background: #2d4a3a; clip-path: none;}.lake::before { top: 10px; width: 24px; height: 11px; margin-left: -12px; border-radius: 50%; background: #5fa8d3; clip-path: none;}.playground::before { width: 18px; margin-left: -9px; background: #d1495b; clip-path: polygon(50% 0, 100% 100%, 82% 100%, 50% 32%, 18% 100%, 0 100%);}aside { padding: 12px 16px; background: #e4ecd6;}aside h2 { margin: 4px 0 8px; font-size: 1.05rem;}.card { padding: 4px 16px; border-radius: 12px; background: #ffffff; box-shadow: 0 1px 2px rgb(31 42 34 / 0.08);}.card h2 { font-size: 1.15rem;}footer { padding: 12px 16px; background: #2d4a3a; color: #ffffff;}Ready for more?
The full lesson has 5 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson