---
title: "Why does width: 100% plus padding overflow its parent?"
description: "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."
url: https://www.danny.engineering/answer/css/why-does-width-100-plus-padding-overflow-its-parent
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["CSS"]
---

# Why does width: 100% plus padding overflow its parent?

## 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.


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


## 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:


```css
.input {
  width: 100%;          /* 300px if the parent is 300px wide */
  padding: 0 16px;      /* + 32px */
  border: 1px solid;    /* + 2px */
}
/* 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.


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


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:


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

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

.legacy-widget {
  box-sizing: content-box;
}
```


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.


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


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.


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


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.

## Related reading

- [MDN: box-sizing](https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing)
- [MDN: Introduction to the CSS box model](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model)
