More CSS answers

by Danny
Published 2 min read

By default, width sets the content box only, so padding and borders are added on top and push the element past its parent. box-sizing: border-box fixes it.

The short answer

#

By default, width sets the size of the content area only. Padding and borders are then added on top, so width: 100% plus padding: 1rem is wider than the parent. Switch to box-sizing: border-box so that width includes padding and borders. Most projects set it globally.


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

Why it happens

#

Every element is a box with four layers: content, padding, border and margin. The box-sizing property decides which of those layers width and height measure.

The default value is content-box. With it, the browser works out the rendered width like this:


_10
.input {
_10
width: 100%; /* 300px if the parent is 300px wide */
_10
padding: 0 16px; /* + 32px */
_10
border: 1px solid; /* + 2px */
_10
}
_10
/* Rendered width: 334px, so 34px spills out of the parent */

With border-box, the same rules give a rendered width of exactly 300px. The browser takes the padding and border out of the 300px, and the content area shrinks to 266px.

The overflow is most obvious on form fields, because inputs and textareas usually have padding and a border, and are often set to width: 100%. It also shows up on cards and grid items with padding.

How to fix it

#

Set border-box on everything

This is the standard fix and is part of almost every CSS reset. Put it near the top of your stylesheet.


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

Including the pseudo-elements matters, because ::before and ::after are separate boxes and do not inherit box-sizing by default.

Some teams prefer an inheritable version, so a third-party widget that expects content-box can opt out for its whole subtree:


_13
html {
_13
box-sizing: border-box;
_13
}
_13
_13
*,
_13
*::before,
_13
*::after {
_13
box-sizing: inherit;
_13
}
_13
_13
.legacy-widget {
_13
box-sizing: content-box;
_13
}

Tailwind's Preflight and most popular resets already apply border-box globally, so if you use one of them, you may only see this issue in code that sits outside it.

Do not set a width at all

Block elements such as div, section and p already fill the width of their parent, minus their own margins, padding and borders. Setting width: 100% on them adds nothing and can cause this overflow. Remove the width and let them stretch.


_10
.card {
_10
/* width: 100%; not needed */
_10
padding: 1.5rem;
_10
}

Inputs, buttons and images are different, because they size to their content by default. They are where width: 100% and border-box really matter.

Use calc() for one-off cases

If you cannot change box-sizing (for example in a style you do not control), subtract the extra space yourself.


_10
.input {
_10
width: calc(100% - 34px);
_10
padding: 0 16px;
_10
border: 1px solid #999;
_10
}

This works but is brittle: change the padding and you must update the calc() too. Prefer border-box.

Common pitfalls

#
  • Margins are never included. border-box covers padding and borders only. width: 100% plus margin: 0 1rem still overflows. Remove the width, or use calc().
  • Forgetting pseudo-elements. * { box-sizing: border-box; } alone does not reach ::before and ::after.
  • Checking the wrong element. Use your browser's DevTools box model panel. It shows the content, padding, border and margin sizes, so you can see which layer is adding width.
  • Overflow from long content. If the box is the right size but text or a URL pokes out, that is a different problem. Try overflow-wrap: anywhere or min-width: 0 on flex children.
#

Was this resource helpful?