Box Model
The rule that every element on a web page is a rectangle of content wrapped in padding, border, and margin, and the two quirks, box sizing and collapsing margins, behind most surprises in CSS spacing.
The box model is how CSS describes the space an element occupies. Every element on a web page generates a rectangular box, and every box has four layers, from the inside out:
- Content. The text, image, or child elements themselves.
- Padding. Space between the content and the border. It takes the element's background color.
- Border. A line around the padding, possibly zero width.
- Margin. Transparent space outside the border that pushes neighboring boxes away.
Browser developer tools draw this as nested rectangles, which answers most "where did this gap come from" questions. Layout, the step of the Critical Rendering Path that computes every box's position and size, works entirely in these terms.
Box sizing is the first quirk. By default, width: 200px sets the width of the content only, so adding 20 pixels of padding and a 1-pixel border on each side makes the element 242 pixels wide. That rule, content-box, makes it hard to fit elements to a Grid System or give columns percentage widths that add up to 100. Old versions of Internet Explorer measured width including padding and border, which was technically wrong but easier to reason about. The box-sizing: border-box property later made that behavior available everywhere, and most stylesheets now apply it to every element in their first lines.
Margin collapsing is the second. When two vertical margins touch, such as the bottom margin of one paragraph and the top margin of the next, they don't add. The larger one wins, so 20 pixels above and 30 below produce 30, not 50. This was designed so text spacing doesn't double. It surprises people in other cases, such as a child's top margin escaping through its parent and pushing the parent down. Margins inside Flexbox and CSS Grid containers never collapse, one reason many developers now space elements with the gap property rather than margins.
Other rules shape the box:
- Display type. Block boxes stack vertically and fill the available width. Inline boxes flow within a line of text and ignore vertical margins and set heights.
- Outlines and shadows draw outside the border without taking space, so a focus outline never shifts layout.
- Logical properties such as
margin-inline-startreplace left and right with start and end, so spacing flips correctly for right-to-left languages.
Spacing, in design terms white space, is mostly decided through padding, margins, and gaps. Design systems usually restrict these to a fixed scale so spacing stays consistent; see Design Token. Utility-class frameworks expose the same scale as short class names; see Utility-First CSS.
Boxes also determine painting order and overlap: positioned boxes can form a Stacking Context, and the Cascade decides which box properties apply in the first place.
See also1
Linked from8
- CascadeDesign & Interface
The algorithm deciding which declaration wins when several apply to the same element.
- Cascading Style SheetsWeb Platform
The language that describes how web pages look, proposed in 1994 to separate presentation from structure, nearly sunk by inconsistent browser support, and grown since into a layout and animation system that does work once reserved for scripts.
- Critical Rendering PathWeb Platform
The sequence of steps between receiving HTML and painting pixels, and everything that blocks it.
- CSS GridDesign & Interface
The CSS layout mode for rows and columns at once, which brought the designer's layout grid into the browser and lets responsive layouts adapt with no breakpoints at all.
- Design TokenDesign & Interface
Named design decisions — color, spacing, radius, type — stored as data so they can be shared and re-themed.
- FlexboxDesign & Interface
The CSS layout mode for arranging items along one axis, distributing space and aligning them without floats or hacks, which finally made vertical centering trivial and powers most component layouts.
- Grid SystemDesign & Interface
A shared set of columns, gutters, and alignment lines that constrains where content can sit, so unrelated layouts still read as one system.
- Utility-First CSSDesign & Interface
Composing styles from many small single-purpose classes rather than authoring semantic rules.