The padding property is used to create space around an element's content, inside of any defined borders. Think of it as 'breathing room' for the text or content inside a box.
Like margins, CSS has individual properties for each side of the box:
padding-toppadding-rightpadding-bottompadding-leftp {
padding-top: 10px;
padding-bottom: 25px;
padding-left: 20px;
padding-right: 15px;
}
You can write all padding properties in a single line. The shorthand follows the clockwise direction: Top, Right, Bottom, Left.
/* Four values: Top, Right, Bottom, Left */
padding: 25px 50px 75px 100px;
/* Three values: Top, Right/Left, Bottom */
padding: 25px 50px 75px;
/* Two values: Top/Bottom, Right/Left */
padding: 15px 30px;
/* One value: All four sides */
padding: 20px;
When you add padding to an element, it is typically added to the total width of that element. If an element has a specified width, adding padding will make it wider than its set width.
box-sizing: border-box; to keep the width of an element constant regardless of the padding added.
/* Content is wider than 300px */
.box1 {
width: 300px;
padding: 25px;
border: 1px solid black;
}
/* Content remains 300px width */
.box2 {
width: 300px;
padding: 25px;
border: 1px solid black;
box-sizing: border-box; /* Highly Recommended! */
}
Unlike the margin property, the padding property cannot have negative values. Padding must always be 0 or a positive number.
Padding is essential for making clickable buttons readable and attractive.
.my-button {
padding: 12px 24px; /* Makes the button look professional */
background-color: #2e7d52;
color: white;
border: none;
}
box-sizing: border-box. Without it, padding is added on top of your set width, silently making elements wider than intended — the single most common CSS layout surprise.
Two boxes, both set to width: 200px with the same padding and border — only their box-sizing differs:
.content-box {
width: 200px;
padding: 20px;
border: 2px solid #2e7d52;
box-sizing: content-box; /* default */
}
.border-box {
width: 200px;
padding: 20px;
border: 2px solid #2e7d52;
box-sizing: border-box;
}
Rendered result (both set to the same 200px width):
Q: Can padding be negative?
A: No — padding must always be zero or a positive value. Use margin or transform instead if you need to pull an element outward.
Q: Does padding affect the background color area?
A: Yes — an element's background-color extends underneath its padding, right up to the border. Margin, by contrast, is always transparent.
Q: What's the shorthand order for padding?
A: Same as margin — top, right, bottom, left (clockwise from the top) when four values are given. See the MDN padding reference for the 1/2/3-value shorthand rules.