Learn HTML and CSS: Aligning items in their cell
Aligning in a cell
On the screen every little tent fills its whole box, right up to the hedge. "You pitch a tent in the middle of your pitch," says Gus, "not in the corner." In a grid you choose where an item stands in its cell.
Pitches in their cell
By default a grid item fills its whole cell: it stretches in width and in height (stretch). With justify-items you align all items in the row, from left to right: start, center or end. With align-items you do the same in the column, from top to bottom. When the cell is bigger than the item, you see the difference.
place-items: center is short for both. For one item you write justify-self, align-self or place-self.
On the page every pitch stands in the middle of its cell, which is 90px tall.
.field { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 90px); gap: 8px; place-items: center;}Try it yourself
Move the camper to the right-hand side of its cell. The other pitches stay as they are.
.field { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;}<!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