CSS box model and box-sizing

Quick reference for the CSS box model: box-sizing calculations, padding, border, margin, and when vertical margins collapse.

The box model splits a box into content, padding, border, and margin. This page deepens the tutorial chapter “Understand the box model” and keeps that lesson, its worked 280px example, the in-page figure, and the #box-model anchor unchanged.

The tutorial already states the key calculation: with border-box, width includes padding and border but never margin. Here the same rule is written as a property table, a second worked pair, and the conditions for vertical margin collapse.

Examples were parsed for syntax and structure. The pixel arithmetic is from the box-sizing definitions, not from a measured browser render.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

box-sizing calculations

The initial value of box-sizing is content-box. Then width and height set the content box, and padding and border are added outside that width to get the border box.

With box-sizing: border-box, width and height set the border box. Content width is the declared width minus horizontal padding and border. Margin is never part of width in either mode. The tutorial’s .card example (width 280px, padding 20px, border 2px) therefore has a 280px border box and 236px of content: 280 − 40 − 4.

Syntax or propertyInitial / defaultNotes
box-sizingcontent-boxcontent-box or border-box. border-box is commonly set on a universal rule in component work so that width is the size you can see, including padding and border.
width / heightautoUsed size of the content box (content-box) or of the border box (border-box), subject to min-width, max-width, and the formatting context.
padding0Space between the content edge and the border edge. Included in a border-box width; added outside a content-box width.
bordermedium none currentcolorThe border width is part of the border box. A 2px border on both sides removes 4px from content when box-sizing is border-box.
margin0Space outside the border edge. It is never included in width. Vertical margins may collapse in block layout; see the next section.
overflowvisiblevisible, hidden, auto, scroll, or clip. A value other than visible (and the older visible/clip interactions) can change formatting and can also block parent-child margin collapse.

The same width under content-box and border-box

box-sizing: content-box;
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid;

content-box grows outside the declared width. border-box keeps the border box at the declared width.

  • Arithmetic: content-box border box is 200 + 40 + 10 = 250px. border-box content is 200 − 40 − 10 = 150px. Margin is omitted from both.
html
<div class="row">
  <div class="box content">content-box</div>
  <div class="box border">border-box</div>
</div>
<style>
.row { display: flex; gap: 16px; align-items: flex-start; }
.box { width: 200px; padding: 20px; border: 5px solid #0B5853; }
.content { box-sizing: content-box; }
.border { box-sizing: border-box; }
</style>

What this markup means

Both boxes declare width: 200px, padding: 20px, and a 5px border. The content-box border box is 250px wide. The border-box border box stays 200px, with 150px of content. This is the same rule as the tutorial’s 280px card, not a different model.

Related: CSS tutorial: box model, Display

MDN documentation → · CSS specification →

When vertical margins collapse

In a block formatting context, adjoining vertical margins can collapse into a single margin. The used value is the larger of the two, or the difference if one is negative, not the sum. Horizontal margins do not collapse.

Sibling block boxes in normal flow can collapse their meeting vertical margins. A parent and its first or last in-flow block child can also collapse, unless something separates them: padding or border on the parent, a block formatting context on the parent (for example overflow other than visible, or display: flow-root), or clearance.

Margins of flex items and grid items do not collapse with each other or with their container. That is why the tutorial can use gap on a grid or flex container instead of fighting collapsed item margins. Do not assume a 16px + 16px stack will always occupy 32px in ordinary block layout.

Adjoining sibling margins in block layout

margin-bottom: 24px;
margin-top: 16px;

In normal block flow, adjoining vertical sibling margins collapse rather than add.

  • If these two blocks stay in normal flow with no padding or border between them, the collapsed margin is 24px, not 40px. Flex or grid on the parent would prevent that collapse.
html
<div class="stack">
  <p class="one">First block</p>
  <p class="two">Second block</p>
</div>
<style>
.stack { margin: 0; }
.one { margin: 0 0 24px; }
.two { margin: 16px 0 0; }
</style>

What this markup means

The first paragraph’s 24px bottom margin and the second paragraph’s 16px top margin are adjoining in a block formatting context. They collapse to 24px according to the box model. The example does not use flex or grid on .stack, so the flex/grid “margins do not collapse” exception does not apply.

Related: CSS tutorial: box model, Flexbox, Grid

MDN documentation → · CSS specification →