Home / Box Model & Sizing

How the CSS Box Model Works: A Visual Guide to Page Structure

September 27, 2026 ·

how css box model works

Every element on a web page occupies a rectangular area. Even text that appears to flow freely is built from line boxes, and every heading, image, button, and paragraph sits inside its own block-level or inline-level box. Understanding this structure is the foundation of CSS layout because it explains why elements take up space, why they sit apart from one another, and why a simple width change can make a row overflow.

The four layers of a box

The CSS box model describes each element as a set of nested layers. At the center is the content area: the text, image, or child elements the box contains. Surrounding the content is padding, an inner space that pushes the border away from the content. Outside the padding is the border, which draws the visible edge of the box. Finally, margin is the outer space between this box and neighboring boxes.

These layers serve different jobs. Padding belongs to the element itself and usually keeps its content from touching its edge. Borders belong to the element’s visual design. Margins belong to the relationship between elements, creating separation without changing the appearance of either box.

A simple visual example

Imagine an article card with 20 pixels of content width on each side, 16 pixels of padding, a 2-pixel border, and 12 pixels of margin. Inside the card, the text begins 16 pixels from the border. The border sits 16 pixels beyond the content. The next card starts 12 pixels after this card’s border. Each layer has a distinct purpose, and removing one changes the structure rather than merely the decoration.

When you inspect an element in a browser, the box model diagram usually shows these measurements together. Reading it from the inside out helps you predict how much room the element will occupy and where its visible edges will fall.

How sizing calculations add up

The total space an element consumes is not simply its declared width. In the standard content-box model, the width and height properties apply only to the content area. Padding and border are added outside that area.

For a horizontal box, the total outer width before margin is:

content width + left padding + right padding + left border + right border

Margins are then added outside that result to find the distance from the element’s outer edge to the surrounding layout. This distinction matters when several boxes share a row. If each child is wider than the available space after its padding and border, the row will wrap or overflow.

Consider a column with a declared width of 300 pixels, 24 pixels of horizontal padding on each side, and a 1-pixel border on each side. Its content box is 300 pixels wide, but its border box is 350 pixels wide: 300 + 24 + 24 + 1 + 1. If two such columns sit inside a 700-pixel container, they need 700 pixels together, leaving no room for the gap between them. The layout looks correct in isolation but fails in context.

box-sizing changes the question

The box-sizing property changes which box the width and height values describe. The default value, content-box, measures the content area. The alternative, border-box, includes content, padding, and border in the declared dimensions.

With border-box, a box declared as 300 pixels wide remains 300 pixels wide at its outer border edge, even when padding and border are added. The content area becomes smaller to make room for those layers. This behavior is often easier to reason about when building grids, sidebars, and responsive components because the outer size stays predictable.

Border-box does not make padding or border free. It only changes where the measurement starts. A box with a fixed width and large padding can still have very little room for text, so the content may wrap more often or become cramped. The calculation is still happening; it is simply absorbed inside the declared size.

Choosing a sizing strategy

Many projects apply box-sizing: border-box broadly to make component dimensions easier to control. Even with that convention, it helps to calculate the actual content space before choosing padding. A useful habit is to ask two questions: what is the element’s outer size, and how much room must remain for its content?

For fixed-height elements, the same logic applies vertically. Padding, borders, and content must fit inside the chosen height, or the content will overflow. For elements whose height should follow the content, allowing the browser to calculate height is usually safer than forcing a value.

Where margins behave differently

Margins do not add visual space inside an element; they separate it from adjacent boxes. Vertical margins between block elements can collapse. When two margins meet, the larger one may win instead of both distances adding together. This is why paragraph spacing in CSS can look different from a simple sum of top and bottom margins.

Margin collapsing does not occur in every situation. It can be affected by padding, borders, positioning, flex layout, and grid layout. In flex and grid containers, items are placed according to the container’s rules, so the outer gaps you see are often controlled by gap properties as well as margins.

Negative margins are also possible. They can pull boxes closer together or offset an element, but they do not remove the underlying box structure. They change relationships between boxes rather than shrinking the content, padding, or border layers.

How box sizing changes page structure

Sizing decisions influence the entire page, not just one component. If a header, navigation list, and content column each use content-box widths that seem reasonable alone, their combined size may exceed the viewport. Adding padding to a card can then cause a grid to wrap unexpectedly. Switching to border-box can stabilize the outer dimensions, but the internal content still needs enough space.

Responsive layouts make this more visible. A two-column design may become a single column when the available width is too small for both boxes, their padding, borders, and gaps. The breakpoint is not only about screen width; it is about the total width demanded by the boxes and the spaces between them.

A practical check

When a layout does not fit, inspect one element at a time. Confirm whether the declared width refers to the content box or the border box. Add the horizontal padding and border to find the outer width. Include margins and gaps to find the space required in the row. Then compare that total with the container’s available width.

This process turns layout surprises into measurable decisions. The CSS box model is not merely a diagram in the developer tools; it is the set of rules that determines how content, padding, border, and margin combine to give a page its shape.

Related reading