Sub topic 6
box-sizing: content-box vs border-box
This chapter assumes you know the four layers of the box model (content, padding, border, margin) from the earlier chapters. It also assumes you can write CSS rules and use px, rem, and %. Here you will learn what the width and height properties measure, and how box-sizing changes that.
The question box-sizing answers
When you write width: 300px, which part of the box is 300px wide? The CSS box-sizing property decides. It has two values you will use:
content-boxis the default.widthandheightset the size of the content area. Padding and border are added outside it.border-boxmakeswidthandheightset the size of the border box. Padding and border are taken out of that size, and the content area gets what remains.
Margin is never part of either calculation. It always sits outside the border box.
Content-box arithmetic
Here is a card with the default sizing:
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}Padding and border apply to both the left and right sides, so each is counted twice:
| Part | Calculation | Result |
|---|---|---|
| Content width | set by `width` | 300px |
| Horizontal padding | 20px × 2 | 40px |
| Horizontal border | 5px × 2 | 10px |
| Border box width | 300 + 40 + 10 | 350px |
| Horizontal margin | 10px × 2 | 20px |
| Space occupied in the line | 350 + 20 | 370px |
The element you declared as 300px wide takes up 370px. Adding padding or a border to a rule that already has a width makes the element grow. That is the main source of layout surprises.
Border-box arithmetic
Now switch the same card to border-box:
*,
*::before,
*::after {
box-sizing: border-box;
}
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
}The border box is 300px wide. The browser subtracts the padding and border to get the content width: 300 − 40 − 10 = 250px. You can change the padding or border later and the card stays 300px wide. Only the content area changes.
The same rule applies vertically. With height: 200px, padding: 20px, and a 5px border, the content height is 200 − 40 − 10 = 150px.
Why border-box is the usual choice
Most teams set border-box for every element, for these reasons:
- Percentages behave. With
content-box, a child set towidth: 100%plus padding is wider than its parent and overflows. Withborder-box, it fits. - Mixed units are safe. You can write
width: 50%; padding: 1.5rem;without calculating what is left over. - Columns add up. Two siblings at
width: 50%with padding and borders still fit on one line, as long as there is no extra margin or whitespace between them. - The number you write is the number you see. In developer tools and in
offsetWidth, the box measures what the stylesheet said.
Here is the percentage problem as a page you can run:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>box-sizing and percentages</title>
<style>
.parent {
width: 400px;
background: #eee;
}
.child {
width: 100%;
padding: 20px;
border: 5px solid #333;
background: #9cf;
margin-bottom: 1rem;
}
.fixed {
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">content-box: 450px wide, sticks out of the parent</div>
<div class="child fixed">border-box: 400px wide, fits the parent</div>
</div>
</body>
</html>The first child is 100% of 400px for its content, then adds 40px of padding and 10px of border. Its border box is 450px, so it overhangs the parent by 50px. The second child fits.
Applying border-box globally
There are two common ways to set it.
The first is the universal selector, which also covers pseudo-elements:
*,
*::before,
*::after {
box-sizing: border-box;
}The second is the inheritance pattern:
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}box-sizing is not inherited by default, so inherit is what carries the value down from html. The benefit is that a third-party component can opt out of border-box by setting box-sizing: content-box on its own root. Everything inside the component then inherits content-box instead of being forced back to border-box by a universal rule. Use the first form for an application you fully control. Use the second when you embed code that expects other sizing.
Edge cases and gotchas
Padding and border cannot be squeezed out
Under border-box, the content area cannot go below zero. If padding and border add up to more than the declared width, the browser does not shrink them. It sets the content size to 0 and the border box becomes as wide as the padding and border together:
.tiny {
box-sizing: border-box;
width: 30px;
padding: 20px;
border: 5px solid #333;
}The padding and border total 50px, which is more than 30px. The border box is 50px wide, not 30px, and the content width is 0. If a box is wider than you set, check for this.
Min and max dimensions follow box-sizing
min-width, max-width, min-height, and max-height use the same box as width. With border-box, max-width: 600px limits the whole border box. With content-box, it limits only the content, and the border box can reach 600px plus padding and border. Keep this in mind when you mix a width and a max-width on the same element.
auto widths are not affected the same way
A block element with width: auto fills the available space of its container, and padding, border, and margin are subtracted from that space. The element's border box fits no matter which box-sizing is set. box-sizing only matters once you give an explicit size.
Margin is never included
Neither value includes margin. A border-box element with width: 50% and a margin-left: 1rem still overflows a line shared with another 50% element. For gaps between columns, use gap in flex or grid layouts instead of margins.
No padding-box
The specification once had a padding-box value, which would have made width cover content and padding but not the border. Browsers have dropped or never shipped it. Do not rely on it. Only content-box and border-box are safe.
Form controls and replaced elements
Many browsers use border-box by default for some form controls, such as <button>, <select>, and several <input> types. Other elements, such as <textarea> and <img>, default to content-box. A global reset removes these inconsistencies. This is one more reason to apply one.
Checking the result in code
You can read the computed size in JavaScript to confirm what the browser did:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Measure the box</title>
<style>
.card {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid #333;
}
</style>
</head>
<body>
<div class="card" id="card">Hello</div>
<script>
const card = document.getElementById("card");
console.log(card.offsetWidth); // 300 (border box)
console.log(card.clientWidth); // 290 (padding box, no border)
console.log(getComputedStyle(card).width); // "300px" under border-box
</script>
</body>
</html>offsetWidth returns the border box width. clientWidth returns the content plus padding, without the border (and without any scrollbar). The computed width value follows box-sizing: it reports 300px here, but it would report 250px if you removed box-sizing: border-box and kept width: 250px. The result depends on which box the property measures, so do not assume the computed width is always the content width.
Summary of decisions
| Situation | Choice |
|---|---|
| New application or site | `border-box` on all elements |
| Embedding a widget that assumes the default | The inheritance pattern, with `content-box` on the widget root |
| Need `width` to mean "text area only" | `content-box` on that element |
| Padding must never change the overall width | `border-box` |
Exercise
An element has width: 400px; padding: 10px 30px; border: 2px solid; margin: 0 15px;. Calculate its border box width and the horizontal space it occupies when (a) box-sizing is content-box and (b) box-sizing is border-box. Then say what happens under border-box if you change the width to 50px.
Show answer
(a) content-box: padding is 30 × 2 = 60px and border is 2 × 2 = 4px, so the border box is 400 + 60 + 4 = 464px. Margin adds 15 × 2 = 30px, so it occupies 494px. (b) border-box: the border box is 400px, the content width is 400 − 60 − 4 = 336px, and the element occupies 400 + 30 = 430px. At width: 50px under border-box, padding and border total 64px, which exceeds 50px. The content width is clamped to 0 and the border box becomes 64px wide.