The CSS Box Model is one of the most important concepts in CSS.
Every HTML element is treated as a rectangular box.
Understanding the Box Model is essential for:
Every HTML element consists of four layers.
+----------------------+
| Margin |
| +----------------+ |
| | Border | |
| | +------------+ | |
| | | Padding | | |
| | | +--------+ | | |
| | | |Content | | | |
| | | +--------+ | | |
| | +------------+ | |
| +----------------+ |
+----------------------+
Structure:
Content
↓
Padding
↓
Border
↓
Margin
The content area contains the actual content of an element.
Examples include:
Example:
<div>
Hello World
</div>
Here, Hello World is the content.
The width and height properties define the size of the content area.
Width:
div {
width: 300px;
}
Height:
div {
height: 200px;
}
Together:
.card {
width: 300px;
height: 200px;
}
Percentage width:
.container {
width: 100%;
}
Responsive width:
.container {
width: 100%;
max-width: 1200px;
}
This is a common approach for responsive layouts.
Padding is the space inside an element between the content and its border.
Border
│
Padding
│
Content
Example:
.box {
padding: 20px;
}
Without padding:
+------------+
|Hello World |
+------------+
With padding:
+------------------+
| |
| Hello World |
| |
+------------------+
.box {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}
One value:
padding: 20px;
Two values:
padding: 10px 20px;
Top & Bottom → 10px
Left & Right → 20px
Four values:
padding: 10px 20px 30px 40px;
Top → 10px
Right → 20px
Bottom → 30px
Left → 40px
Margin is the space outside an element.
It creates space between neighboring elements.
Element
↓
Margin
↓
Other Element
Example:
.card {
margin: 20px;
}
.card {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
}
margin: 20px;
A common technique is:
.container {
width: 500px;
margin: 0 auto;
}
This centers the element horizontally.
The border surrounds the content and padding.
Syntax:
border:
width
style
color;
Example:
.card {
border: 2px solid black;
}
Width:
border-width: 2px;
Style:
border-style: solid;
Color:
border-color: blue;
Common border styles:
Examples:
border: 2px solid black;
border: 2px dashed black;
border: 2px dotted black;
The border-radius property creates rounded corners.
Example:
.card {
border-radius: 10px;
}
Circle:
.avatar {
width: 100px;
height: 100px;
border-radius: 50%;
}
An outline is a line drawn outside the border.
Unlike borders:
Syntax:
outline:
width
style
color;
Example:
button {
outline: 2px solid blue;
}
Visual:
Outline
↓
+-----------+
| Border |
| Content |
+-----------+
| Border | Outline |
|---|---|
| Takes space | No extra space |
| Part of Box Model | Outside Box Model |
| Supports border-radius | Limited radius support |
| Common for layout | Common for focus states |
Example:
input:focus {
outline: 3px solid blue;
}
Outlines are commonly used for accessibility.
The box-shadow property adds shadow effects around elements.
Syntax:
box-shadow:
horizontal
vertical
blur
spread
color;
Example:
.card {
box-shadow:
0 4px 10px rgba(0,0,0,0.2);
}
Breakdown:
0px → Horizontal Offset
4px → Vertical Offset
10px → Blur Radius
rgba → Shadow Color
Light shadow:
box-shadow:
0 2px 5px rgba(0,0,0,0.1);
Medium shadow:
box-shadow:
0 5px 15px rgba(0,0,0,0.2);
Strong shadow:
box-shadow:
0 10px 30px rgba(0,0,0,0.3);
Multiple shadows:
box-shadow:
0 2px 5px rgba(0,0,0,0.1),
0 10px 20px rgba(0,0,0,0.2);
Modern websites almost always use:
* {
box-sizing: border-box;
}
.box {
width: 300px;
padding: 20px;
}
Actual width:
300 + 20 + 20
= 340px
.box {
box-sizing: border-box;
}
Actual width:
300px Total
Padding Included
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
HTML:
<div class="card">
Frontend Development
</div>
CSS:
.card {
width: 300px;
padding: 20px;
margin: 20px;
border: 1px solid #ddd;
border-radius: 10px;
box-shadow:
0 4px 12px rgba(0,0,0,0.1);
}
.card {
background: white;
width: 350px;
padding: 24px;
border-radius: 12px;
box-shadow:
0 4px 20px rgba(0,0,0,0.08);
}
This style is commonly used for:
Use box-sizing: border-box.
* {
box-sizing: border-box;
}
Use padding for internal spacing.
padding: 20px;
Use margin to separate elements.
margin: 20px;
Prefer subtle shadows.
box-shadow:
0 4px 12px rgba(0,0,0,0.1);
Use rounded corners for modern interfaces.
border-radius: 10px;
Every HTML element follows the Box Model.
Margin
↓
Border
↓
Padding
↓
Content
Example:
.card {
width: 300px;
padding: 20px;
margin: 20px;
border: 1px solid #ddd;
box-shadow:
0 4px 10px rgba(0,0,0,0.1);
}
Understanding the Box Model is essential for creating clean layouts, proper spacing, responsive designs, and modern user interfaces.