日本語

How to Use CSS box-sizing to Include Padding in the Width

​​CSS​HTML,Playwright

By default, the CSS width sets only the content width, and padding and border are added outside it. With box-sizing: border-box, width includes padding and border, so the value you set is the visible width. This fixes cases where a width: 100% input or card overflows its parent to the side, or where four boxes sized at width: 25% wrap onto a new line.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened pages made only of HTML and CSS in Playwright, measured the visible width with getBoundingClientRect(), and measured the content width by subtracting padding from clientWidth. I checked the default styles of form controls in Chromium. I did not test Firefox or Safari.

By default, width does not include padding

The initial value of box-sizing is content-box. As the MDN box-sizing reference explains, width and height in this case refer only to the content area and do not include padding or border. With border-box, width and height include padding and border, so the content area shrinks by that amount. margin is not included in either case.

I compared an element with width: 200px, padding: 20px, and border: 4px solid.

css
.box {
  width: 200px;
  padding: 20px;
  border: 4px solid;
  box-sizing: border-box; /* Change this to content-box to compare */
}
box-sizing Visible width Content width
content-box (initial value) 248px 200px
border-box 200px 152px

With content-box, the 40px of left and right padding and the 8px of border were added to 200px, giving 248px. With border-box, the visible width stayed at 200px, and the content width shrank to 200 − 40 − 8 = 152px.

Reading width with getComputedStyle() in JavaScript gave 200px in both cases. The CSS specification says that with border-box, the value seen through getComputedStyle() also refers to the size including border. Which box a value like this measures depends on box-sizing.

width: 100% plus padding overflows the parent

The content-box mismatch is most visible when padding is added to width: 100%. I placed a child with width: 100% and 20px of left and right padding inside a parent 300px wide.

css
.parent {
  width: 300px;
}

.child {
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
Two diagrams stacked vertically. In the top one, with content-box, the light blue child bar extends past the gray dotted parent (300px wide) on the right; the 40px that overflow are marked by a pink line. The child is 340px wide. In the bottom one, with border-box, the child bar fits exactly inside the dotted parent, and the child is 300px wide
A child with width: 100% and padding: 0 20px inside a parent 300px wide. Only box-sizing was changed.

With content-box, width: 100% made the content area as wide as the parent, 300px, and the 40px of left and right padding were added outside it. The child became 340px wide and overflowed the parent by 40px (the parent's scrollWidth was also 340px). With border-box, the padding fit inside the 300px, and the child fit the parent exactly.

The same thing happens when elements sit side by side. I placed four boxes with width: 25%, 10px of left and right padding, and a 1px border, using float: left, inside a parent 600px wide. With content-box, each box was 150 + 20 + 2 = 172px, so the four together were 688px, more than the parent, and the fourth box wrapped to the next line. With border-box, all four were 150px and fit on one line.

Applying border-box to the whole page

Setting it on each element one by one is tedious, so there is a way to apply it to the whole page at once. First, note that box-sizing is not inherited (the specification gives Inherited: no). When I set border-box only on a parent and checked, a child kept content-box, and a child with width: 100px and 10px of left and right padding was 120px wide.

For that reason, use the universal selector (*) to apply it to every element. When I wrote only *, ordinary elements became border-box, but pseudo-elements created with ::before stayed content-box. You need to include ::before and ::after as well.

css
*,
*::before,
*::after {
  box-sizing: border-box;
}

Another option is to set border-box only on html and have the other elements inherit the value from their parent.

css
html {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

For ordinary elements, both approaches gave border-box. The ::before elements were also border-box in both cases. The difference appears when you switch some element back to content-box. I placed a child with width: 200px and 20px of left and right padding inside an element with .old { box-sizing: content-box; } and compared.

Approach box-sizing of the child inside .old Visible width of the child
border-box on * border-box 200px
border-box on html, inherit on * content-box 240px

With the approach that sets border-box on *, the child has border-box set directly, so switching the parent back to content-box does not affect the child. With the inherit approach, the child inherited content-box from its parent. If you want to keep a set of components that were built assuming content-box, the inherit approach lets you do it with one declaration on the outermost element of each component.

Inputs and textareas are content-box by default in some cases

Some form controls have a default box-sizing set by the browser's built-in styles. On a page with no styles of my own, I read the values from getComputedStyle() in Chromium. For comparison, I also included div and table.

Element Default box-sizing
button, select, table border-box
input of type checkbox, submit, color, search border-box
input of type text, email, number content-box
textarea content-box
div content-box

Buttons and select boxes are border-box from the start, so width is already the visible width. Many input elements that take text, and textarea, are content-box. The MDN reference also lists table, select, and button, and the input types radio, checkbox, reset, button, submit, color, and search, as elements the browser styles with border-box by default.

So if you do not set border-box across the page, only text inputs can overflow. In a parent 300px wide, an <input type="text"> with width: 100%, padding: 8px, and border: 1px solid had a visible width of 318px and overflowed the parent by 18px. Adding box-sizing: border-box made it fit in 300px.

css
input,
textarea {
  box-sizing: border-box;
  width: 100%;
  padding: 8px;
}

It also affects height, min-width, and max-width

box-sizing changes more than width. The specification says it changes how all size properties are interpreted, including height, min-width, and max-width, and flex-basis as well. I measured height with 20px of padding on top, bottom, left, and right, and min-width and max-width with 20px of padding on the left and right.

Setting Visible size with content-box Visible size with border-box
height: 100px Height 140px Height 100px
min-width: 100px (no content) Width 140px Width 100px
max-width: 200px (long text) Width 240px Width 200px

With border-box, each value you set became the visible size as written. When you use max-width: 200px to cap a card's width, you do not need to recalculate for the added padding.

If padding and border add up to more than width, the element is wider than width

Even with border-box, the content width never drops below 0. The specification says that when width minus padding and border is less than 0, the content width is treated as 0. I reduced width on an element with 20px of left and right padding and 2px of border (44px in total).

width Visible width Content width
60px 60px 16px
40px 44px 0px
20px 44px 0px
0 44px 0px

Once width dropped below the 44px total of padding and border, the visible width stopped at 44px. Even width: 0 leaves a 44px box, so with border-box and width: 0, you cannot remove an element that has padding. The MDN reference also says that you cannot hide an element by using border-box.

​Posted by 86