The Box Model
The CSS Box Model
The CSS Box Model is a fundamental concept that every web developer must understand. It describes how every HTML element is rendered as a rectangular box, and how the space around and within that box is calculated.
What is the Box Model?
Every element in HTML is treated as a box. This box consists of four layers, from inside to outside:
- Content - The actual content of the element (text, images, etc.)
- Padding - The space between the content and the border
- Border - A line that surrounds the padding
- Margin - The space outside the border, separating the element from other elements
Interactive Box Model Demo
Use the interactive demo below to explore how each property affects the total size of an element. Adjust the sliders and watch how the box changes in real-time.
>CSS Box Model
Adjust the sliders to see how margin, border, padding, and content interact.
/* Box Sizing */
box-sizing: content-box;
/* Dimensions */
width: 120px; /* content width */
height: 80px;
/* Padding */
padding: 20px 20px 20px 20px;
/* Border */
border-width: 4px 4px 4px 4px;
border-style: solid;
/* Margin */
margin: 24px 24px 24px 24px;Understanding Each Layer
Content
The content area is where your text, images, or other content lives. You control its size with the width and height properties.
.element { width: 200px; height: 100px;}Padding
Padding creates space inside the element, between the content and the border. It's useful for giving content some "breathing room."
.element { /* All sides */ padding: 20px;
/* Individual sides */ padding-top: 10px; padding-right: 15px; padding-bottom: 10px; padding-left: 15px;
/* Shorthand: top right bottom left */ padding: 10px 15px 10px 15px;
/* Shorthand: vertical horizontal */ padding: 10px 15px;}Border
The border surrounds the padding and content. It can have different styles, widths, and colors.
.element { border: 2px solid #333;
/* Or specify each side */ border-top: 1px dashed blue; border-right: 2px solid green;}Margin
Margin creates space outside the element, separating it from neighboring elements.
.element { /* All sides */ margin: 20px;
/* Center horizontally */ margin: 0 auto;
/* Individual sides */ margin-top: 10px; margin-bottom: 20px;}Calculating Total Size
By default (with box-sizing: content-box), the total width of an element is:
Total Width = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right
This can make layouts tricky because setting width: 100px doesn't mean the element takes up exactly 100px.
The box-sizing Property
The box-sizing property changes how the total width and height are calculated:
- content-box (default): Width and height only apply to the content area
- border-box: Width and height include padding and border
/* Recommended: Apply to all elements */*, *::before, *::after { box-sizing: border-box;}With border-box, if you set width: 100px, the element will be exactly 100px wide, including padding and border. This makes layouts much more predictable.
Pro Tip
Most CSS frameworks and resets include box-sizing: border-box by default. It's considered a best practice for modern web development.
Margin Collapsing
One quirk of the box model is margin collapsing. When two vertical margins touch, they collapse into a single margin equal to the larger of the two.
.box1 { margin-bottom: 20px;}
.box2 { margin-top: 30px;}
/* The gap between them is 30px, not 50px! */This only happens with vertical margins, not horizontal ones.
Summary
- Every element is a box with content, padding, border, and margin
- Use
box-sizing: border-boxfor more intuitive sizing - Padding is inside the border, margin is outside
- Vertical margins collapse when they touch
Understanding the box model is essential for creating precise layouts and debugging spacing issues in CSS.
Lesson Complete!
Great job! You've finished this lesson. Ready to continue?