There is one line of CSS that appears at the top of almost every modern stylesheet, and it changes the meaning of every width, height, padding, and border that you write afterwards.
It is a single property with a surprising effect, and once you know it you will not write a stylesheet without it.
The previous lesson in this module covered "What Are the Four Layers of the Box Model?", and the next lesson in this module is about "Why Do Vertical Margins Collapse Together?".
What does the box-sizing change?
It changes what the width and the height measure.
By default they measure the content only, so the padding and the border are added outside, and the element ends up bigger than the number you wrote.
With border-box, the width you write is the total width of the element, and the padding and the border are taken out of it.
/* the default: content-box */
.card {
width: 300px;
padding: 20px;
border: 2px solid #333;
}
/* total width: 300 + 40 + 4 = 344px */
/* the alternative: border-box */
.card {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 2px solid #333;
}
/* total width: exactly 300px */The difference is 44 pixels on a single element, and it compounds quickly.
Three cards in a row behave differently. With the content-box they need 344 pixels each, which is 1032 pixels in total. With the border-box they need 300 each, which is 900 in total, and that is exactly the width of the row.
What does the universal reset look like?
*,
*::before,
*::after {
box-sizing: border-box;
}Those three selectors matter.
The universal selector covers the ordinary elements, and the two pseudo-element selectors cover the generated content of the ::before and the ::after, which do not inherit the box model from their element by default.
Leave them out and a generated box is sized differently from its neighbours. That looks like a rounding error until you inspect it and find the real cause.
Why is the border-box the better default?
Because it makes the number you write mean what the name suggests.
When a designer says the card is 300 wide, the width you set is 300, and the padding sits inside of it.
With the content-box, the same sentence means something different from the number that ends up on the screen. Every layout that has to fit a fixed number of items across becomes a calculation.
/* content-box: two items plus a gap do not fit */
.row { display: flex; gap: 20px; }
.card { width: 50%; padding: 20px; } /* each is 50% + 40px */
/* border-box: two items plus a gap fit exactly */
.card { width: 50%; padding: 20px; } /* with box-sizing: border-box */Second, it removes an entire category of bug.
The problem of "two 50% items with padding overflow the row" has no equivalent under the border-box, and it is one of the most common layout bugs there is.
Third, it makes it safe to change the padding later. The outer size stays the same, and the content area absorbs the change, so a padding tweak never breaks the surrounding layout.
Are there cases where the content-box is better?
There are, and they are worth knowing rather than assuming that the rule is absolute.
A width that should describe the text area, such as a column of text in a fixed-width layout, is more naturally expressed as the content-box. The padding is genuinely extra space around the text there, and not space carved out of the width.
There is also a convention around form elements.
Many stylesheets set box-sizing: content-box on the inputs, the selects, and the textareas, because the default size of those elements is calculated from their content, and the author is expected to add the padding afterwards.
This is a historical habit and not a requirement, but if you set the universal reset then you have to handle those elements deliberately.
*,
*::before,
*::after {
box-sizing: border-box;
}
/* inputs keep a predictable inner text area */
input,
select,
textarea {
box-sizing: content-box;
padding: 0.5rem;
}How do you spot the difference on a live page?
Look at the element in the developer tools with the box model overlay on.
The tooltip shows the content size and the total size.
If the total is larger than the width you wrote, the element is using the content-box. If the total matches, it is using the border-box.
It is a two-second check, and it explains a lot of the "why is this wider than I set it" questions.
under content-box
width: 300
total rendered width: 344
under border-box
width: 300
total rendered width: 300What does the border-box change about percentage widths?
The switch to the border-box changes more than the padding.
It changes what a percentage width means, and the difference shows up as soon as padding meets a fluid layout.
With the content-box, width: 50% sets the content width. Add padding and a border and the box is now wider than half of its parent, so two such boxes side by side no longer fit and the second one wraps onto a new line.
With the border-box, width: 50% is the total visible width, with the padding and the border included. The two fifty-percent boxes now sit exactly next to each other, whatever padding you add.
<div class="pair">
<div class="cell">left</div>
<div class="cell">right</div>
</div>*,
*::before,
*::after {
box-sizing: border-box;
}
.pair {
display: flex;
gap: 1rem;
}
.cell {
width: 50%;
padding: 1rem;
border: 1px solid #d8e0e8;
background: #f4f8fc;
}With that reset in place, the two cells divide the row exactly, and the padding is inside the fifty percent rather than on top of it.
Without the reset they would overflow the row, because each cell would be fifty percent of the row plus twice its padding plus its borders.
The same reasoning applies to the height, and to the min-width and the max-width.
The rule is worth stating in one line. With the border-box, the width you write is the width the visitor sees, and that is almost always what you meant.
There is one caveat. The reset does not change how a box without a declared width behaves.
An element with width: auto still fills the space it is given either way, because the used width is computed from the space available and not from the value of the property.
Quick reference
- The
box-sizing: border-boxmakes the width and the height include the padding and the border. - The default is the
content-box, where they do not. - You apply the reset to the
*, the::before, and the::after. - It removes the "two 50% items overflow" class of bug.
- Text areas in a fixed column are the main case where the content-box reads better.
Try it yourself: write a row of three cards at 33 percent each with padding, and watch it overflow. Add the universal border-box reset and watch the row fit exactly. Then measure one card in the developer tools before and after, and confirm which layer the padding came out of.