HTML CSS JavaScript SEO Python Posts Privacy About Contact

CSS Box Model

All HTML elements can be considered as boxes. The CSS box model describes the spacing around every element.

Every HTML element is a rectangular box made of four layers:

  1. Content — the text or image inside
  2. Padding — space between content and border (inside)
  3. Border — a line around the padding
  4. Margin — space outside the border (between elements)

Try It — Visualise the Box Model

Preview

Box Model Layers

LayerPropertyWhat it does
Contentwidth, heightThe size of the content area
PaddingpaddingSpace inside the border
BorderborderLine around the element
MarginmarginSpace outside the element

Total Width Formula

Total width = width + left padding + right padding + left border + right border

Tip: Use box-sizing: border-box in your CSS to make width include padding and border automatically — this is much more predictable and is what most modern websites use.