CSS - scroll-padding-块起始



CSS scroll-padding-block-start 指定元素在块维度上的滚动填充的起始边缘的偏移量。

可能的值

  • <length-percentage> − 有效的长度或百分比值,定义从视口对应边缘向内偏移。

    <auto> − 由用户代理确定的从视口对应边缘向内偏移。

应用于

所有滚动容器。

语法

scroll-padding-block-start =  auto | <length-percentage>

CSS scroll-padding-block-start - 长度值

以下示例演示了如何使用scroll-padding-block-start 属性来设置 div 元素在块维度上的滚动填充的起始边缘的偏移量,使用长度值 (30px)。

<html>
<head>
<style>
   #container {
      width: 50%;
      aspect-ratio: 3/1;
      padding: 40px 0;
      border: solid black 2px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
      scroll-padding-block-start: 30px;
   }

   .boxA {
      background-color: rgb(234, 234, 128);
      width: 85%;
      aspect-ratio: 3/1;
   }

   .boxB {
      background-color: lightgreen;
      width: 70%;
      aspect-ratio: 4/1;
   }

   .boxA, .boxB {
      margin: 2px;
      scroll-snap-align: start none;
   }
</style>
</head>
<body>
   <h3>CSS scroll-padding-block-start property.</h3>
   <p>scroll-padding-block-start property sets the offset for the start edge of the scrollport of an element in the block dimension.</p>

   <div id="container">
      <div class="boxA"></div>
      <div class="boxB"></div>
      <div class="boxA"></div>
      <div class="boxB"></div>
      <div class="boxA"></div>
   </div>
</body>
</html>

CSS scroll-padding-block-start - 百分比值

以下示例演示了如何使用scroll-padding-block-start 属性来设置 div 元素在块维度上的滚动填充的起始边缘的偏移量,使用百分比值 (60%)。

<html>
<head>
<style>
   #container {
      width: 50%;
      aspect-ratio: 3/1;
      padding: 40px 0;
      border: solid black 2px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
      scroll-padding-block-start: 60%;
   }

   .boxA {
      background-color: rgb(234, 234, 128);
      width: 85%;
      aspect-ratio: 3/1;
   }

   .boxB {
      background-color: lightgreen;
      width: 70%;
      aspect-ratio: 4/1;
   }

   .boxA, .boxB {
      margin: 2px;
      scroll-snap-align: start none;
   }
</style>
</head>
<body>
   <h3>CSS scroll-padding-block-start property.</h3>
   <p>scroll-padding-block-start property sets the offset for the start edge of the scrollport of an element in the block dimension.</p>

   <div id="container">
      <div class="boxA"></div>
      <div class="boxB"></div>
      <div class="boxA"></div>
      <div class="boxB"></div>
      <div class="boxA"></div>
   </div>
</body>
</html>

CSS scroll-padding-block-start - auto 值

以下示例演示了如何使用scroll-padding-block-start 属性来设置 div 元素在块维度上的滚动填充的起始边缘的偏移量,使用关键字值 (auto)。

<html>
<head>
<style>
   #container {
      width: 50%;
      aspect-ratio: 3/1;
      padding: 40px 0;
      border: solid black 2px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
      scroll-padding-block-start: auto;
   }

   .boxA {
      background-color: rgb(234, 234, 128);
      width: 85%;
      aspect-ratio: 3/1;
   }

   .boxB {
      background-color: lightgreen;
      width: 70%;
      aspect-ratio: 4/1;
   }

   .boxA, .boxB {
      margin: 2px;
      scroll-snap-align: start none;
   }
</style>
</head>
<body>
   <h3>CSS scroll-padding-block-start property.</h3>
   <p>scroll-padding-block-start property sets the offset for the start edge of the scrollport of an element in the block dimension.</p>

   <div id="container">
      <div class="boxA"></div>
      <div class="boxB"></div>
      <div class="boxA"></div>
      <div class="boxB"></div>
      <div class="boxA"></div>
   </div>
</body>
</html>
广告