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

瀏覽器相容性

另見