The scroll-padding
property is a CSS feature that defines the optimal viewing region of a scroll container, allowing you to create a padding-like offset from the edges. This is particularly useful for preventing content from being obscured by fixed elements like headers or navigation bars when a user navigates to an anchor link.
Example 1: Basic scroll-padding
/* Sets a 20-pixel scroll padding on all sides of the scroll container */
.scroll-container {
scroll-padding: 20px;
height: 200px;
overflow-y: scroll;
}
Explanation: This code applies a 20-pixel padding on all four sides of the .scroll-container
. When you navigate to an element within this container, the scrolling will stop 20 pixels before the edge.
Example 2: scroll-padding-top
/* Adds a 50-pixel offset from the top of the scroll container */
.container {
scroll-padding-top: 50px;
height: 250px;
overflow-y: scroll;
}
Explanation: The scroll-padding-top
property is used here to ensure that when a user jumps to an anchor link within .container
, there is a 50-pixel space from the top, preventing the target element from being hidden under a fixed header.
Example 3: scroll-padding with different values
/* Applies 20px to top/bottom and 40px to left/right */
.scroll-box {
scroll-padding: 20px 40px;
height: 300px;
overflow-y: auto;
}
Explanation: This shorthand sets the scroll-padding-top
and scroll-padding-bottom
to 20 pixels, and scroll-padding-left
and scroll-padding-right
to 40 pixels.
Example 4: Using logical properties
/* Sets scroll padding at the start of the block direction */
.logical-padding {
scroll-padding-block-start: 3rem;
height: 200px;
overflow-y: scroll;
}
Explanation: The scroll-padding-block-start
property is a logical property that corresponds to scroll-padding-top
in a horizontal writing mode. This ensures predictable behavior regardless of the document's writing direction.
Example 5: Individual side scroll padding
/* Sets individual scroll padding for each side */
.individual-padding {
scroll-padding-top: 10px;
scroll-padding-right: 20px;
scroll-padding-bottom: 15px;
scroll-padding-left: 25px;
height: 220px;
overflow-y: scroll;
}
Explanation: This example demonstrates how to set different scroll padding values for each side of the scroll container individually, offering fine-grained control over the scroll snapping behavior.
Example 6: scroll-padding with smooth scrolling
/* Combines scroll-padding with smooth scrolling for a better user experience */
html {
scroll-behavior: smooth;
}
.smooth-scroll-container {
scroll-padding-top: 60px;
height: 280px;
overflow-y: scroll;
}
Explanation: Here, scroll-padding-top
is used in conjunction with scroll-behavior: smooth
on the root element. This combination creates a smooth scrolling effect to anchor links while maintaining the desired top offset.
Example 7: Using calc() for dynamic scroll-padding
/* Dynamically calculates the scroll padding based on the viewport height */
:root {
--header-height: 70px;
}
.dynamic-padding {
scroll-padding-top: calc(var(--header-height) + 10px);
height: 300px;
overflow-y: scroll;
}
Explanation: This advanced example uses calc()
and CSS variables to set the scroll-padding-top
. This makes the scroll padding responsive; if the header height changes, the padding will adjust accordingly.