Learn HTML and CSS: The box model
The box model
The price cards are stuck together, and the text is stuck to the edge of each card. "Where's the air?" asks Joanne.
Every element is a box
To the browser every element is a box, built from four layers from the inside out.
The content: the text or the image. The padding: space inside the box, between the content and the border; the background runs through it. The border. And the margin: space outside the border, towards the other boxes, always transparent.
Under the page you can see the first card's box as layers, with its sizes, just like in your browser's developer tools.
.card { padding: 16px; border: 2px solid #2d6a4f; margin-bottom: 24px;}One, two or four values
padding: 16px gives all four sides 16px. With two values the first is for top and bottom, the second for left and right: padding: 8px 16px. With four values you go clockwise: top, right, bottom, left. For one side you write padding-top, padding-right, padding-bottom or padding-left. margin works the same way.
A border needs three things: a width, a style and a colour, like border: 2px solid #2d6a4f. Without a style (solid, dashed, dotted) you see nothing.
And with border-radius you round the corners.
.notice { padding: 8px 16px; border: 2px dashed #e76f51; border-radius: 12px;}Try it yourself
Give every card (.card) 16px of padding on all sides, a border of 2px solid #2d6a4f, rounded corners of 16px and a 24px margin below.
/* 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