CSS padding-top Property
The CSS padding-top property sets the space between an element's content and the top edge of its padding area.
It controls the physical top side of an element, regardless of writing mode or text direction. Top padding creates internal spacing, contributes to the CSS box model, and normally displays the element's background color or image.
padding-top Syntax
The padding-top property accepts one non-negative length or percentage value, or a CSS-wide keyword.
Basic Syntax
selector {
padding-top: 20px;
}
This adds 20 pixels of internal spacing between the element's content and its top border.
Length Values
.box {
padding-top: 10px;
}
.box {
padding-top: 1.5rem;
}
.box {
padding-top: 2em;
}
.box {
padding-top: 3vh;
}
Lengths can use absolute units such as px or relative units such as rem, em, and viewport units.
Percentage Values
.box {
padding-top: 5%;
}
The percentage is calculated relative to the containing block's width, not its height.
Calculated Values
.box {
padding-top: calc(1rem + 10px);
}
.box {
padding-top: clamp(1rem, 4vw, 3rem);
}
CSS functions can combine units or constrain padding to a responsive range.
CSS-Wide Keywords
.box {
padding-top: initial;
}
.box {
padding-top: inherit;
}
.box {
padding-top: unset;
}
.box {
padding-top: revert;
}
.box {
padding-top: revert-layer;
}
CSS-wide keywords must be used alone and cannot be combined with length or percentage values in the same declaration.
Property Values
| Value | Description |
|---|---|
<length> |
Sets top padding using a non-negative length such as 10px, 1rem, or 2em. |
<percentage> |
Sets top padding as a non-negative percentage of the containing block's width. |
0 |
Removes top padding. |
initial |
Resets top padding to its initial value of 0. |
inherit |
Uses the computed top padding value from the parent element. |
unset |
Resets top padding to 0 because the property is not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 |
| Inherited | No |
| Applies to | All elements except certain internal table elements |
| Computed value | Computed length or percentage |
| Animation type | By computed value type |
| Percentages | Relative to the containing block's width |
| Negative values | Not permitted |
| Physical side | Top |
| Shorthand | padding |
Valid Values
.box {
padding-top: 0;
}
.box {
padding-top: 20px;
}
.box {
padding-top: 1.5rem;
}
.box {
padding-top: 5%;
}
.box {
padding-top: calc(1rem + 5px);
}
Invalid Values
.box {
padding-top: -10px;
}
.box {
padding-top: auto;
}
.box {
padding-top: 10px 20px;
}
Negative values, auto, and multiple length or percentage values are invalid for this longhand property.
How It Works
The padding-top property creates internal space between an element's content box and its top border.
Increasing top padding changes the size of the padding area. Its effect on the element's overall dimensions depends on the element's sizing rules and box-sizing value.
Adding Top Padding
.panel {
padding-top: 30px;
background-color: #dbeafe;
border: 2px solid #2563eb;
}
The panel receives 30 pixels of internal spacing above its content.
Removing Top Padding
.panel {
padding-top: 0;
}
This removes top padding without changing padding on the other three sides.
Combining Individual Padding Properties
.panel {
padding-top: 30px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}
Each physical side receives its own padding value.
Background and Padding
.notice {
padding-top: 20px;
background-color: #fef3c7;
border: 1px solid #d97706;
}
By default, the background extends through the padding area, so the extra top space shares the element's background.
Padding and Child Margins
.panel {
padding-top: 20px;
}
.panel h3 {
margin-top: 15px;
}
The top padding separates the parent's content area from its border. The heading's top margin adds spacing within the panel. Nonzero top padding also prevents the heading's top margin from collapsing through the parent's top edge in normal block layout.
Padding vs. Margin
Padding creates space inside an element's border. Margin creates space outside the border.
| Property | Effect |
|---|---|
padding-top |
Adds internal space between the content and the top border. |
margin-top |
Adds external space beyond the element's top border. |
Padding Example
.card {
padding-top: 30px;
background-color: #dbeafe;
}
The extra space appears inside the card and normally shares its background.
Margin Example
.card {
margin-top: 30px;
background-color: #dbeafe;
}
The extra space appears outside the card's border and does not receive the card's background.
Margin Collapsing
Vertical margins between certain block-level elements can collapse into a single margin. Padding does not collapse.
Choosing Between Padding and Margin
Use padding when content needs spacing inside a component. Use margin when the component needs separation from surrounding elements.
Padding and Box Sizing
The box-sizing property determines whether declared dimensions include padding and borders.
content-box
.box {
box-sizing: content-box;
height: 200px;
padding-top: 20px;
border: 2px solid #2563eb;
}
With content-box, the declared height applies to the content area. The top padding and two borders increase the total rendered height to 224 pixels, assuming no bottom padding.
border-box
.box {
box-sizing: border-box;
height: 200px;
padding-top: 20px;
border: 2px solid #2563eb;
}
With border-box, the 200-pixel height includes the content area, padding, and borders, provided the content can fit.
Practical Use
.card {
box-sizing: border-box;
min-height: 200px;
padding: 20px;
padding-top: 40px;
}
This creates extra top padding while allowing the card to grow vertically when its content requires more space.
Important Consideration
Padding is part of the element's box dimensions. Large padding values can leave less room for content when fixed dimensions and border-box are used.
Percentage Padding
Percentage values for padding-top are calculated relative to the containing block's width, not its height.
Basic Percentage Example
.container {
width: 400px;
}
.box {
padding-top: 10%;
}
When the containing block is 400 pixels wide, 10% top padding equals 40 pixels.
Why Width Matters
Although padding-top controls vertical spacing, its percentage reference is the containing block's width in a horizontal writing mode.
Responsive Spacing
.section {
padding-top: 5%;
}
The top padding changes as the containing block's width changes.
Using calc()
.section {
padding-top: calc(5% + 10px);
}
This combines a percentage-based value with a fixed length.
Using clamp()
.section {
padding-top: clamp(1rem, 4vw, 3rem);
}
The padding adjusts with viewport width while remaining between 1rem and 3rem.
Aspect Ratio Technique
.media-box {
position: relative;
width: 100%;
height: 0;
padding-top: 56.25%;
}
In a horizontal writing mode, this traditional technique creates a box with a 16:9 ratio when the percentage is resolved against the same width used for the box. Modern layouts can usually use aspect-ratio: 16 / 9 instead.
Important Consideration
Percentage padding is based on the containing block's logical width. In vertical writing modes, this reference can correspond to the containing block's physical height.
Interaction With the padding Shorthand
The padding-top property is one of the four physical longhand properties controlled by the padding shorthand.
Using Four Values
.box {
padding: 10px 20px 30px 40px;
}
The values apply clockwise: top, right, bottom, and left. The top padding is 10 pixels.
Using Three Values
.box {
padding: 10px 20px 30px;
}
The first value sets top padding, the second sets right and left padding, and the third sets bottom padding.
Using Two Values
.box {
padding: 10px 20px;
}
The first value sets top and bottom padding. The second sets right and left padding.
Overriding Top Padding
.box {
padding: 20px;
padding-top: 40px;
}
The right, bottom, and left sides remain at 20 pixels, while the top becomes 40 pixels.
Shorthand After Longhand
.box {
padding-top: 40px;
padding: 20px;
}
At equal cascade priority, the later shorthand resets all four padding sides to 20 pixels.
Physical vs. Logical Padding
The padding-top property always targets the physical top side. Logical padding properties adapt to writing mode and text direction.
| Property | Controls |
|---|---|
padding-top |
The physical top side in every writing mode. |
padding-block-start |
The logical block-start side, which is top in horizontal-tb. |
padding-inline-start |
The logical inline-start side, which is top in vertical left-to-right writing. |
padding-inline-end |
The logical inline-end side, which is top in vertical right-to-left writing. |
Physical Padding
.box {
writing-mode: vertical-rl;
padding-top: 30px;
}
The top side receives 30 pixels of padding even when the writing mode is vertical.
Logical Padding
.box {
writing-mode: vertical-rl;
direction: ltr;
padding-inline-start: 30px;
}
In vertical left-to-right writing, inline-start corresponds to the physical top side.
Choosing the Right Property
Use padding-top when spacing must remain on the physical top side. Use logical padding when spacing should follow the writing mode or text direction.
padding-top Example
The following example compares three content boxes with different amounts of top padding.
HTML Example
<div class="padding-demo">
<div class="padding-card no-padding">
<strong>No Top Padding</strong>
<p>padding-top: 0;</p>
</div>
<div class="padding-card medium-padding">
<strong>20px Top Padding</strong>
<p>padding-top: 20px;</p>
</div>
<div class="padding-card large-padding">
<strong>40px Top Padding</strong>
<p>padding-top: 40px;</p>
</div>
</div>
CSS Example
.padding-demo {
display: grid;
gap: 12px;
}
.padding-card {
box-sizing: border-box;
padding: 12px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
.padding-card p {
margin: 6px 0 0;
font-size: 14px;
}
.no-padding {
padding-top: 0;
}
.medium-padding {
padding-top: 20px;
}
.large-padding {
padding-top: 40px;
}
padding-top: 0;
padding-top: 20px;
padding-top: 40px;
Each box uses the same border and background. Increasing padding-top moves the content farther from the top border without changing the other padding values.
Practical Content Panel
.content-panel {
padding: 1.5rem;
padding-top: 2.5rem;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
This provides additional space above the content while maintaining consistent padding on the other three sides.
Accessibility Considerations
Top padding can improve readability by providing comfortable spacing between content and the element's upper border.
Provide Comfortable Spacing
.notice {
padding: 1rem;
padding-top: 1.5rem;
line-height: 1.5;
}
Additional internal spacing can prevent text from appearing crowded against the top edge of a panel.
Support Text Resizing
Relative units such as rem can help padding respond to changes in the user's preferred font size.
Avoid Content Clipping
Large top padding combined with fixed heights and restrictive overflow settings can reduce the space available for content. Test layouts with enlarged text and narrow viewports.
Consider Different Writing Modes
For multilingual interfaces, consider padding-block-start when spacing should follow the beginning of the block axis rather than remain on the physical top side.
Maintain Usable Controls
Padding can contribute to the clickable area of interactive elements. Ensure buttons and other controls remain accessible by keyboard and touch.
Important Consideration
Padding improves layout spacing but does not replace semantic HTML, keyboard accessibility, readable typography, or sufficient contrast.
Common Mistakes
Confusing Padding With Margin
Padding creates space inside the border. Margin creates space outside it.
Using Negative Values
.box {
padding-top: -20px;
}
Negative padding values are invalid. Use margins or positioning when a layout requires an offset.
Using auto
.box {
padding-top: auto;
}
The auto keyword is not supported for padding.
Providing Multiple Values
.box {
padding-top: 10px 20px;
}
This longhand property accepts only one value. Use the padding shorthand to set multiple sides.
Assuming Percentages Use the Element's Height
In horizontal writing modes, percentage top padding is based on the containing block's width, not its height.
Forgetting Shorthand Overrides
.box {
padding-top: 40px;
padding: 20px;
}
At equal cascade priority, the later shorthand overrides the earlier top padding declaration.
Confusing Physical and Logical Padding
padding-top always targets the physical top side. padding-block-start may target a different side depending on writing mode.
Forgetting Box Sizing
Padding can increase an element's total rendered height when box-sizing: content-box is used with an explicitly declared height.
Expecting Padding to Collapse
Padding does not collapse. Unlike certain vertical margins, top padding always contributes its specified spacing to the box.
Important Considerations
Keep the following points in mind when using padding-top:
- It sets internal spacing on the physical top side.
- It does not change sides when writing mode or text direction changes.
- It accepts one non-negative length or percentage value.
- Negative values are invalid.
- The
autokeyword is invalid. - The initial value is
0. - The property is not inherited.
- In horizontal writing modes, percentage values are relative to the containing block's width.
- It is a longhand property of the
paddingshorthand. - Later shorthand declarations can override its value.
- Padding creates space inside an element's border.
- Margin creates space outside the border.
- Padding does not collapse like certain vertical margins.
- Nonzero top padding can prevent a child's top margin from collapsing through its parent.
- The element's background normally extends through its padding area.
- Padding contributes to the CSS box model.
- The
box-sizingproperty affects how padding contributes to declared dimensions. - Fixed heights combined with large padding can cause overflow.
- Relative units can support responsive spacing.
- Percentage top padding can be used in traditional aspect-ratio techniques.
- Modern layouts can use
aspect-ratiofor proportional sizing. - Logical padding properties are useful when spacing should follow writing mode or text direction.
- Physical and logical padding declarations may target the same side.
- The CSS cascade determines which conflicting declaration takes effect.
Browser Support
The CSS padding-top property is Widely available and supported by all modern browsers, including Chrome, Edge, Firefox, and Safari.
It is a long-established CSS property with broad compatibility across desktop and mobile browsers.
For current compatibility information, visit Can I Use? and search for padding-top.
Specifications
The padding-top property is defined in CSS Box Model Module Level 3.
The specification defines padding properties, accepted values, and their contribution to the CSS box model.
For additional documentation and examples, consult MDN Web Docs.
