CSS scroll-padding-bottom 屬性

定義和用法

scroll-padding-bottom 屬性指定從容器底部到子元素吸附位置的距離。

吸附位置是指當你停止滾動時,子元素在容器中吸附到位的位置。吸附位置通過 scroll-snap-align 屬性設置,但也可能受到 CSS 屬性 directionwriting-mode 的影響。

注意:此屬性僅在吸附位置設置在子元素底部時才有效。

要看到 scroll-padding-bottom 屬性的效果,必須在子元素上設置 scroll-snap-align 屬性,并在父元素上設置 scroll-padding-bottomscroll-snap-type 屬性。

實例

例子 1

設置從容器底部到吸附位置的滾動填充為 20px:

div {
  scroll-padding-bottom: 20px;
}

親自試一試

例子 2:圖片庫

在具有吸附行為的圖片畫廊中,可以使用 scroll-padding-bottom 屬性將圖片推到固定元素上方:

#container {
  scroll-padding-bottom: 30px;
}

親自試一試

例子 3:設置底部滾動填充

當在兩個方向上設置吸附行為時,也可以在容器上設置 scroll-padding-bottom 屬性。垂直滾動到下一個元素以查看效果:

#container {
  scroll-padding-bottom: 30px;
}

親自試一試

例子 4:吸附位置

為了使 scroll-padding-bottom 屬性生效,吸附位置必須設置在子元素的底部。在此示例中,writing-mode 屬性將吸附位置從子元素底部更改為左側。使用這樣的代碼時,scroll-padding-bottom 屬性將不再起作用:

#container {
  writing-mode: vertical-rl;
  scroll-padding-bottom: 30px;
}
#container > div {
  scroll-snap-align: end none;
}

親自試一試

CSS 語法

scroll-padding-bottom: auto|value|initial|inherit;

屬性值

描述
auto 默認值。瀏覽器計算填充。
length

以 px、pt、cm 等單位指定 scroll-padding-bottom。

不允許使用負值。請參閱:CSS 單位

% 指定包含元素寬度的百分比作為內邊距。
initial 將此屬性設置為其默認值。參閱 initial
inherit 從其父元素繼承此屬性。參閱 inherit

技術細節

默認值: auto
繼承性:
動畫制作: 不支持。請參閱:動畫相關屬性
版本: CSS3
JavaScript 語法: object.style.scrollPaddingBottom="20px"

瀏覽器支持

表格中的數字表示首個完全支持該屬性的瀏覽器版本。

Chrome Edge Firefox Safari Opera
69.0 79.0 68.0 14.1 56.0

相關頁面

參考:CSS direction 屬性

參考:CSS scroll-snap-align 屬性

參考:CSS scroll-snap-type 屬性

參考:CSS writing-mode 屬性