CSS Reference ยท Scrolling, Interaction and UI
The scroll-padding property
Reference for the CSS scroll-padding shorthand: shrink the optimal viewing area of a scroll container so snap points and anchors clear sticky headers.
The scroll-padding property insets the scrollport of a scroll container for the purpose of finding where things should land. When the browser scrolls to a snap point, a #fragment target or a scrollIntoView() target, it treats the padded area as the visible region.
Use it when something covers the edge of a scroll container, such as a sticky header at the top of the page. With scroll-padding-top on the container, every target stops below the header. Unlike regular padding, it takes no space and draws nothing.
html { scroll-padding-top: 5rem; }
Try it
Syntax
scroll-padding: [ auto | <length-percentage> ]{1,4}; scroll-padding-top | -right | -bottom | -left: auto | <length-percentage>; scroll-padding-block | scroll-padding-inline: [ auto | <length-percentage> ]{1,2};
- Initial value:
auto - Applies to: scroll containers
- Inherited: no
- Animatable: yes, as a length or percentage
- Computed value:
auto, or an absolute length or percentage
Values
| Value | Description |
|---|---|
auto |
The browser decides; usually no inset. |
<length> |
A fixed inset, such as 4rem. |
<percentage> |
A percentage of the scrollport's size in the matching dimension. |
Usage notes
- Put it on the box that scrolls. For page-level scrolling, that is
html, notbody. - It shrinks the area used for snapping and anchors only; it does not clip content or move the scrollbar.
- Pair it with
scroll-marginwhen one target needs a different offset from the rest. - For fixed headers whose height changes (for example on small screens), store it in a custom property and use it in both the header height and
scroll-padding-top. - With scroll snap,
scroll-paddingmoves the snap position so astart-aligned slide stops after the inset, which is handy for peeking at the next slide.
Browser support
Supported in all current major browsers.
Related
scroll-marginโ per-element offsetscroll-snap-typeโ turns snapping onscroll-behaviorโ smooth scrolling- Lesson: Scroll Snap