CSS scroll-padding-block-start 指定块维度中元素的滚动填充的起始边缘的偏移量。
可能的值
- <length-percentage> - 有效的长度或百分比值,定义从滚动端口的相应边缘向内的偏移量。
- <auto> - 从用户代理确定的滚动端口的相应边缘向内偏移。
适用于
所有滚动容器。
语法
scroll-padding-block-start = auto | <length-percentage>
CSS scroll-padding-block-start - 长度值
以下示例演示如何使用 scroll-padding-block-start 属性使用长度值 (30px) 设置 div 元素在块维度中的滚动填充开始边缘的偏移量。
<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 属性使用百分比值 (60%))设置块维度中 div 元素的滚动填充开始边缘的偏移量。
<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 - 自动值
以下示例演示如何使用 scroll-padding-block-start 属性通过关键字值 (auto) 设置 div 元素在块维度中的滚动填充开始边缘的偏移量。
<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>