scroll-margin-inline
shorthand property
sets the scroll margins of an element in the inline dimension.
此特性是下列 CSS 特性的简写:
/* <length> values */ scroll-margin-inline: 10px; scroll-margin-inline: 1em .5em ; /* Global values */ scroll-margin-inline: inherit; scroll-margin-inline: initial; scroll-margin-inline: unset;
<length>
An outset from the corresponding edge of the scroll container.
The scroll-margin values represent outsets defining the scroll snap area that is used for snapping this box to the snapport. The scroll snap area is determined by taking the transformed border box, finding its rectangular bounding box (axis-aligned in the scroll container’s coordinate space), then adding the specified outsets.
| 初始值 |
0
|
|---|---|
| 适用于 | 所有元素 |
| 继承 | no |
| 计算值 | 如指定 |
| 动画类型 | by computed value type |
<length>{1,2}
This example implements something very similar to the interactive example above, except that here we'll explain to you how it's implemented.
The aim here is to create four horizontally-scrolling blocks, the second and third of which snap into place, near but not quite at the right of each block.
The HTML that represents the blocks is very simple:
<div class="scroller"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>
Let's walk through the CSS. the outer container is styled like this:
.scroller {
text-align: left;
width: 250px;
height: 250px;
overflow-x: scroll;
display: flex;
box-sizing: border-box;
border: 1px solid #000;
scroll-snap-type: x mandatory;
}
The main parts relevant to the scroll snapping are
overflow-x
: scroll
, which makes sure the contents will scroll and not be hidden, and
scroll-snap-type
: x mandatory
, which dictates that scroll snapping must occur along the horizontal axis, and the scrolling will always come to rest on a snap point.
The child elements are styled as follows:
.scroller > div {
flex: 0 0 250px;
width: 250px;
background-color: #663399;
color: #fff;
font-size: 30px;
display: flex;
align-items: center;
justify-content: center;
scroll-snap-align: end;
}
.scroller > div:nth-child(2n) {
background-color: #fff;
color: #663399;
}
The most relevant part here is
scroll-snap-align
: end
, which specifies that the right-hand edges (the "ends" along the x axis, in our case) are the designated snap points.
Last of all we specify the scroll margin values, a different one for the second and third child elements:
.scroller > div:nth-child(2) {
scroll-margin-inline: 1rem;
}
.scroller > div:nth-child(3) {
scroll-margin-inline: 2rem;
}
This means that when scrolling past the middle child elements, the scrolling will snap to
1rem
outside the inline end edge of the second
<div>
,和
2rems
outside the inline end edge of the third
<div>
.
注意
: Here we are setting
scroll-margin
on the start
and
end of the inline axis (x in our case), but only the end edge is really relevant. It would work just as well here to only set a scroll margin on that one edge, for example with
scroll-margin-inline: 0 1rem
,或
scroll-margin-inline-end
: 1rem
.
Try it for yourself:
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Scroll Snap Module Level 1
The definition of 'scroll-margin-inline' in that specification. |
候选推荐 | 初始定义 |
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
scroll-margin-inline
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 68 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 68 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
完整支持
不支持
scroll-margin
scroll-margin-block
scroll-margin-block-end
scroll-margin-block-start
scroll-margin-bottom
scroll-margin-inline
scroll-margin-inline-end
scroll-margin-inline-start
scroll-margin-left
scroll-margin-right
scroll-margin-top
scroll-padding
scroll-padding-block
scroll-padding-block-end
scroll-padding-block-start
scroll-padding-bottom
scroll-padding-inline
scroll-padding-inline-end
scroll-padding-inline-start
scroll-padding-left
scroll-padding-right
scroll-padding-top
scroll-snap-align
scroll-snap-coordinate
scroll-snap-destination
scroll-snap-points-x
scroll-snap-points-y
scroll-snap-stop
scroll-snap-type
scroll-snap-type-x
scroll-snap-type-y