CSS scroll-margin Property
Example
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.
Definition and Usage
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:
- scroll-margin: 15px 30px 60px 90px;
- top distance is 15px
- right distance is 30px
- bottom distance is 60px
- left distance is 90px
If the scroll-margin property has three values:
- scroll-margin: 15px 30px 60px;
- top distance is 15px
- left and right distances are 30px
- bottom distance is 60px
If the scroll-margin property has two values:
- scroll-margin: 15px 30px;
- top and bottom distances are 15px
- left and right distances are 30px
If the scroll-margin property has one value:
- scroll-margin: 10px;
- all four distances are 10px
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 |
Browser Support
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 |
CSS Syntax
scroll-margin: 0|value|initial|inherit;
Property Values
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 |
More Examples
Image gallery
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;
}
![alley](https://www.w3schools.com/cssref/img_chania.jpg)
![bridge in jungle](https://www.w3schools.com/cssref/img_forest_narrow.jpg)
![mountains](https://www.w3schools.com/cssref/img_mountains.jpg)
![man with camera](https://www.w3schools.com/cssref/img_man_narrow.jpg)
![Cinque Terre](https://www.w3schools.com/cssref/img_5terre.jpg)
Set scroll-margin at bottom and to the right
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;
}
Related Pages
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