The CSS scroll-padding property defines the βsafe spaceβ inside a scroll container when using scroll snapping or programmatic scrolling. It works like padding for the scroll viewport (not the element itself), ensuring that the target element never touches the container edges during scrolling.
Note
β Affects snapping and scrollIntoView() behavior
β Does NOT change layout β only scroll offset inside container
π¦ Syntax
scroll-padding syntax
scroll-padding: <length> | <percentage>;| Property | Description |
|---|---|
| scroll-padding | Shorthand for all sides |
| scroll-padding-top | Top safe area |
| scroll-padding-right | Right safe area |
| scroll-padding-bottom | Bottom safe area |
| scroll-padding-left | Left safe area |
π scroll-padding vs scroll-margin
| Property | What It Affects? |
|---|---|
| scroll-padding | Adjusts the scroll containerβs safe area |
| scroll-margin | Adjusts the scroll targetβs offset |
Note
β Use scroll-margin on the **item being snapped to**
π¨ 1. Basic Example β Add Safe Space Around Snapping
Basic scroll-padding
.container {
scroll-snap-type: y mandatory;
scroll-padding-top: 50px;
}Now the snapped element will appear 50px below the top of the container.
π¨ 2. Horizontal Sliders / Carousels
Horizontal snap padding
.slider {
scroll-snap-type: x mandatory;
scroll-padding-left: 20px;
}Great for centering cards inside sliders.
π¨ 3. Full-Page Scroll-Snap Layout
Full screen snap
body {
scroll-snap-type: y mandatory;
scroll-padding-top: 60px; /* space for sticky header */
}Note
π¨ 4. Shorthand Version
scroll-padding shorthand
.container {
scroll-padding: 40px 20px;
}Same order as CSS padding:top right bottom left
π¨ 5. Use With scrollIntoView()
scrollIntoView + scroll-padding
.scroll-area {
scroll-padding-top: 80px;
}JavaScript
document.querySelector('#section2')
.scrollIntoView({ behavior: 'smooth' });The section stops with 80px of space above it β without modifying layout.
π¨ 6. Responsive scroll-padding
Adjust snap behavior depending on screen size.
Responsive scroll-padding
.carousel {
scroll-padding: 10px;
}
@media (min-width: 768px) {
.carousel {
scroll-padding: 40px;
}
}π§ͺ Real-World Use Cases
1οΈβ£ Fix Snapping With Sticky Headers
Header offset
.page {
scroll-snap-type: y mandatory;
scroll-padding-top: 70px;
}2οΈβ£ Center Slides in a Slider
Card slider padding
.card-slider {
scroll-snap-type: x proximity;
scroll-padding-left: 30px;
}3οΈβ£ Improve Scroll Navigation in Dashboards
Dashboard panels
.panel-container {
scroll-snap-type: y proximity;
scroll-padding-top: 20px;
}β οΈ Common Mistakes
- β Expecting it to add real padding (it only affects scroll placement)
- β Applying it to items instead of containers
- β Forgetting scroll-snap-type must be set for snapping layouts
- β Assuming it will affect mouse-wheel scrolling (it affects snap positions only)
Note
scroll-margin = element offset
π₯ Summary
scroll-padding is essential for creating smooth, visually comfortable scroll-snapping experiences. It ensures elements never snap too close to edges, especially when sticky headers or centered layouts are involved.