CSS scroll-padding Property
The CSS scroll-padding property is a shorthand that defines the optimal viewing region inside a scroll container by setting scroll padding on its four physical edges.
It helps control where elements align during scrolling without changing normal layout spacing. Common uses include keeping scroll targets visible below sticky headers, creating consistent offsets in scroll snap containers, and improving anchor navigation.
scroll-padding Syntax
The scroll-padding property accepts one to four values. Each value can be a non-negative CSS length, a percentage, or auto.
Basic Syntax
selector {
scroll-padding: 20px;
}
One to Four Values
.container {
scroll-padding: 20px;
}
.container {
scroll-padding: 20px 40px;
}
.container {
scroll-padding: 10px 20px 30px;
}
.container {
scroll-padding: 10px 20px 30px 40px;
}
Other Valid Values
.container {
scroll-padding: auto;
}
.container {
scroll-padding: 5%;
}
.container {
scroll-padding: 2rem;
}
.container {
scroll-padding: calc(2rem + 10px);
}
.container {
scroll-padding: 80px 20px auto;
}
CSS-Wide Keywords
.container {
scroll-padding: initial;
}
.container {
scroll-padding: inherit;
}
.container {
scroll-padding: unset;
}
.container {
scroll-padding: revert;
}
.container {
scroll-padding: revert-layer;
}
Unlike scroll-margin, scroll padding accepts percentages and auto. Negative values are invalid.
Property Values
| Value | Description |
|---|---|
<length> |
Sets scroll padding using a non-negative CSS length such as px, rem, or em. |
<percentage> |
Sets scroll padding as a percentage of the scroll container's scrollport dimension along the corresponding axis. |
auto |
Lets the browser determine an appropriate scroll padding value. It generally resolves to zero unless the browser defines another offset. |
0 |
Applies no additional scroll padding on the specified edges. |
calc() |
Calculates a valid non-negative length or percentage-based value. |
initial |
Resets all four scroll padding longhands to their initial value of auto. |
inherit |
Inherits the computed values of the four scroll padding longhands from the parent element. |
unset |
Resets the longhands to auto because they are not inherited. |
revert |
Reverts the longhands according to CSS cascade origin rules. |
revert-layer |
Reverts the longhands according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto for each longhand |
| Inherited | No |
| Applies to | Scroll containers |
| Computed value | As specified for the individual longhands |
| Animation type | By computed value type for the individual longhands |
| Percentages | Relative to the corresponding dimension of the scrollport |
| Negative values | Not accepted |
| Longhands | scroll-padding-top, scroll-padding-right, scroll-padding-bottom, scroll-padding-left |
Valid Values
.container {
scroll-padding: 40px;
}
.container {
scroll-padding: 10% 5%;
}
.container {
scroll-padding: 4rem 2rem;
}
.container {
scroll-padding: auto;
}
.container {
scroll-padding: calc(3rem + 10px) 20px;
}
Invalid Values
.container {
scroll-padding: -10px;
}
.container {
scroll-padding: 10px 20px 30px 40px 50px;
}
.container {
scroll-padding: smooth;
}
.container {
scroll-padding: 2s;
}
.container {
scroll-padding: normal;
}
The shorthand accepts at most four non-negative length, percentage, or auto values. Scrolling behavior keywords and time units are not valid.
How It Works
The scroll-padding property defines insets from the edges of a scroll container's scrollport. These insets establish the optimal viewing region used for scroll alignment.
For example, adding scroll padding at the top of a container can keep a target from aligning directly against the top edge.
Default Scroll Padding
.container {
scroll-padding: auto;
}
The browser determines the scroll padding. In most cases, the effective offset is zero.
Uniform Scroll Padding
.container {
scroll-padding: 30px;
}
All four edges receive a 30-pixel scroll alignment inset.
Different Horizontal and Vertical Insets
.container {
scroll-padding: 60px 20px;
}
The top and bottom insets are 60 pixels, while the left and right insets are 20 pixels.
Percentage-Based Scroll Padding
.container {
scroll-padding: 10% 5%;
}
Top and bottom percentages use the scrollport's height. Left and right percentages use its width.
Scroll Padding Does Not Change Layout
Unlike the regular padding property, scroll-padding does not change the element's box dimensions or the layout position of its children.
Scroll Range Limitations
The browser may not always achieve the requested alignment if the scroll container does not have enough available scroll range.
One to Four Shorthand Values
The scroll-padding shorthand follows the same clockwise value order as CSS padding.
One Value
.container {
scroll-padding: 20px;
}
All four edges receive 20 pixels.
Two Values
.container {
scroll-padding: 20px 40px;
}
Top and bottom receive 20 pixels. Right and left receive 40 pixels.
Three Values
.container {
scroll-padding: 10px 20px 30px;
}
Top receives 10 pixels, right and left receive 20 pixels, and bottom receives 30 pixels.
Four Values
.container {
scroll-padding: 10px 20px 30px 40px;
}
The values apply clockwise: top, right, bottom, and left.
Equivalent Longhand Declarations
.container {
scroll-padding-top: 10px;
scroll-padding-right: 20px;
scroll-padding-bottom: 30px;
scroll-padding-left: 40px;
}
Mixing Values
.container {
scroll-padding: 80px 5% auto 20px;
}
The top receives 80 pixels, the right receives 5% of the scrollport width, the bottom uses auto, and the left receives 20 pixels.
Overriding One Edge
.container {
scroll-padding: 20px;
scroll-padding-top: 80px;
}
The top inset becomes 80 pixels, while the other edges remain at 20 pixels.
Declaration Order
.container {
scroll-padding-top: 80px;
scroll-padding: 20px;
}
The later shorthand resets all four physical scroll padding longhands to 20 pixels.
Using scroll-padding With Scroll Snap
Scroll padding is commonly used with CSS Scroll Snap to control where items align within a scroll container.
Horizontal Scroll Snap
.gallery {
display: flex;
gap: 20px;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-padding: 0 40px;
}
.gallery .item {
flex: 0 0 200px;
scroll-snap-align: start;
}
The gallery defines 40-pixel scroll alignment insets on its left and right edges.
Vertical Scroll Snap
.scroll-container {
height: 300px;
overflow-y: auto;
scroll-snap-type: y mandatory;
scroll-padding: 30px 0;
}
.scroll-container .item {
min-height: 200px;
scroll-snap-align: start;
}
The container defines 30-pixel top and bottom scroll alignment insets.
Using Different Insets
.gallery {
scroll-padding: 20px 60px 20px 30px;
}
The gallery uses different offsets for each physical edge.
Important Consideration
Scroll padding does not activate scroll snapping. The container must define scroll-snap-type, and its snap targets must define scroll-snap-align.
Sticky Headers and Anchor Navigation
The scroll-padding property can prevent anchor-linked content from appearing beneath a sticky or fixed header.
Setting a Viewport Scroll Offset
html {
scroll-padding-top: 80px;
}
When the document scrolls to an anchor target, the browser can leave approximately 80 pixels of space above the target.
HTML Anchor Links
<nav aria-label="Page sections">
<a href="#overview">Overview</a>
<a href="#features">Features</a>
<a href="#contact">Contact</a>
</nav>
<section id="overview">
<h2>Overview</h2>
</section>
<section id="features">
<h2>Features</h2>
</section>
<section id="contact">
<h2>Contact</h2>
</section>
Using a Custom Property
:root {
--header-height: 80px;
}
html {
scroll-padding-top: calc(var(--header-height) + 16px);
}
The calculated inset accounts for the header height and adds 16 pixels of additional space.
Responsive Header Heights
html {
scroll-padding-top: 90px;
}
@media (max-width: 767px) {
html {
scroll-padding-top: 60px;
}
}
The scroll offset changes to match a shorter header on smaller screens.
Important Consideration
For document scrolling, apply scroll-padding-top to the root html element. For a separately scrolling panel, apply it to that panel's scroll container.
Physical Versus Logical Scroll Padding
The scroll-padding shorthand sets physical edge values. Logical scroll padding properties follow writing mode and text direction.
| Property | Edges |
|---|---|
scroll-padding |
All four physical edges |
scroll-padding-block |
Logical block-start and block-end edges |
scroll-padding-inline |
Logical inline-start and inline-end edges |
scroll-padding-top |
Physical top edge |
scroll-padding-right |
Physical right edge |
scroll-padding-bottom |
Physical bottom edge |
scroll-padding-left |
Physical left edge |
Physical Scroll Padding
.container {
scroll-padding: 20px 40px;
}
The top and bottom receive 20 pixels, while the physical left and right edges receive 40 pixels regardless of writing mode.
Logical Scroll Padding
.container {
scroll-padding-block: 20px;
scroll-padding-inline: 40px;
}
The offsets follow the logical block and inline axes.
Right-to-Left Writing
.container {
direction: rtl;
scroll-padding-inline-start: 40px;
}
In right-to-left horizontal writing, inline-start corresponds to the physical right edge.
Overlapping Declarations
Physical and logical scroll padding properties can affect the same edge. When they do, the CSS cascade determines which declaration applies.
Choosing Between Physical and Logical Properties
Use physical scroll padding when offsets must remain on specific screen edges. Use logical scroll padding when offsets should adapt to writing direction and writing mode.
scroll-padding Versus scroll-margin
Both properties influence scroll alignment, but they are applied to different elements.
| Property | Applied To | Purpose |
|---|---|---|
scroll-padding |
Scroll container | Defines the optimal viewing region inside the scrollport. |
scroll-margin |
Scroll target | Expands or contracts the target's scroll alignment area. |
Container-Side Scroll Padding
.gallery {
scroll-padding: 20px 40px;
}
All targets in the gallery use the container's scroll alignment insets.
Target-Side Scroll Margin
.gallery .featured {
scroll-margin: 20px;
}
The featured target receives its own additional scroll alignment area.
Combining Both Properties
.gallery {
scroll-padding-left: 20px;
}
.gallery .item {
scroll-margin-left: 30px;
}
For left-edge alignment, these values can produce a combined 50-pixel offset between the target's left border edge and the scrollport's left edge.
Important Difference
scroll-padding accepts percentages and auto, but does not accept negative values. scroll-margin accepts negative lengths but does not accept percentages or auto.
scroll-padding Example
This interactive example compares two horizontal scroll snap containers. The second container applies 40 pixels of scroll padding on its left and right edges.
HTML Example
<div class="scroll-padding-demo">
<div class="scroll-example">
<h3>Without Scroll Padding</h3>
<div class="scroll-panel">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item target">Target</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<button type="button">Scroll to Target</button>
</div>
<div class="scroll-example with-padding">
<h3>With Scroll Padding</h3>
<div class="scroll-panel">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item target">Target</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<button type="button">Scroll to Target</button>
</div>
</div>
CSS Example
.scroll-padding-demo {
display: grid;
gap: 20px;
}
.scroll-padding-demo .scroll-panel {
display: flex;
gap: 12px;
overflow-x: auto;
padding-block: 10px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
scroll-snap-type: x mandatory;
}
.scroll-padding-demo .item {
flex: 0 0 150px;
padding: 25px 10px;
text-align: center;
background-color: #e2e8f0;
scroll-snap-align: start;
}
.scroll-padding-demo .target {
background-color: #bfdbfe;
border: 2px solid #2563eb;
}
.scroll-padding-demo .with-padding .scroll-panel {
scroll-padding: 0 40px;
}
.scroll-padding-demo button {
margin-top: 10px;
padding: 8px 12px;
cursor: pointer;
}
JavaScript Example
document.querySelectorAll(".scroll-padding-demo .scroll-example").forEach((example) => {
const button = example.querySelector("button");
const target = example.querySelector(".target");
button.addEventListener("click", () => {
target.scrollIntoView({
behavior: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "instant" : "smooth",
inline: "start",
block: "nearest"
});
});
});
Without Scroll Padding
With Scroll Padding
Click each button to bring the target toward the left edge of its scrolling panel. The second panel uses a 40-pixel scroll padding inset, so the target can align farther from the left edge.
Increasing the Scroll Padding
.scroll-padding-demo .with-padding .scroll-panel {
scroll-padding: 0 70px;
}
The left and right alignment insets increase to 70 pixels.
Removing the Scroll Padding
.scroll-padding-demo .with-padding .scroll-panel {
scroll-padding: 0;
}
The additional alignment insets are removed.
Browser Compatibility Note
The final scroll position depends on the available scroll range and the browser's scroll snapping behavior. Targets near the beginning or end of the container may not reach their requested alignment.
Accessibility Considerations
Scroll padding can improve navigation by helping prevent important content from being obscured by sticky headers or other interface elements.
Keep Anchor Targets Visible
Use appropriate scroll padding when anchor navigation could place headings underneath a fixed or sticky header.
Maintain Keyboard Accessibility
Ensure that scrolling controls remain keyboard accessible and provide visible focus indicators.
Consider Focus Management
Programmatic scrolling does not automatically move keyboard focus. Scripted navigation may require additional focus handling.
Respect Reduced Motion Preferences
The property does not animate scrolling. If smooth scrolling is used, respect prefers-reduced-motion.
Support Different Writing Directions
Consider logical scroll padding properties when interfaces must adapt to right-to-left or vertical writing modes.
Test Responsive Layouts
Verify scroll alignment at different viewport sizes, browser zoom levels, and text scaling settings.
Common Mistakes
Using Negative Values
.container {
scroll-padding: -20px;
}
Negative values are invalid. Scroll padding accepts only non-negative lengths, percentages, or auto.
Providing Too Many Values
.container {
scroll-padding: 10px 20px 30px 40px 50px;
}
The shorthand accepts a maximum of four values.
Confusing Scroll Padding With Normal Padding
scroll-padding defines scroll alignment insets, while regular padding affects layout spacing inside an element.
Applying the Property to Scroll Targets
Apply scroll-padding to the scroll container. Use scroll-margin on individual scroll targets.
Expecting Scroll Snapping Automatically
The property does not enable scroll snapping. The container and its targets still need the appropriate scroll snap properties.
Using the Wrong Element for Viewport Scrolling
For document scrolling, use the root html element rather than assuming that applying scroll padding to body will control the viewport.
Ignoring Declaration Order
A later scroll-padding shorthand can override earlier physical scroll padding longhand declarations.
Confusing Scroll Padding With Smooth Scrolling
Scroll padding affects alignment, while scroll-behavior or JavaScript scrolling options control scrolling animation.
Ignoring Available Scroll Range
Large scroll padding values may not produce the expected offset when the container cannot scroll far enough.
Important Considerations
Keep the following points in mind when using scroll-padding:
- It is a shorthand for the four physical scroll padding properties.
- It accepts one to four values in clockwise order.
- Each value can be a non-negative length, percentage, or
auto. - The initial value of each longhand is
auto. - The property is not inherited.
- Negative values are invalid.
- Percentages are relative to the scrollport dimension along the corresponding axis.
- It defines the optimal viewing region inside a scroll container.
- It does not change normal layout spacing or element dimensions.
- It can influence scroll snapping, anchor navigation, and programmatic scrolling.
- It does not create a scroll container or enable scroll snapping.
- Use
scroll-padding-toponhtmlto account for sticky headers during document scrolling. - Use
scroll-marginwhen individual targets need their own alignment offsets. - Logical scroll padding properties adapt to writing mode and text direction.
- Available scroll range may limit the final alignment position.
- Test scrolling behavior across different viewport sizes and input methods.
Browser Support
The CSS scroll-padding property is Baseline Widely available and is supported in current versions of major browsers.
Older browsers may have limited support. The visible effect can also depend on the scrolling operation and available scroll range.
For current compatibility information, visit Can I Use? and search for scroll-padding.
When unsupported, scrolling generally remains functional, but the additional alignment insets may not be applied.
Specifications
The scroll-padding property is defined in CSS Scroll Snap Module Level 1.
CSS Scroll Snap Module Level 1
The specification defines how scroll padding establishes the optimal viewing region within a scrollport.
For additional documentation and examples, consult MDN Web Docs.
