HTML en CSS leren: Items uitlijnen in hun cel
Uitlijnen in een cel
Op het scherm vult elk tentje zijn hele vak, tot tegen de haag. "Een tent zet je in het midden van je plaats," zegt Gust, "niet in de hoek." In een grid kies je zelf waar een item in zijn cel staat.
Plaatsen in hun cel
Een grid-item vult standaard zijn hele cel: het rekt uit in de breedte en in de hoogte (stretch). Met justify-items lijn je alle items uit in de rij, van links naar rechts: start, center of end. Met align-items doe je hetzelfde in de kolom, van boven naar onder. Is de cel groter dan het item, dan zie je het verschil.
place-items: center is de afkorting voor allebei. Voor één item schrijf je justify-self, align-self of place-self.
Op de pagina staat elke plaats in het midden van haar cel, die 90px hoog is.
.terrein { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 90px); gap: 8px; place-items: center;}Probeer het zelf
Schuif de camper naar de rechterkant van haar cel. De andere plaatsen blijven zoals ze zijn.
.terrein { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;}<!DOCTYPE html><html lang="nl"><head> <title>De Luie Hangmat</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>De Luie Hangmat</h1> <nav> <a href="index.html">Home</a> <a href="plaatsen.html">Plaatsen</a> <a href="prijzen.html">Prijzen</a> <a href="boeken.html">Boeken</a> </nav> </header> <main> <p class="slogan">Kamperen tussen de dennen in Bree. Rust gegarandeerd.</p> <section class="terrein"> <div class="plaats tent">Tent</div> <div class="plaats caravan">Caravan</div> <div class="plaats camper">Camper</div> <div class="plaats chalet">Chalet</div> <div class="plaats tent">Tent</div> <div class="plaats hangmat">Hangmat</div> </section> <section class="kaarten"> <article class="kaart"> <h2>Sanitair</h2> <p>Warme douches en een afwasplaats, de hele dag open.</p> </article> <article class="kaart"> <h2>Winkeltje</h2> <p>Vers brood elke ochtend vanaf 8 uur.</p> </article> <article class="kaart"> <h2>Vijver</h2> <p>Zwemmen in de zomer, vissen het hele jaar.</p> </article> </section> </main> <footer> <p>Hangmatweg 1, 3960 Bree</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;}.terrein { 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);}/* Een tentje, caravan of chalet op elke plaats: alleen voor de sier. */.plaats { 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;}.plaats::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%);}.chalet::before { background: #9c6b3f; clip-path: polygon(50% 0, 100% 45%, 100% 100%, 0 100%, 0 45%);}.hangmat::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;}.receptie::before { width: 14px; height: 14px; margin-left: -7px; border-radius: 50%; background: #2d4a3a; clip-path: none;}.vijver::before { top: 10px; width: 24px; height: 11px; margin-left: -12px; border-radius: 50%; background: #5fa8d3; clip-path: none;}.speeltuin::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;}.kaart { padding: 4px 16px; border-radius: 12px; background: #ffffff; box-shadow: 0 1px 2px rgb(31 42 34 / 0.08);}.kaart h2 { font-size: 1.15rem;}footer { padding: 12px 16px; background: #2d4a3a; color: #ffffff;}Klaar voor meer?
De volledige les heeft 5 oefeningen, van makkelijk naar uitdagend, met hints, punten en een reeks die je kan opbouwen.
Begin aan de les