CSS Box Model
Every HTML element is treated like a box in CSS. Understanding content, padding, border and margin is essential for creating clean cards, sections, buttons and layouts.
Content
The actual text, image or element content.
Padding
Space between content and border.
Border
Line around padding and content.
Margin
Space outside the border.
After This Chapter, You Will Understand
This chapter gives you strong control over spacing and element sizing.
Content
Understand the actual area where content appears.
Padding
Control inner spacing inside elements.
Border
Add visible boundary around elements.
Margin
Control outer spacing between elements.
What is CSS Box Model?
The box model explains how every HTML element occupies space on a webpage.
1 Simple Meaning
In CSS, every element is a rectangular box. This box has content, padding, border and margin. These four layers decide how much space the element takes and how far it stays from other elements.
width: 300px;
padding: 20px;
border: 3px solid #0d6efd;
margin: 30px;
}
Content, Padding, Border and Margin
Each layer has a specific purpose in element design.
| Part | Meaning | CSS Property | Example |
|---|---|---|---|
| Content | Actual text, image or inner content | width, height | width: 300px; |
| Padding | Space inside the element, around content | padding | padding: 20px; |
| Border | Line around padding and content | border | border: 2px solid blue; |
| Margin | Space outside the element | margin | margin: 30px; |
Padding and Margin Examples
Padding improves inner comfort. Margin creates distance between elements.
No Padding
Content feels tight and uncomfortable.
Good Padding
Content gets breathing space.
Margin Gap
Margin creates distance from outside.
2 Individual Sides
You can control each side separately.
margin-right: 10px;
margin-bottom: 20px;
margin-left: 10px;
3 Shorthand
Shorthand saves time and keeps CSS shorter.
margin: 10px 20px 30px 40px;
/* top-bottom left-right */
padding: 20px 30px;
content-box vs border-box
box-sizing controls how width and height are calculated.
4 Default: content-box
In the default model, width applies only to content. Padding and border are added extra, so the element becomes bigger than the given width.
width: 300px;
padding: 20px;
box-sizing: content-box;
}
5 Recommended: border-box
In border-box, width includes content, padding and border. This makes layout calculation easier.
box-sizing: border-box;
}
Practice CSS Box Model
Edit padding, margin, border and box-sizing to see the result.
Mistakes Students Should Avoid
Most beginner layout issues happen because margin and padding are confused.
Wrong Habits
- Using margin when padding is needed
- Using padding when spacing between elements is needed
- Forgetting that border adds extra size in content-box
- Adding too much random spacing
- Not using box-sizing: border-box in layouts
Correct Habits
- Use padding for inner spacing
- Use margin for outer spacing
- Use border-box for easier width control
- Keep spacing consistent
- Test card and section spacing on mobile too
Test Your Understanding
Select the correct answers and check your score.
1. Which part contains the actual text or image?
2. Padding is space:
3. Margin is space:
4. Which property controls whether width includes padding and border?
5. Which value is recommended for easier layout sizing?
Remember These Points
Revise these before moving to Chapter 16.
Box Model
Every HTML element is treated as a rectangular box in CSS.
Content
The actual area where text, images or inner content appear.
Padding
Space between content and border.
Border
Line around padding and content.
Margin
Space outside the border.
border-box
Makes width include content, padding and border.