Web Developer Path ยท lesson 11 of 18
The box model
Every element is a box. Learn how its content, padding, border and margin add up so you can space and size anything.
What you will learn
- the four layers: content, padding, border, margin
- shorthand order: top, right, bottom, left
box-sizing: border-boxand why it is the sensible choice- centring a box with
margin: 0 auto
Understand the idea
To the browser every element is a rectangular box with four layers. In the middle is the content. Around it is padding, space inside the box. Then comes the border, a line around the padding. Outside is the margin, empty space that keeps other boxes away. Padding takes the background colour of the box, but margin is always transparent.
Shorthand sets all four sides together. margin: 10px 20px means 10px top and bottom and 20px left and right. With four values they go clockwise: top, right, bottom, left (margin: 1px 2px 3px 4px). The same works for padding. A border needs a width, a style and a colour: border: 2px solid navy;.
A trap: by default width is the width of the content only, so padding and border make the box wider than you asked. box-sizing: border-box changes that so width includes the padding and border, which makes sizes predictable. Many developers set it on every element.
To centre a box horizontally give it a width and set margin: 0 auto. The two automatic side margins share the leftover space equally.
Example
.card {
box-sizing: border-box;
width: 240px;
padding: 16px;
border: 3px solid royalblue;
margin: 12px auto;
}With border-box the card is exactly 240px wide, padding and border included. The automatic side margins centre it.
Your turn: code to type and practise
Style .card: give it a width, some padding, a border, a margin of 12px auto, and box-sizing: border-box.
Stretch challenge
Remove box-sizing and measure how much wider the card becomes in the preview.
โ Combinators, pseudo-classes and pseudo-elementsDisplay and position โ
More in this module
Keep going in Web Developer Path
Adding CSS: three ways and three selectors
Colour the `h1` text, give `.intro` a `background-color`, and give `#special` both a `color` and a `background-color`.
Start โ8Backgrounds and gradients
Give `.hero` a `background-image` that uses `linear-gradient`, set its `background-size`, and give `.card` a `background-color`. Use the long-hand properties.
Start โ9Fonts and text styling
Style `h1` with a `font-family`, a `font-size`, `text-align: center` and `text-transform: uppercase`. Style `p` with a `line-height` and `letter-spacing`, and remove the underline from `a` with `text-decoration: none`.
Start โ10Combinators, pseudo-classes and pseudo-elements
Write four rules with exactly these selectors: `.menu a:hover` (set a `color`), `.menu li:first-child` (set `font-weight`), `h2::before` (set `content`) and `input[type="text"]` (set a `border`).
Start โ12Display and position
Make `.card` `position: relative`, pin `.badge` to its top-right corner with `position: absolute`, `top` and `right`, and hide `.hide-me` with `display: none`.
Start โPractise the idea
๐๏ธ Play Block Views
Count hidden cubes, match front, top and side views, then build a stack from three plans.
FAQ
The box model: common questions
What is the CSS box model?
The idea that every element is a box made of content, padding, border and margin, from the inside out.
What does box-sizing: border-box do?
It makes the width and height include padding and border, so the box is the size you set.
How do I centre a div horizontally?
Give the div a width, then set margin: 0 auto. The automatic left and right margins share the leftover space equally, so the box sits in the middle.
I am stuck. What can I do?
Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.
Does my work get saved?
Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.
Start a conversation
Enjoying coding? Meet a teacher.
Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay