scroll-margin-top

Baseline 已广泛支持

此特性已得到良好支持,可在多种设备和浏览器版本上使用。自 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: white;
  font-size: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  scroll-snap-align: start;
}

.scroller > div:nth-child(even) {
  background-color: white;
  color: rebeccapurple;
}

语法

css
/* <length> values */
scroll-margin-top: 10px;
scroll-margin-top: 1em;

/* Global values */
scroll-margin-top: inherit;
scroll-margin-top: initial;
scroll-margin-top: revert;
scroll-margin-top: revert-layer;
scroll-margin-top: unset;

<length>

从滚动容器顶部边缘开始的偏移量。

正式定义

初始值0
应用于所有元素
继承性
计算值同指定值
动画类型按计算值类型

正式语法

scroll-margin-top = 
<length>

规范

规范
CSS 滚动捕捉模块级别 1
# margin-longhands-physical

浏览器兼容性

另见