Grandma Marni’s packing table: set up a real crate on a real table top, and the browser measures the content, the straw (padding), the planks (border) and its…
Why it matters
"Why is my element wider than I set?" The answer is almost always the box model. Knowing the four layers and box-sizing ends the guessing about why a layout is a few pixels off. Many CSS resets (Tailwind included) put box-sizing: border-box on every element, so you need to know both.
The key idea
box-sizing decides what width counts
Common mistakes
Set width: 100% + padding, the element overflows With content-box, 100% is the content width only, then padding and border are added: 100% + 2×20 + 2×4 is wider than the parent. Use box-sizing: border-box so padding and border fit inside the 100%.
Top/bottom margins "vanish" or merge That is margin collapsing: in block layout, touching vertical margins merge into one and the larger is used (24 and 16 make 24, not 40). Not a box model bug. In a flex or grid container item margins do not collapse; see also the "Collapsing Margins" explorer.
Using margin for spacing between flex/grid items Prefer gap. In flex and grid, margins do not collapse, so two items' bottom + top margins stack (24 + 16 = 40), and the first/last items get margin to the edge too. gap only spaces between items.
Already border-box, yet the box is wider than widthborder-box takes from the content, but content cannot go negative. If padding + border exceed the width (width: 40px with padding 20 and border 4 = 48), the content is 0 and the box stays 48px. Shrink the padding or grow the width.
Try it in this playground
With content-box, raise the padding The crate grows even though width stays: padding is added outside width. Watch the width arithmetic change.
Switch to border-box, raise the padding again The crate stays put and the content shrinks. getComputedStyle().width now reports the width up to the border.
Set width: 100% with content-box The crate hangs off the table by its padding + border. That is the classic "100% input is too long" bug.
Click margin, border, padding, content in the diagram Each layer lights up on the real crate, at the size the browser measured.