CSS
How Do I Add Padding to an Element in the CSS Box Model?
When working with CSS layouts, controlling the spacing inside elements is just as important as spacing between them. That’s where padding comes in — a key part of the CSS Box Model.
In this post, you’ll learn exactly how to add padding to elements and how it affects their size and appearance.
🧱 What Is Padding in the CSS Box Model?
The CSS Box Model breaks every element into four parts:
- Content – The actual text, image, or content.
- Padding – Space between the content and the element’s border.
- Border – The edge around the element.
- Margin – Space outside the element, between it and others.
Padding adds internal space inside the border but outside the content.
🧪 How to Add Padding in CSS
✅ Basic Syntax:
selector {
padding: value;
}
📌 Example:
.card {
padding: 20px;
}
✅ Adds 20px of space on all four sides (top, right, bottom, and left) of the .card
element’s content.
🔄 Padding Shorthand Variations
You can control each side individually using these shorthand formats:
Syntax | Meaning |
---|---|
padding: 20px; | All sides get 20px |
padding: 20px 10px; | Top & bottom: 20px, Left & right: 10px |
padding: 20px 15px 10px; | Top: 20px, Left & right: 15px, Bottom: 10px |
padding: 20px 15px 10px 5px; | Top, Right, Bottom, Left (clockwise) |
🎯 Individual Side Properties
You can also set each side specifically:
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
📐 How Padding Affects Element Size
By default (box-sizing: content-box
), padding increases the total size of the element.
Example:
.box {
width: 200px;
padding: 20px;
}
- Content width: 200px
- Total width becomes:
200 + 20×2 = 240px
🛠 Use box-sizing: border-box
to Avoid Size Increase
To keep the total size fixed, even with padding, use:
.box {
width: 200px;
padding: 20px;
box-sizing: border-box;
}
✅ Now, padding is included inside the defined width, keeping layout predictable.
🧾 Summary
Task | CSS Code Example |
---|---|
Add equal padding | padding: 20px; |
Add top and bottom padding | padding: 10px 0; |
Padding without affecting size | box-sizing: border-box; |
🧠 Conclusion
Adding padding in CSS is a simple but powerful way to control internal spacing within elements. Whether you’re designing buttons, cards, or containers, padding helps create visual clarity and breathing room for content.
Use box-sizing: border-box
if you want your padding not to affect the total size of the element.
Pro Tip: Want consistent spacing throughout your site? Use CSS custom properties (--padding-base
) to manage padding values efficiently.