How to Add Space Inside an Element with padding in CSS
padding puts space inside an element, between its content and its edges. The background fills the padding, so the element looks bigger and roomier.
Syntax
<selector> {
padding: <size>;
}
| Part | What it does |
|---|---|
<selector> | The element to pad. |
<size> | The space on all four sides, such as 16px. Two values set top and bottom, then left and right. Four values go clockwise from the top. |
MDN's padding page shows every form of the value.
The Example
The example is two notices on a yellow background. The second has the class roomy, which gives it 16 pixels of padding.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Padding</title>
<style>
p {
background: #fff4ce;
}
.roomy {
padding: 16px;
}
</style>
</head>
<body>
<p>The lift is out of order today.</p>
<p class="roomy">Lunch is at 12:30.</p>
</body>
</html>
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- The second notice is 51 pixels tall, where the first is 19. The padding added 16 pixels above the text and 16 below.
- The text moved 16 pixels in from the left edge, and the yellow fills the padding all around it.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting width to include the padding | Padding is added to the width: a box with width: 200px and padding: 16px was 232 pixels wide | Allow for it, or add box-sizing: border-box so the width includes the padding |
| Using a negative value | Padding can't be negative: padding: -16px was ignored and the padding stayed 0px | Use 0 or a positive value |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The element is wider than its width | The padding is added to the width | Use box-sizing: border-box |
| The padding seems to do nothing | The element has no background or border, so the extra space is hard to see | Give it a background while you check the spacing |
FAQ
What's the difference between padding and margin?
Padding is inside the element, and the background fills it. Margin is outside, between the element and its neighbors, and the background doesn't reach it.
What do two values mean?
Top and bottom first, then left and right: padding: 4px 12px gave 4 pixels at the top and bottom and 12 on the sides.
Conclusion
Use padding for space inside an element, between its content and its edges. Remember that it adds to the element's width unless you use box-sizing: border-box.
Additional Resources
- padding, on MDN.
