CSS padding Property
The CSS padding property is a shorthand that sets the space between an element's content and its borders on all four sides.
Padding creates internal spacing around text, images, and other content. It can be applied equally to every side or assigned different values for the top, right, bottom, and left. Unlike margins, padding is inside the element's border and contributes to its dimensions under the default box model.
padding Syntax
The padding shorthand accepts one to four non-negative length or percentage values. The number of values determines which sides receive each value.
Basic Syntax
selector {
padding: 20px;
}
One to Four Values
.box {
padding: 20px;
}
.box {
padding: 10px 20px;
}
.box {
padding: 10px 20px 30px;
}
.box {
padding: 10px 20px 30px 40px;
}
Different Units
.box {
padding: 1rem;
}
.box {
padding: 2em;
}
.box {
padding: 5%;
}
.box {
padding: 10px 2rem;
}
CSS-Wide Keywords
.box {
padding: initial;
}
.box {
padding: inherit;
}
.box {
padding: unset;
}
.box {
padding: revert;
}
.box {
padding: revert-layer;
}
The initial value for each padding side is 0. Negative padding values are invalid.
Property Values
| Value | Description |
|---|---|
<length> |
Sets padding using a non-negative length, such as 10px, 1rem, or 2em. |
<percentage> |
Sets padding as a non-negative percentage of the containing block's logical width. |
0 |
Removes padding from the specified sides. |
initial |
Resets all four padding sides to 0. |
inherit |
Uses the computed padding values of the parent element. |
unset |
Resets all four sides to 0 because padding is not inherited. |
revert |
Reverts padding according to cascade origin rules. |
revert-layer |
Reverts padding according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 for each side |
| Inherited | No |
| Applies to | All elements, except certain internal table elements |
| Computed value | Computed length or percentage for each side |
| Animation type | By computed value type for each longhand |
| Percentages | Relative to the containing block's logical width |
| Negative values | Not permitted |
| Shorthand for | padding-top, padding-right, padding-bottom, padding-left |
Valid Values
.box {
padding: 0;
}
.box {
padding: 15px;
}
.box {
padding: 1rem 2rem;
}
.box {
padding: 5% 10%;
}
.box {
padding: calc(1rem + 5px);
}
Invalid Values
.box {
padding: -10px;
}
.box {
padding: auto;
}
.box {
padding: 10px 20px 30px 40px 50px;
}
Padding does not accept negative values or auto. The shorthand accepts no more than four values.
How It Works
Padding adds space inside an element's border, between the content box and the border. The element's background normally extends through the padding area.
Equal Padding on All Sides
.content-box {
padding: 20px;
background-color: #dbeafe;
border: 2px solid #2563eb;
}
The content has 20 pixels of internal spacing on every side.
Different Horizontal and Vertical Padding
.content-box {
padding: 10px 25px;
}
The top and bottom receive 10 pixels, while the right and left receive 25 pixels.
Removing Padding
.content-box {
padding: 0;
}
All four padding sides are set to zero.
Background and Padding
.notice {
padding: 15px;
background-color: #fef3c7;
border: 1px solid #d97706;
}
The background color fills the content and padding areas by default, making padding useful for notices, cards, buttons, and other interface elements.
Padding Shorthand Values
The number of values determines how padding is distributed around an element.
| Values | Example | Result |
|---|---|---|
| One | padding: 20px; |
All four sides: 20px |
| Two | padding: 10px 20px; |
Top/bottom: 10px; right/left: 20px |
| Three | padding: 10px 20px 30px; |
Top: 10px; right/left: 20px; bottom: 30px |
| Four | padding: 10px 20px 30px 40px; |
Top: 10px; right: 20px; bottom: 30px; left: 40px |
One Value
.box {
padding: 20px;
}
All four sides receive the same padding.
Two Values
.box {
padding: 10px 20px;
}
The first value sets top and bottom padding. The second sets right and left padding.
Three Values
.box {
padding: 10px 20px 30px;
}
The values set top, horizontal sides, and bottom padding, respectively.
Four Values
.box {
padding: 10px 20px 30px 40px;
}
The values follow clockwise order: top, right, bottom, and left.
Important Consideration
The shorthand uses physical sides. Its order does not change with writing mode or text direction. For writing-mode-aware spacing, use logical padding properties.
Padding and the CSS Box Model
The CSS box model consists of content, padding, borders, and margins. Padding sits between the content and the border.
Default Box Sizing
.box {
width: 200px;
padding: 20px;
border: 2px solid #2563eb;
box-sizing: content-box;
}
With content-box, the declared width applies only to the content. Padding and borders increase the element's total rendered width.
| Component | Width |
|---|---|
| Content | 200px |
| Left and right padding | 40px |
| Left and right borders | 4px |
| Total width | 244px |
Using border-box
.box {
width: 200px;
padding: 20px;
border: 2px solid #2563eb;
box-sizing: border-box;
}
With border-box, the declared width includes the content, padding, and borders. The total width remains 200 pixels, leaving 156 pixels for the content.
Common Box-Sizing Reset
*,
*::before,
*::after {
box-sizing: border-box;
}
This rule makes element sizing more predictable by including padding and borders within declared widths and heights.
Important Consideration
If padding and borders exceed a specified border-box size, the browser cannot reduce the content box below zero. The rendered box may therefore be larger than the specified size.
Padding vs. Margin
Padding and margin both create spacing, but they affect different parts of the box model.
| Feature | Padding | Margin |
|---|---|---|
| Location | Inside the border | Outside the border |
| Background | Normally extends into padding | Does not extend into margin |
| Negative values | Not allowed | Allowed |
auto |
Not allowed | Allowed |
| Typical use | Space around content within an element | Space between elements |
| Vertical collapsing | Does not collapse | Some vertical margins can collapse |
Padding Example
.card {
padding: 20px;
background-color: #dbeafe;
}
The card's content is separated from its edges by 20 pixels.
Margin Example
.card {
margin: 20px;
background-color: #dbeafe;
}
The card has 20 pixels of external spacing around its border box.
Combining Padding and Margin
.card {
padding: 20px;
margin-bottom: 25px;
border: 1px solid #64748b;
}
Padding separates the card's content from its border, while margin creates space below the card.
Percentage Padding
Percentage padding is calculated relative to the containing block's logical width, not the element's own width or height.
Basic Percentage Example
.container {
width: 400px;
}
.box {
padding: 5%;
}
In a horizontal writing mode, if the containing block's width is 400 pixels, each padding side resolves to 20 pixels.
Vertical Padding Percentages
.box {
padding-top: 10%;
padding-bottom: 10%;
}
Even top and bottom padding percentages are based on the containing block's logical width. They are not calculated from its height.
Responsive Spacing
.responsive-panel {
padding: 3% 5%;
}
Percentage padding changes as the containing block's logical width changes, making it useful for some responsive layouts.
Combining Fixed and Relative Units
.responsive-panel {
padding: clamp(12px, 3vw, 30px);
}
The clamp() function provides a minimum padding of 12 pixels, a preferred value of 3 viewport-width units, and a maximum of 30 pixels.
Important Consideration
Percentage padding can become unexpectedly large or small when the containing block changes size. Test responsive layouts at different viewport widths.
Padding Longhand Properties
The padding shorthand sets four physical padding properties.
| Property | Description |
|---|---|
padding-top |
Sets padding above the content. |
padding-right |
Sets padding to the right of the content. |
padding-bottom |
Sets padding below the content. |
padding-left |
Sets padding to the left of the content. |
Equivalent Longhand Declarations
.box {
padding: 10px 20px 30px 40px;
}
The shorthand above is equivalent to:
.box {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;
}
Overriding One Side
.box {
padding: 20px;
padding-bottom: 40px;
}
The element has 20 pixels of padding on the top, right, and left, and 40 pixels on the bottom.
Shorthand Declaration Order
.box {
padding-left: 40px;
padding: 20px;
}
The later shorthand resets all four physical sides to 20 pixels, including the left side.
Logical Padding Properties
Logical properties provide writing-mode-aware alternatives to physical padding.
.box {
padding-block: 15px;
padding-inline: 25px;
}
padding-block controls spacing along the logical block axis, while padding-inline controls spacing along the logical inline axis.
In horizontal writing mode, these correspond to vertical and horizontal padding. In vertical writing modes, their physical orientation changes.
Important Consideration
Physical and logical padding properties can target the same side. When declarations have equal cascade priority, source order determines which declaration wins.
padding Example
The following example compares one-value, two-value, three-value, and four-value padding declarations.
HTML Example
<div class="padding-demo">
<div class="padding-item">
<h3>One Value</h3>
<div class="padding-box padding-one">
<span>Content</span>
</div>
</div>
<div class="padding-item">
<h3>Two Values</h3>
<div class="padding-box padding-two">
<span>Content</span>
</div>
</div>
<div class="padding-item">
<h3>Three Values</h3>
<div class="padding-box padding-three">
<span>Content</span>
</div>
</div>
<div class="padding-item">
<h3>Four Values</h3>
<div class="padding-box padding-four">
<span>Content</span>
</div>
</div>
</div>
CSS Example
.padding-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 20px;
padding: 20px;
background-color: #f1f5f9;
}
.padding-item h3 {
margin: 0 0 10px;
font-size: 16px;
}
.padding-box {
box-sizing: border-box;
min-height: 120px;
background-color: #bfdbfe;
border: 2px solid #2563eb;
}
.padding-box span {
display: block;
padding: 10px;
background-color: #fff;
text-align: center;
}
.padding-one {
padding: 20px;
}
.padding-two {
padding: 10px 30px;
}
.padding-three {
padding: 10px 20px 30px;
}
.padding-four {
padding: 10px 20px 30px 40px;
}
One Value
Two Values
Three Values
Four Values
The blue area represents padding around the white content box. Each example demonstrates how the shorthand distributes spacing across the four sides.
Padding and Box Sizing Example
This example compares how padding affects total width with content-box and border-box.
HTML Example
<div class="sizing-demo">
<div class="sizing-box sizing-content">
content-box
</div>
<div class="sizing-box sizing-border">
border-box
</div>
</div>
CSS Example
.sizing-demo {
padding: 15px;
background-color: #f1f5f9;
}
.sizing-box {
width: 200px;
max-width: 100%;
padding: 20px;
margin-bottom: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
.sizing-content {
box-sizing: content-box;
}
.sizing-border {
box-sizing: border-box;
}
With content-box, the declared 200-pixel width excludes padding and borders, producing a total width of 244 pixels. With border-box, the total width remains 200 pixels.
Accessibility Considerations
Appropriate padding improves readability, touch interaction, and the separation of content from surrounding borders.
Provide Comfortable Text Spacing
.notice {
padding: 1rem 1.5rem;
line-height: 1.5;
}
Padding keeps text from touching the edges of a container and helps make content easier to read.
Create Usable Interactive Controls
.button {
padding: 12px 20px;
min-height: 44px;
}
Padding can increase the clickable area of buttons and links. Check that controls meet applicable target-size accessibility requirements.
Support Text Resizing
.card {
padding: 1rem;
}
Relative units such as rem can help spacing scale with user font-size preferences.
Avoid Content Clipping
Large padding combined with fixed dimensions can leave too little space for content. Test layouts with increased text size and narrow viewports.
Important Consideration
Padding improves spacing, but it does not replace clear typography, sufficient color contrast, or accessible interactive controls.
Common Mistakes
Using Negative Padding
.box {
padding: -10px;
}
Negative padding is invalid. Use margins or other layout properties when negative spacing is necessary.
Using auto
.box {
padding: auto;
}
Unlike margins, padding does not accept auto.
Confusing Value Order
.box {
padding: 10px 20px 30px 40px;
}
The values follow clockwise order: top, right, bottom, and left.
Forgetting Box Sizing
With the default content-box model, padding increases an element's total dimensions beyond its declared width or height.
Assuming Percentage Padding Uses Element Height
Percentage padding is based on the containing block's logical width, including for top and bottom padding.
Overriding Individual Sides Accidentally
.box {
padding-left: 40px;
padding: 20px;
}
The later shorthand resets the left padding to 20 pixels.
Using Padding to Separate Elements
Padding creates space inside an element. Use margins or layout gaps when the goal is to separate neighboring elements.
Important Considerations
Keep the following points in mind when using the padding property:
- The property sets internal spacing on all four physical sides.
- It is a shorthand for the four physical padding longhands.
- The initial value is
0for each side. - Padding is not inherited.
- The shorthand accepts one to four values.
- Four values follow clockwise order: top, right, bottom, and left.
- Negative values are invalid.
- The
autokeyword is invalid. - Lengths and percentages can be combined.
- Percentage padding uses the containing block's logical width.
- Padding normally displays the element's background.
- Padding does not collapse like some vertical margins.
- With
content-box, padding increases the total box dimensions. - With
border-box, padding is included in declared dimensions. - The shorthand resets all four physical padding sides.
- Physical padding sides do not change with writing mode.
- Logical padding properties provide writing-mode-aware alternatives.
- Relative units can help create responsive spacing.
- Padding can increase the interactive area of buttons and links.
- Test layouts with text resizing and narrow viewports.
Browser Support
The CSS padding property is Widely available and has been supported by all major browsers for many years.
Basic length values, percentage values, and the one-to-four-value shorthand syntax are supported across modern browsers. Newer CSS functions used inside padding declarations may have separate compatibility requirements.
Basic Compatible Example
.box {
padding: 10px 20px;
}
This declaration works across modern browsers and establishes 10 pixels of vertical padding and 20 pixels of horizontal padding.
For current compatibility information, visit Can I Use? and search for padding.
Specifications
The padding property is defined in CSS Box Model Module Level 3.
The specification defines the padding area, physical padding properties, shorthand syntax, and percentage behavior.
For additional documentation and examples, consult MDN Web Docs.
