Sub topic 3
Padding: space inside the border
Padding is the space between an element's content and its border. This chapter assumes you can write CSS rules with selectors and declarations, and that you know common length units such as px, em, rem, and %. It also assumes you know that every element generates a box with content, padding, border, and margin areas.
What padding does
Padding sits inside the border and takes on the element's own background. If you set a background color, it fills the content area and the padding area. Margin, in contrast, is always transparent.
Padding is useful for three jobs:
- Keeping text away from the edge of a colored or bordered box.
- Enlarging a clickable area, such as a button or link.
- Creating breathing room without changing the element's position relative to its neighbours.
Padding properties
You can set each side on its own, or use the padding shorthand.
| Property | Side |
|---|---|
| `padding-top` | Top |
| `padding-right` | Right |
| `padding-bottom` | Bottom |
| `padding-left` | Left |
| `padding` | All sides, in shorthand |
The shorthand accepts one to four values:
- One value applies to all four sides.
- Two values set vertical (top and bottom), then horizontal (left and right).
- Three values set top, then horizontal, then bottom.
- Four values go clockwise from the top: top, right, bottom, left.
/* top right bottom left */
.box {
margin: 10px 20px 30px 40px;
padding: 8px 16px;
}Here padding: 8px 16px gives 8px above and below the content, and 16px on the left and right. The margin line uses the four-value form, and you will meet it in a later chapter.
Padding and total size
Padding adds to the size of the box. With the default box-sizing: content-box, width sets only the content area. Padding and border are added on top.
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}The border box width is 300 + 20 + 20 + 5 + 5 = 350px. With the 10px margins on each side, the card takes 370px of horizontal space.
With box-sizing: border-box, width includes padding and border. The content area shrinks to fit.
*,
*::before,
*::after {
box-sizing: border-box;
}
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
}The card's border box stays 300px wide. The content area is 300 - 40 - 10 = 250px.
Rules and common errors
- Padding cannot be negative. A negative value is invalid, and the browser ignores the declaration.
- Percentages refer to width. A percentage padding is calculated from the width of the containing block, even for
padding-topandpadding-bottom. Apadding: 10%box has equal-looking spacing on all sides only if the container's width is the reference you expect. - Padding does not collapse. Unlike vertical margins, padding between adjacent elements always adds up.
- Inline elements behave differently. Vertical padding on a
spandraws the background, but it does not push neighbouring lines away. - Width overflow surprises. An element with
width: 100%and padding undercontent-boxbecomes wider than its parent. Useborder-boxto avoid this.
Padding with logical properties
Physical sides such as left and right do not adapt to writing direction. Logical properties do. padding-inline covers the start and end of the text line, and padding-block covers the start and end of the block direction.
.article {
max-inline-size: 40rem;
margin-inline: auto;
padding-block: 1rem;
}In a horizontal left-to-right language, padding-block: 1rem equals 1rem on top and bottom. In a vertical writing mode, the same declaration applies to the left and right instead. A later chapter covers logical properties in full.
Exercise
A .panel has box-sizing: content-box, width: 200px, padding: 10px 15px, and border: 2px solid. What is the border box width, and what would width need to be under border-box to get the same border box width?
Show answer
Under content-box, the border box width is 200 + 15 + 15 + 2 + 2 = 234px. Under border-box, you would set width: 234px, which leaves a 200px content area.
Exercise
Why does padding: 10px 20px 30px set a different left padding than padding: 10px 20px 30px 40px?
Show answer
With three values, the second value applies to both left and right, so left padding is 20px. With four values, the left padding is the fourth value, 40px.