Set scroll padding to 20px from the container to the snap position:
div {
scroll-padding: 20px;
}
Try it Yourself »
More "Try it Yourself" examples below.
The scroll-padding
property specifies the distance from the container to the snap position of child elements.
This means that when you stop scrolling, the scrolling will quickly adjust and stop at a specified distance from the container to the snap position of the child element in focus.
Snap position is the position on the child element where it snaps into place in the container when you stop scrolling.
Note: In the example above, scroll padding is set on all sides, but only the scroll padding on the top side changes the scrolling behaviour because the scroll-snap-align is set to "start".
The scroll-padding
property is a shorthand property for the following properties:
Values for the scroll-padding
property can be set in different ways:
If the scroll-padding property has four values:
If the scroll-padding property has three values:
If the scroll-padding property has two values:
If the scroll-padding property has one value:
To see the effect from the scroll-padding
property, the scroll-snap-align
property must be set on the child elements, and the scroll-padding
and scroll-snap-type
properties must be set on the parent element.
Default value: | auto |
---|---|
Inherited: | no |
Animatable: | no. Read about animatable |
Version: | CSS3 |
JavaScript syntax: | object.style.scrollPadding="20px" Try it |
The numbers in the table specify the first browser version that fully supports the property.
Property | |||||
---|---|---|---|---|---|
scroll-padding | 69.0 | 79.0 | 68.0 | 14.1 | 56.0 |
scroll-padding: auto|
value|initial|inherit;
Value | Description |
---|---|
auto | Default value. The browser calculates the padding |
length | Specifies scroll-padding in px, pt, cm, etc. Negative values are not allowed. Read about length units |
% | Specifies the padding in percent of the width of the containing element |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
The scroll-padding
property can be used in an image gallery with snap behaviour, to push images below a fixed element:
#container {
scroll-padding: 30px 0 0 0;
}
The scroll-padding
property can be set at both the bottom and the right side of the container. Scroll to the next element both horizontally and vertically to see the effect:
#container {
scroll-padding: 0 10px 30px 0;
}
CSS scroll-padding-bottom property: CSS Scroll-padding-bottom property
CSS scroll-padding-left property: CSS Scroll-padding-left property
CSS scroll-padding-right property: CSS Scroll-padding-right property
CSS scroll-padding-top property: CSS Scroll-padding-top property
CSS scroll-snap-align property: CSS Scroll-snap-align property
CSS scroll-snap-type property: CSS Scroll-snap-type property
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!