Skip to main content

How to Include Padding and Border in an Element's Width with box-sizing in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<selector> {
box-sizing: <box>;
}
PartWhat 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:

index.html, an HTML page with a style element holding a rule for the class box with a width of 200px, 20px of padding and a 5px solid 0f6cbd border, and a rule for the class border-box with box-sizing border-box, and two div elements, the second with both classes.

index.html
<!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 page in Chromium: two boxes with thick blue borders, the first, content-box, visibly wider than the second, border-box.

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 width says. The padding and the border now fit inside the 200 pixels, so its content area shrank to 150.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting width to be the whole box with the default content-boxThe box came out 250 pixels wide, not 200Add box-sizing: border-box
Setting it on one element and expecting its children to followbox-sizing isn't inherited: MDN's page lists it as not inheritedSet it on every element, as in the FAQ below

Troubleshooting​

SymptomLikely causeFix
A box is wider than its widthThe padding and border are added to itUse box-sizing: border-box
The content area is smaller than expectedborder-box takes the padding and border out of the widthThat'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​