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 px, em, rem, and % units. It also assumes you know the four layers of the box (content, padding, border, margin) from the overview chapter.
What padding does
Padding is the transparent gap that surrounds the content area and sits inside the border. It is the box's inner buffer. It does three jobs:
- It keeps text and images away from the border, so content does not touch the edge of a card or button.
- It enlarges the box's visible and clickable area without changing the content.
- It carries the element's own background, so the padded area looks like part of the element.
The last point separates padding from margin. An element's background-color and background-image are painted under the content and the padding. By default they also extend under the border, because background-clip defaults to border-box. Margin is never painted with the element's background.
The four padding properties
Padding has one longhand property for each side and one shorthand property.
padding-toppadding-rightpadding-bottompadding-leftpadding, which sets any combination of them at once
The initial value of each is 0. Browsers apply default padding to some elements through the user-agent stylesheet, for example ul and button. That is why a bare list is indented and a bare button is not flush with its text.
Shorthand value counts
The padding shorthand takes one to four values. They are assigned clockwise, starting at the top.
| Values | Meaning | Example | Result |
|---|---|---|---|
| 1 | All four sides | `padding: 10px` | 10px on every side |
| 2 | Vertical, then horizontal | `padding: 8px 16px` | top and bottom 8px, left and right 16px |
| 3 | Top, horizontal, bottom | `padding: 4px 12px 20px` | top 4px, left and right 12px, bottom 20px |
| 4 | Top, right, bottom, left | `padding: 1px 2px 3px 4px` | each side set separately |
A missing value copies from the opposite side. With three values, the left side copies the right. With two values, the bottom copies the top and the left copies the right.
/* These two rules are equivalent. */
.box-a {
padding: 8px 16px;
}
.box-b {
padding-top: 8px;
padding-right: 16px;
padding-bottom: 8px;
padding-left: 16px;
}A common mistake is to use the shorthand when you meant to change one side. padding: 20px on a rule that appears later in the cascade resets all four sides, including sides you set earlier. If you want to change only one side, use the longhand property.
Which values are valid
Padding accepts a length or a percentage. Other values behave differently from margin:
- Negative values are invalid.
padding-left: -10pxis dropped, and the browser ignores the whole declaration. To pull content closer, reduce the padding or use a negative margin on the element or its child. autois invalid. Margin can beautofor centring. Padding cannot, sopadding: 0 autois ignored.0needs no unit.padding: 0is valid.
Relative units work as you would expect. em is relative to the element's own font-size for padding, and rem is relative to the root font size. Many designs use em for buttons so the padding grows with the text:
.btn {
font-size: 1rem;
padding: 0.5em 1em;
}
.btn.large {
font-size: 1.5rem; /* padding scales to 0.75rem 1.5rem automatically */
}Percentage padding
A percentage padding is calculated from the width of the containing block, not from the element's own size. This applies to all four sides, including padding-top and padding-bottom. In a horizontal writing mode, a percentage vertical padding therefore depends on width, not height.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Percentage padding</title>
<style>
.parent {
width: 400px;
background: #eee;
}
.child {
padding: 10%; /* 10% of 400px = 40px on all four sides */
background: #9cf;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">Padded by 40px on every side</div>
</div>
</body>
</html>The parent is 400px wide, so the child gets 40px of padding on all four sides. If you resize the parent to 300px, the padding becomes 30px, including the top and bottom.
Percentage padding was once used to hold an aspect ratio, for example padding-top: 56.25% for 16:9. The aspect-ratio property now does this directly and is clearer.
Inside flex and grid containers, percentage padding on a child resolves against the container's content box (the flex container's, or the grid area for grid items). It still resolves against the inline size, not the height.
Padding and the total size of a box
Padding changes how large the box is, and the effect depends on box-sizing. With the default content-box, width sets only the content area, so padding is added on top:
.panel {
width: 200px;
padding: 20px;
/* content-box: the border box is 200 + 20 + 20 = 240px wide */
}With box-sizing: border-box, width includes padding and border, so the content area shrinks to make room. The box-sizing chapter covers this fully. For padding, remember that padding never changes the declared width under border-box, but it can squeeze the content area. If padding is larger than the declared width, the content area becomes 0 and the box is still as wide as the padding requires.
A common overflow error
The classic bug is width: 100% combined with padding:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Padding overflow</title>
<style>
.wrap {
width: 300px;
background: #eee;
}
.broken {
width: 100%;
padding: 20px; /* content-box: 300 + 40 = 340px, overflows */
background: #f99;
}
.fixed {
box-sizing: border-box;
width: 100%;
padding: 20px; /* border-box: stays 300px */
background: #9f9;
}
</style>
</head>
<body>
<div class="wrap">
<div class="broken">Sticks out by 40px</div>
<div class="fixed">Fits exactly</div>
</div>
</body>
</html>There are two fixes. Set box-sizing: border-box, as above. Or leave out width. A block-level element with width: auto fills its container and subtracts its own padding and border automatically, so it never overflows.
Padding on inline elements
Padding behaves differently depending on the element's display value. The display chapter covers the full model. For padding, the key points are:
- Block and inline-block elements apply padding on all four sides, and all four sides affect layout.
- Inline elements (such as
spanandaby default) apply horizontal padding normally. It pushes the text of that element sideways and the neighbouring text moves over. Vertical padding is painted, but it does not change the height of the line, so it can overlap lines above and below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inline padding</title>
<style>
.tag {
background: gold;
padding: 12px 8px;
}
.tag.solid {
display: inline-block; /* vertical padding now affects layout */
}
</style>
</head>
<body>
<p>Line one has an <span class="tag">inline tag</span> in it.</p>
<p>Line two has an <span class="tag solid">inline-block tag</span> in it.</p>
</body>
</html>In the first paragraph, the gold background overlaps the paragraph's line above and below, but the paragraph does not get taller. In the second paragraph, the tag is an inline-block, so the 12px of vertical padding makes its line taller and pushes other content away.
This is why padding on links often seems to do nothing for vertical spacing. To make a link a larger click target, set display: inline-block or display: block on it.
Padding does not collapse
Vertical margins between adjacent block boxes can collapse into one. Padding never collapses. Two adjacent paddings always add together. This also makes padding a quick way to stop a child's margin from collapsing through its parent. Even a small padding on the parent separates the two margins. The margin collapsing chapter shows this in detail.
Checking your padding
Padding is easy to misjudge by eye, so verify it in the browser:
- Right-click the element and choose Inspect.
- In the box-model diagram in the Styles or Computed panel, the padding area is usually shaded green.
- Hover the element in the DOM tree to highlight its padding in the page.
- Read the numbers in the diagram. Percentage padding shows as the computed pixel value.
Physical and logical sides
The properties above use physical sides: top, right, bottom, left. CSS also has logical properties such as padding-inline and padding-block. These follow the writing direction, so they work correctly for right-to-left and vertical text. A later chapter covers them. For now, padding-inline: 1rem means the same as padding-left: 1rem; padding-right: 1rem in a left-to-right language like English.
Exercise
A .parent is 500px wide with no padding. Its child has width: auto; padding: 5% 10%; box-sizing: content-box. What are the child's top and bottom padding and its left and right padding, in pixels? Does the child overflow the parent?
Show answer
Percentage padding resolves against the parent's width, even for top and bottom. So 5% of 500px gives 25px of top and bottom padding, and 10% of 500px gives 50px of left and right padding. The child does not overflow. With width: auto, a block box fills its container and subtracts its own padding, so the content area is 500 - 100 = 400px and the border box is 500px.
Exercise
You add padding: 15px to a span inside a paragraph, and the lines of text above and below it are still the same distance apart. The gold background now overlaps them. Why, and what single property change makes the padding push the surrounding lines away?
Show answer
A span is an inline element. Its vertical padding is painted but does not affect the height of the line box, so it overlaps nearby lines. Setting display: inline-block on the span (or display: block) makes the vertical padding count in layout and push the surrounding lines away.