Canddi

Concise CSS Box Model Guide

The CSS box model describes the space occupied by content, padding, borders and margins. Understanding those layers helps explain why an element can appear wider than the width you assigned to it.

The four layers

  • Content: the area containing text, images or other content.
  • Padding: space between the content and border. The element’s background normally paints through this area; padding does not inherit a separate background colour.
  • Border: the edge around the padding and content, with its own width, style and colour.
  • Margin: transparent spacing outside the border.
Content sits inside padding, then the border, with transparent margin outside. Schematic, not to scale.
The shaded margin identifies the region in this diagram; an actual CSS margin remains transparent.

A worked content-box example

Try this rule on a block element with class box. The explicit box-sizing value makes the calculation predictable even if your stylesheet uses a different default.

.box {
  box-sizing: content-box;
  width: 250px;
  height: 100px;
  padding: 10px;
  border: 5px solid black;
  margin: 10px;
  background-color: red;
}

With these values, the content is 250 pixels wide and 100 pixels high. Each side contributes 10 pixels of padding and 5 pixels of border:

  • Border-box width: 250 + 10 + 10 + 5 + 5 = 280 pixels.
  • Border-box height: 100 + 10 + 10 + 5 + 5 = 130 pixels.
  • Width including both margins: 280 + 10 + 10 = 300 pixels.

These are the dimensions for this simple example without layout constraints overriding them. Do not assume vertical margins always add together: neighbouring block margins can collapse in normal flow. For more on the layers and margin behaviour, see MDN’s box-model guide.

What changes with border-box?

Change the first declaration to box-sizing: border-box;. The declared width and height now include padding and borders, but still exclude margins. Keeping all other values from the example gives:

The same declarations under two sizing rules
Measurement content-box border-box
Content width 250px 220px
Border-box width 280px 250px
Content height 100px 70px
Width with margins 300px 270px

The content width becomes 250 minus 20 pixels of padding and 10 pixels of border: 220 pixels. This makes border-box useful when an element must fit a planned width. See the MDN box-sizing reference for its definition and examples.

Check a box in your browser

Inspect the element in your browser’s developer tools and find its computed box-model display. Compare the content dimensions, padding, border and margin with the values you expected. Check which rule supplies box-sizing before changing widths at random.

Common pitfalls:

  • A fixed height can leave too little space when text wraps. Let text containers grow unless you have a clear reason to constrain them.
  • A width of 100% plus padding can overflow its parent under content-box.
  • Ordinary non-replaced inline elements, such as a text span, do not respond to width and height in the same way as this block example. Consider the element’s display behaviour.

Put the box model into practice

Try changing one value at a time in a small practice page, then compare the computed dimensions. Continue with our HTML and CSS list tutorial, or contact Academy Class to discuss web-design training for your goals.