Box Model
In one line
The box model describes how every HTML element is a rectangular box with content, padding, border, and margin.
In simple words
Every HTML element on a page is a rectangular box. The CSS box model describes the parts of this box: the content area (where text or images go), padding (space inside the border), border (the edge), and margin (space outside the border).
The total size of an element is: content + padding + border + margin. By default (box-sizing: content-box), the width property only sets the content width, so padding and border add to the total. Setting box-sizing: border-box includes padding and border in the declared width, which is usually more intuitive.
Understanding the box model is essential for controlling spacing, alignment, and sizing in CSS. Most CSS resets set box-sizing: border-box on all elements to make sizing more predictable.
Example
div {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
/* Total width with border-box: 200px (content+padding+border) */
/* Plus 20px margin (10px each side) outside the border */With border-box, the 200px width includes content, padding, and border. Margin is always outside.
Related terms
Related Resources
Related Lessons
- CSS Box Model
Learn the box model.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.