How to Include Padding and Border in an Element's Width with box-sizing in CSS
By default, an element's width is only its content: padding and border are added on top. box-sizing: border-box makes width mean the whole box, so a box set to 200 pixels is 200 pixels on the screen.
Syntax
<selector> {
box-sizing: <box>;
}
| Part | What it does |
|---|---|
<selector> | The elements whose width to change the meaning of. |
<box> | content-box, the default: width is the content only. border-box: width includes the padding and the border. |
MDN's box-sizing page explains both values, with the sums for each.
The Example
The example is two boxes with the same rule: 200 pixels wide, 20 pixels of padding and a 5-pixel blue border. The second also has box-sizing: border-box.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Box sizing</title>
<style>
.box {
width: 200px;
padding: 20px;
border: 5px solid #0f6cbd;
}
.border-box {
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="box">content-box</div>
<div class="box border-box">border-box</div>
</body>
</html>
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- The first box is 250 pixels wide: its 200-pixel content, plus 20 pixels of padding and 5 pixels of border on each side.
- The second box is 200 pixels wide, as its
widthsays. The padding and the border now fit inside the 200 pixels, so its content area shrank to 150.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting width to be the whole box with the default content-box | The box came out 250 pixels wide, not 200 | Add box-sizing: border-box |
| Setting it on one element and expecting its children to follow | box-sizing isn't inherited: MDN's page lists it as not inherited | Set it on every element, as in the FAQ below |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A box is wider than its width | The padding and border are added to it | Use box-sizing: border-box |
| The content area is smaller than expected | border-box takes the padding and border out of the width | That's expected: the box keeps its outer width |
FAQ
How do I apply it to every element?
Use the universal selector, with the ::before and ::after pseudo-elements: *, *::before, *::after { box-sizing: border-box; }. With that rule, a box with the box class was 200 pixels wide without the border-box class.
Which elements are border-box already?
MDN's page lists them: table, select and button, and input elements of the types radio, checkbox, reset, button, submit, color and search.
Conclusion
Set box-sizing: border-box and width means what you see on the screen. Set it on every element at once, and every width on the page reads the same way.
Additional Resources
- box-sizing, on MDN.
