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