الانتقال إلى المحتوى
FrontendHTML و CSS· 6 دقائق للقراءة

width: 200px Isn't Always 200px: The Box Model and box-sizing

By default, width sizes only the content; padding and border are added on top of it.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

Mobile users report that the checkout page scrolls sideways. The culprit is one email input inside a 320px card: width: 100%, padding: 12px 16px and a 1px border. The developer read width: 100% as "exactly as wide as the card", but the input renders 354px wide and its right edge sits 18px past the card. The browser isn't buggy. It is doing exactly what the CSS says.

The common misconception

The belief: width: 200px makes an element exactly 200px wide on screen, and width: 100% makes it exactly as wide as its parent.

It's what the word "width" suggests, and it's often true: a box with no padding and no border really is 200px wide. Many projects also switch every element to border-box in their base stylesheet (MDN calls it a common choice among developers), so people who learned CSS inside such a codebase never met the default. The first time they write CSS without that reset, in a new landing page, an email template or an embedded widget, the numbers stop adding up.

What actually happens

Every box has four layers

Every element generates a box with four nested areas: the content, the padding around it, the border around that, and the margin outside the border. This is the . MDN's guide is explicit that the box stops at the border: margin changes how much space the box takes on the page, but it isn't part of the box's size.

The box-sizing property decides which of those edges your width and height measure.

content-box: width is the content only

The initial value of box-sizing is content-box. With it, width and height size only the content area, and padding and border are added outside:

CSS box.css
.box { width: 200px; padding: 16px; border: 2px solid; }/* on screen: 200 + 16 + 16 + 2 + 2 = 236px */

Rendered in headless Chromium, getComputedStyle reports a width of 200px while offsetWidth, which measures the border box, reports 236.

Percentages follow the same rule. For a box in normal flow, width: 100% resolves against the parent's content box, and then your padding and border are added. In the checkout card the content box is 320px, so the input's content is 320px and its border box is 320 + 32 + 2 = 354px.

border-box: width is the visible box

With box-sizing: border-box, the length you write applies to the border box. Padding and border move inside it, and the content gets what's left: 200 − 32 − 4 = 164px. The CSS Box Sizing spec applies this to all sizing properties, so min-width, max-width and flex-basis follow the same choice.

There's one limit: the content box can't be negative. The spec floors it at zero, so a box with width: 20px, 16px of padding on each side and a 2px border renders 36px wide, not 20px. border-box doesn't guarantee a size; it changes what the number measures.

Margins are extra, and vertical ones can collapse

Neither model puts margin inside the width. Vertical margins also have a rule of their own in normal block flow: when they touch, they into one margin the size of the larger. An alert with margin-bottom: 24px above a form with margin-top: 16px leaves a 24px gap, not 40px. Margins don't collapse inside a flex or grid container, so the same two elements in a column flexbox render 40px apart.

Inline boxes add one more surprise: MDN notes that width, height and vertical margins have no effect on them. A span with width: 200px stays as wide as its text until you give it display: inline-block.

Here is the checkout card, before and after:

CSS checkout.css
.card {  width: 320px;  padding: 16px;}.email {  width: 100%; /* 100% of the card's content: 320px */  padding: 12px 16px;  border: 1px solid #ccc;}/* .email renders 354px wide: 18px past the card */

Rendered in headless Chromium, the fixed version gives a 320px card and a 288px input that ends 16px inside the card's edge. Notice that the reset changed the card too: its 320px now includes its padding, so the card itself got 32px narrower. That's the real cost of switching an existing project, and the reason to do it early.

MythReality
width: 200px is the size on screenBy default it's the content box; padding and border are added (236px above)
width: 100% fills the parent exactlyIt's 100% of the parent's content box, plus your own padding and border
border-box always gives the exact widthThe content box is floored at zero, so large padding still grows the box
Margin is part of the element's sizeThe box stops at the border; margin is space outside it
Two vertical margins add upIn block flow, touching vertical margins collapse to the larger one

How to apply it in your project

  1. Pick one model, first thingPut the reset at the top of the first stylesheet you load, before any component styles, so every rule after it is written against the same math.
  2. Audit width plus paddingSearch for rules that set width or height together with padding or border: full-width inputs, buttons, cards, sidebars. Check each one in the browser after you switch.
  3. Fence off legacy widgetsFor third-party or old components that assume the default, add one rule on their root element: box-sizing: content-box. Their insides keep working while the rest of the app moves on.
  4. Space items with gapBetween flex or grid items, prefer gap over margins. It never collapses and never doubles up between neighbors.

Key takeaways

  • width and height size the content box by default; padding and border are added on top.
  • box-sizing: border-box makes the length mean the border box for every sizing property, but the content box never goes below zero.
  • Percentages resolve against the parent's content box, so width: 100% plus padding overflows under content-box.
  • Margins are never part of the box, and touching vertical margins in block flow collapse to the larger one.
  • Choose the box model once, globally and early, and let legacy widgets opt out through inherit.

📚 المصادر

  1. The box model — MDN
  2. box-sizing — MDN
  3. Mastering margin collapsing — MDN
  4. CSS Box Sizing Module Level 3: the box-sizing property — W3C

التعليقات (0)