Learn HTML and CSS: Centring
Centring with margin auto
Joanne wants the notice in the middle of the page, not against the left edge. She tried `text-align: center`, but then only the text is in the middle, not the block.
Sharing out the space fairly
A block with a width smaller than its parent is centred with margin-left: auto and margin-right: auto, or in short: margin: 0 auto. The browser shares the space that's left fairly between left and right.
Without a width (or max-width) the block takes the full width, and there's nothing to share.
text-align: center is something else: it centres the text inside the box, not the box itself.
.notice { max-width: 16rem; margin: 16px auto;}Try it yourself
Put the main content (main) in the middle, never wider than 48rem. And put the notice in the middle, never wider than 20rem.
/* Your CSS goes here. */<!DOCTYPE html><html lang="en"> <head> <title>The Steep Wall climbing centre</title> <link rel="stylesheet" href="basis.css"> <link rel="stylesheet" href="style.css"> </head> <body> <header> <img src="greep.svg" alt="" width="40" height="40"> <h1>The Steep Wall</h1> <p class="slogan">Climbing centre in Ilkley</p> <nav> <a href="index.html">Home</a> <a href="routes.html">Routes</a> <a href="prices.html">Prices</a> <a href="lessons.html">Lessons</a> </nav> </header> <main> <p class="notice">We're closed on Saturday 12 April for a competition.</p> <section class="cards"> <article class="card"> <h2>Day pass</h2> <p class="price">£14</p> <p>Climb all day, for as long as your arms hold out.</p> <a class="button" href="buy.html">Buy</a> </article> <article class="card"> <h2>Ten-visit pass</h2> <p class="price">£120</p> <p>Ten visits, valid for a year.</p> <a class="button" href="buy.html">Buy</a> </article> </section> <section class="news"> <h2>News</h2> <p>There are twelve new routes in the bouldering room, from green to black.</p> <p>There's a beginners' class every Tuesday. We'll lend you climbing shoes.</p> <p>On Wednesday afternoons the youth club climbs. Feel free to come and watch.</p> </section> </main> <footer> <p>7 Handhold Lane, Ilkley</p> </footer> </body></html>body { margin: 0; font-family: system-ui, sans-serif; background: #eef3ee; color: #1f2a24;}header { padding: 16px; background: #2d6a4f; color: #ffffff;}header h1 { margin: 4px 0 0;}.slogan { margin: 0 0 8px;}nav a { color: #ffffff;}main { padding: 0 16px 24px;}.notice { background-color: #ffe8d6;}.card { padding: 0 16px; background: #ffffff;}.price { font-size: 1.5rem; font-weight: bold;}.button { background-color: #e76f51; color: #ffffff; text-decoration: none;}.news { background: #ffffff;}footer { padding: 12px 16px; background: #1b4332; 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