CSS Box Model
31 March 2025 | Category: CSS
The CSS Box Model defines how elements are displayed and sized on a webpage. It consists of four layers:
- Content – The actual text or image inside the element.
- Padding – Space between content and border.
- Border – Surrounds the padding and content.
- Margin – Space between this element and other elements.
1️⃣ Understanding the CSS Box Model
Every HTML element is treated as a box in CSS. The total size of an element is calculated as:
Total Width = Width + Padding (left + right) + Border (left + right) + Margin (left + right)
Total Height = Height + Padding (top + bottom) + Border (top + bottom) + Margin (top + bottom)
Visual Representation
+--------------------------+ ← Margin
| |
| +--------------------+ | ← Border
| | | |
| | +------------+ | | ← Padding
| | | Content | | |
| | +------------+ | |
| | | |
| +--------------------+ |
| |
+--------------------------+
2️⃣ CSS Box Model Properties
Property | Description |
---|---|
width / height | Defines the content width & height |
padding | Creates space inside the border |
border | Defines the outline of the box |
margin | Creates space outside the box |
Example:
.box {
width: 200px;
height: 100px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}
📌 Total width = 200px + (10px * 2) + (5px * 2) + (20px * 2) = 270px
📌 Total height = 100px + (10px * 2) + (5px * 2) + (20px * 2) = 170px
3️⃣ box-sizing
Property
By default, width
and height
only apply to the content, not padding or border. To make sure the total size includes padding and border, use:
.box {
box-sizing: border-box;
}
Comparison:
Property | Effect |
---|---|
box-sizing: content-box; | Default behavior – padding & border are added outside width & height |
box-sizing: border-box; | Width & height include padding & border (recommended) |
✅ Best practice: Always use box-sizing: border-box;
for consistent sizing.
4️⃣ Padding in Detail
padding
defines the space inside the border.
.box {
padding: 20px; /* Applies to all sides */
}
📌 You can set different values for each side:
.box {
padding: 10px 15px 20px 25px; /* top right bottom left */
}
or
.box {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
}
5️⃣ Border in Detail
The border
property defines the outline of the element.
Example:
.box {
border: 5px solid red;
}
Border Type | Example |
---|---|
solid | border: 2px solid black; |
dashed | border: 2px dashed red; |
dotted | border: 2px dotted blue; |
double | border: 4px double green; |
none | border: none; |
6️⃣ Margin in Detail
margin
defines external spacing between elements.
.box {
margin: 20px; /* Applies to all sides */
}
📌 You can also set individual sides:
.box {
margin: 10px 15px 20px 25px; /* top right bottom left */
}
or
.box {
margin-top: 10px;
margin-right: 15px;
margin-bottom: 20px;
margin-left: 25px;
}
🔹 Auto Margin (Centering Elements)
.box {
width: 300px;
margin: 0 auto;
}
📌 This centers the element horizontally.
7️⃣ Collapsing Margins
When two vertical margins meet, the larger one takes effect instead of adding up.
.box1 {
margin-bottom: 50px;
}
.box2 {
margin-top: 30px;
}
📌 Instead of 50px + 30px = 80px
, the margin will be 50px
(the larger one).
8️⃣ Overflow Handling
If content exceeds the box size, use overflow
:
Property | Effect |
---|---|
visible | Content overflows (default) |
hidden | Extra content is hidden |
scroll | Adds scrollbars |
auto | Adds scrollbars only when needed |
Example:
.box {
width: 100px;
height: 50px;
overflow: hidden;
}
9️⃣ Summary Cheat Sheet
Property | Effect |
---|---|
width & height | Defines element’s size |
padding | Space inside the element |
border | Outline of the box |
margin | Space outside the element |
box-sizing: border-box; | Ensures padding & border are included in width |
margin: auto; | Centers the element horizontally |
overflow: hidden; | Hides overflowing content |
🎯 Conclusion
✅ CSS Box Model defines how elements are sized and spaced
✅ box-sizing: border-box;
simplifies layout calculations
✅ margin
, padding
, and border
affect spacing and size
✅ Understanding these avoids layout issues