When you send a parcel, the item is wrapped in bubble wrap and then put in a carton. On the post office shelf, the carton sits a little apart from the parcel next to it.
In CSS, every element is a box built the same way as that parcel. The element's content sits in the middle, like the item. Around the content are three layers: padding (the bubble wrap), border (the carton) and margin (the gap to the next parcel). The way the browser calculates the size of this box is called the box model.
The four layers of a box
This example has one box and the CSS rule that sets its four layers:
<div class="box">Content</div>
<style>
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
</style>
.boxis the selector: the declarations inside{ }apply to the element withclass="box".width: 200pxsets the width of the content, which is what is inside the box. In this example the content is the text "Content".padding: 20pxis the space inside the box, between the content and the edge line. The box's background colour fills the padding too.border: 5px solid blackis the edge line: 5px thick, an unbroken line (solid), black.margin: 10pxis the empty space outside the edge line, the gap to other elements. A margin is always transparent.
So the order of the layers, from the inside out, is: content, padding, border, margin. Each declaration is written property: value; (one colon, a semicolon at the end), inside the { } of its selector.
Why the box got wider
The first example sets width: 200px, but on screen the box is 250px wide. The reason is the box-sizing property, which decides what width measures. Its default value is box-sizing: content-box: width only sets the width of the content. Padding and border are added outside that width, each on the left and on the right:
200 (content) + 20 + 20 (padding) + 5 + 5 (border) = 250px
Even with no border, the box is already wider: width: 200px with padding: 20px is 240px on screen, not 200px. The margin isn't counted, because it is outside the border.
border-box: width becomes the full width
Now the .box gets box-sizing: border-box:
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}
With box-sizing: border-box, padding and border are counted inside the width. The box stays 200px wide on screen. The content gets narrower instead: 200 - 40 - 10 = 150px (padding 20 + 20, border 5 + 5). The box's width is now easy to predict: it always equals width. That is why many developers put the rule below at the top of their CSS. The selector * means every element:
* {
box-sizing: border-box;
}
But * doesn't reach ::before and ::after (extra parts that CSS creates, covered in the pseudo-elements lesson). So the full rule uses three selectors separated by commas: *, *::before, *::after { box-sizing: border-box; }.
Sometimes two boxes need different box-sizing values. A rule with two selectors separated by a comma, like .a, .b { ... }, gives both boxes the same values. Different values need a rule of their own for each class, each with its own { and }:
.a { box-sizing: content-box; }
.b { box-sizing: border-box; }
⚠️ When the box isn't as wide as you calculated
- A typo in the property name. The browser doesn't know
boz-sizingorbox-size, so it skips them quietly, with no error message. - Missing curly braces. If you write
.b box-sizing: border-box;without{ }, the browser throws that rule away, and often the rules after it too. - Counting padding once. Padding and border are on two sides, left and right. For the full width, count each of them twice.
width: 100%plus padding.width: 100%means as wide as the parent. Withcontent-box, the padding is added outside that width, so the box becomes wider than its parent.border-boxprevents that.
🔬 Try it yourself
Slide the values below and watch each layer of the box change.
TL;DR: Every element is a box with four layers, from the inside out: content (what is inside), padding (space inside the box), border (the edge line) and margin (the gap to other elements). With the default box-sizing: content-box, width only sets the width of the content. Padding and border are added outside that width, on the left and right: a box with width: 200px, 20px padding and a 5px border is 200 + 20 + 20 + 5 + 5 = 250px wide on screen. With box-sizing: border-box, padding and border are counted inside the width, so the box stays as wide as width. The margin is outside the border, so it is not counted.
⚡ The four layers of a box
Write:
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
Meaning: Every element is a box with four layers. From the inside out: content (what is inside, as wide as width), padding (space inside the box), border (the edge line), then margin (the gap to other elements). The box's background colour fills the padding too. A margin is always transparent.
⚡ The box got wider
Guess first: how wide on screen is the .box from the previous step?
250px: 200 + 20 + 20 (padding) + 5 + 5 (border). The margin isn't counted, because it is outside the border.
Meaning: The browser's default is box-sizing: content-box. With that value, width only sets the width of the content, and padding and border are added outside it, on the left and on the right.
⚡ width becomes the full width
Write:
* {
box-sizing: border-box;
}
Meaning: The selector * means every element. With border-box, padding and border are counted inside the width. The width: 200px box from the first step stays 200px wide on screen, and its content gets narrower instead.