Skip to main content

How to Add Space Inside an Element with padding in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<selector> {
padding: <size>;
}
PartWhat 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:

index.html, an HTML page with a style element holding a rule for p with the background fff4ce and a rule for the class roomy with a padding of 16px, and two paragraphs, the second with the class roomy.

index.html
<!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 page in Chromium: a thin yellow band with The lift is out of order today., and below it a taller yellow box with Lunch is at 12:30. set in from its edges.

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​

MistakeWhat happensDo this instead
Expecting width to include the paddingPadding is added to the width: a box with width: 200px and padding: 16px was 232 pixels wideAllow for it, or add box-sizing: border-box so the width includes the padding
Using a negative valuePadding can't be negative: padding: -16px was ignored and the padding stayed 0pxUse 0 or a positive value

Troubleshooting​

SymptomLikely causeFix
The element is wider than its widthThe padding is added to the widthUse box-sizing: border-box
The padding seems to do nothingThe element has no background or border, so the extra space is hard to seeGive 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​