
width: 200px Isn't Always 200px: The Box Model and box-sizing
- كاتب موثّق
محتويات المقال
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
width: 200px makes an element exactly 200px wide on screen, and width: 100% makes it exactly as wide as its parent.
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
box-sizing property decides which of those edges your width and height measure.
content-box: width is the content only
box-sizing is content-box. With it, width and height size only the content area, and padding and border are added outside:
.box { width: 200px; padding: 16px; border: 2px solid; }/* on screen: 200 + 16 + 16 + 2 + 2 = 236px */getComputedStyle reports a width of 200px while offsetWidth, which measures the border box, reports 236.
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
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.
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
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.
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.
.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 */html { box-sizing: border-box;}*,*::before,*::after { box-sizing: inherit;}.card { width: 320px; /* the whole card, padding included */ padding: 16px;}.email { width: 100%; /* the card's content box: 288px */ padding: 12px 16px; border: 1px solid #ccc;}width: 200px is the size on screen | |
width: 100% fills the parent exactly | |
border-box always gives the exact width | |
How to apply it in your project
Pick one model, first thing Put 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. Audit width plus padding Search for rules that set widthorheighttogether withpaddingorborder: full-width inputs, buttons, cards, sidebars. Check each one in the browser after you switch.Fence off legacy widgets For 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.Space items with gap Between flex or grid items, prefer gapover margins. It never collapses and never doubles up between neighbors.
Key takeaways
widthandheightsize the content box by default; padding and border are added on top.box-sizing: border-boxmakes 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 undercontent-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.
التعليقات (0)