CSS padding-right Property
The CSS padding-right property sets the space between an element's content and the right edge of its padding area.
It controls the physical right side of an element, regardless of writing mode or text direction. Right padding creates internal spacing, contributes to the CSS box model, and normally displays the element's background color or image.
padding-right Syntax
The padding-right property accepts one non-negative length or percentage value, or a CSS-wide keyword.
Basic Syntax
selector {
padding-right: 20px;
}
This adds 20 pixels of internal spacing between the element's content and its right border.
Length Values
.box {
padding-right: 10px;
}
.box {
padding-right: 1.5rem;
}
.box {
padding-right: 2em;
}
.box {
padding-right: 3vw;
}
Lengths can use absolute units such as px or relative units such as rem, em, and viewport units.
Percentage Values
.box {
padding-right: 5%;
}
The percentage is calculated relative to the containing block's width.
Calculated Values
.box {
padding-right: calc(1rem + 10px);
}
.box {
padding-right: clamp(1rem, 4vw, 3rem);
}
CSS functions can combine units or constrain padding to a responsive range.
CSS-Wide Keywords
.box {
padding-right: initial;
}
.box {
padding-right: inherit;
}
.box {
padding-right: unset;
}
.box {
padding-right: revert;
}
.box {
padding-right: 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 right padding using a non-negative length such as 10px, 1rem, or 2em. |
<percentage> |
Sets right padding as a non-negative percentage of the containing block's width. |
0 |
Removes right padding. |
initial |
Resets right padding to its initial value of 0. |
inherit |
Uses the computed right padding value from the parent element. |
unset |
Resets right 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 | Right |
| Shorthand | padding |
Valid Values
.box {
padding-right: 0;
}
.box {
padding-right: 20px;
}
.box {
padding-right: 1.5rem;
}
.box {
padding-right: 5%;
}
.box {
padding-right: calc(1rem + 5px);
}
Invalid Values
.box {
padding-right: -10px;
}
.box {
padding-right: auto;
}
.box {
padding-right: 10px 20px;
}
Negative values, auto, and multiple length or percentage values are invalid for this longhand property.
How It Works
The padding-right property creates internal space between an element's content box and its right border.
Increasing right 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 Right Padding
.panel {
padding-right: 30px;
background-color: #dbeafe;
border: 2px solid #2563eb;
}
The panel receives 30 pixels of internal spacing to the right of its content.
Removing Right Padding
.panel {
padding-right: 0;
}
This removes right padding without changing padding on the other three sides.
Combining Individual Padding Properties
.panel {
padding-top: 10px;
padding-right: 30px;
padding-bottom: 10px;
padding-left: 20px;
}
Each physical side receives its own padding value.
Background and Padding
.notice {
padding-right: 20px;
background-color: #fef3c7;
border: 1px solid #d97706;
}
By default, the background extends through the padding area, so the extra right space shares the element's background.
Padding vs. Margin
Padding creates space inside an element's border. Margin creates space outside the border.
| Property | Effect |
|---|---|
padding-right |
Adds internal space between the content and the right border. |
margin-right |
Adds external space beyond the element's right border. |
Padding Example
.card {
padding-right: 30px;
background-color: #dbeafe;
}
The extra space appears inside the card and normally shares its background.
Margin Example
.card {
margin-right: 30px;
background-color: #dbeafe;
}
The extra space appears outside the card's border and does not receive the card's background.
Choosing Between Padding and Margin
Use padding when content needs breathing room 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;
width: 200px;
padding-right: 20px;
border: 2px solid #2563eb;
}
With content-box, the declared width applies to the content area. The right padding and two borders increase the total rendered width to 224 pixels, assuming no left padding.
border-box
.box {
box-sizing: border-box;
width: 200px;
padding-right: 20px;
border: 2px solid #2563eb;
}
With border-box, the 200-pixel width includes the content area, padding, and borders, provided the content can fit.
Practical Use
.card {
box-sizing: border-box;
width: 100%;
padding: 20px;
padding-right: 40px;
}
This creates extra right padding while keeping the declared width inclusive of padding and borders.
Percentage Padding
Percentage values for padding-right are calculated relative to the containing block's width.
Basic Percentage Example
.container {
width: 400px;
}
.box {
padding-right: 10%;
}
When the containing block is 400 pixels wide, 10% right padding equals 40 pixels.
Responsive Spacing
.section {
padding-right: 5%;
}
The right padding changes as the containing block's width changes.
Using calc()
.section {
padding-right: calc(5% + 10px);
}
This combines a percentage-based value with a fixed length.
Using clamp()
.section {
padding-right: clamp(1rem, 4vw, 3rem);
}
The padding adjusts with viewport width while remaining between 1rem and 3rem.
Important Consideration
Percentage padding depends on the containing block's width, not necessarily the element's own width. The resulting space can change when the containing block resizes.
Interaction With the padding Shorthand
The padding-right 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 right padding is 20 pixels.
Using Two Values
.box {
padding: 10px 20px;
}
The first value sets top and bottom padding. The second sets right and left padding to 20 pixels.
Overriding Right Padding
.box {
padding: 20px;
padding-right: 40px;
}
The top, bottom, and left sides remain at 20 pixels, while the right becomes 40 pixels.
Shorthand After Longhand
.box {
padding-right: 40px;
padding: 20px;
}
At equal cascade priority, the later shorthand resets all four padding sides to 20 pixels.
Physical vs. Logical Padding
The padding-right property always targets the physical right side. Logical padding properties adapt to writing mode and text direction.
| Property | Controls |
|---|---|
padding-right |
The physical right side in every writing mode. |
padding-inline-end |
The logical inline-end side, which is right in horizontal left-to-right writing. |
padding-inline-start |
The logical inline-start side, which is right in horizontal right-to-left writing. |
padding-block-start |
The logical block-start side, which is right in vertical-rl. |
padding-block-end |
The logical block-end side, which is right in vertical-lr. |
Physical Padding
.box {
direction: rtl;
padding-right: 30px;
}
The right side receives 30 pixels of padding even when text direction is right-to-left.
Logical Padding
.box {
direction: rtl;
padding-inline-end: 30px;
}
In horizontal right-to-left writing, the left side receives 30 pixels of padding.
Choosing the Right Property
Use padding-right when spacing must remain on the physical right side. Use logical padding when spacing should follow the writing mode or text direction.
padding-right Example
The following example compares three content boxes with different amounts of right padding.
HTML Example
<div class="padding-demo">
<div class="padding-card no-padding">
<strong>No Right Padding</strong>
<p>padding-right: 0;</p>
</div>
<div class="padding-card medium-padding">
<strong>20px Right Padding</strong>
<p>padding-right: 20px;</p>
</div>
<div class="padding-card large-padding">
<strong>40px Right Padding</strong>
<p>padding-right: 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;
text-align: right;
}
.padding-card p {
margin: 6px 0 0;
font-size: 14px;
}
.no-padding {
padding-right: 0;
}
.medium-padding {
padding-right: 20px;
}
.large-padding {
padding-right: 40px;
}
padding-right: 0;
padding-right: 20px;
padding-right: 40px;
Each box uses the same border and background. Because the text is right-aligned, increasing padding-right visibly moves the content farther from the right border without changing the other padding values.
Practical Content Panel
.content-panel {
padding: 1.5rem;
padding-right: 2.5rem;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
This provides additional space on the right while maintaining consistent padding on the other three sides.
Accessibility Considerations
Right padding can improve readability by providing comfortable spacing between content and the element's border.
Provide Comfortable Spacing
.notice {
padding: 1rem;
padding-right: 1.5rem;
line-height: 1.5;
}
Additional internal spacing can prevent content from appearing crowded against the right 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.
Consider Right-to-Left Content
For multilingual interfaces, consider padding-inline-start or padding-inline-end when spacing should follow the text direction rather than remain on the physical right side.
Avoid Content Clipping
Large padding values combined with fixed widths and restrictive overflow settings can reduce the space available for content. Test layouts with enlarged text and narrow viewports.
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-right: -20px;
}
Negative padding values are invalid. Use margins or positioning when a layout requires an offset.
Using auto
.box {
padding-right: auto;
}
The auto keyword is not supported for padding.
Providing Multiple Values
.box {
padding-right: 10px 20px;
}
This longhand property accepts only one value. Use the padding shorthand to set multiple sides.
Assuming Percentages Use the Element's Width
Percentage padding is based on the containing block's width, not necessarily the element's own width.
Forgetting Shorthand Overrides
.box {
padding-right: 40px;
padding: 20px;
}
At equal cascade priority, the later shorthand overrides the earlier right padding declaration.
Confusing Physical and Logical Padding
padding-right always targets the physical right side. padding-inline-end may target a different side depending on writing mode and direction.
Forgetting Box Sizing
Padding can increase an element's total rendered width when box-sizing: content-box is used.
Important Considerations
Keep the following points in mind when using padding-right:
- It sets internal spacing on the physical right 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.
- 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.
- 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 dimensions combined with large padding can cause overflow.
- Relative units can support responsive spacing.
- 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-right 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-right.
Specifications
The padding-right 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.
