← 返回首页
scroll-margin-top - CSS:层叠样式表 | MDN

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

scroll-margin-top

基线 广泛可用

自 2021年4月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

scroll-margin-top 属性定义了滚动吸附区域的上外边距,此区域用于将此盒吸附至滚动口。滚动吸附区域的确定方法为:取变换后的边框盒,求其矩形包围盒(与滚动容器的坐标空间中的轴对齐),再加上指定的外边距。

本文内容

尝试一下

scroll-margin-top: 0;
scroll-margin-top: 20px;
scroll-margin-top: 2em;
<section class="default-example" id="default-example"> <div class="scroller"> <div>1</div> <div id="example-element">2</div> <div>3</div> </div> <div class="info">Scroll »</div> </section>
.default-example .info { inline-size: 100%; padding: 0.5em 0; font-size: 90%; writing-mode: vertical-rl; } .scroller { text-align: left; height: 250px; width: 270px; overflow-y: scroll; display: flex; flex-direction: column; box-sizing: border-box; border: 1px solid black; scroll-snap-type: y mandatory; } .scroller > div { flex: 0 0 250px; background-color: rebeccapurple; color: #fff; font-size: 30px; display: flex; align-items: center; justify-content: center; scroll-snap-align: start; } .scroller > div:nth-child(even) { background-color: #fff; color: rebeccapurple; }

语法

css
/* <length> 值 */ scroll-margin-top: 10px; scroll-margin-top: 1em; /* 全局值 */ scroll-margin-top: inherit; scroll-margin-top: initial; scroll-margin-top: revert; scroll-margin-top: revert-layer; scroll-margin-top: unset;

取值

<length>

滚动容器的上外边距。

形式定义

初始值适用元素是否是继承属性计算值动画类型
0
所有元素
as specified
按计算值的类型

形式语法

scroll-margin-top =
<length>
此语法反映了基于 CSS Scroll Snap Module Level 1 的最新标准。并非所有浏览器都已实现全部内容。有关支持信息,请参阅浏览器兼容性

规范

规范
CSS Scroll Snap Module Level 1
# margin-longhands-physical

浏览器兼容性

启用 JavaScript 以查看此浏览器兼容性表。

参见