CSS Properties
CSS scroll-padding-block Property
Photo Credit to CodeToFun
🙋 Introduction
The scroll-padding-block
property in CSS is a shorthand property that sets the scroll padding of an element in the block dimension.
This property is used to define the space between the snap position and the edges of the scroll container in the block axis (top and bottom in a vertical writing mode, left and right in a horizontal writing mode). It is part of the CSS Scroll Snap module and helps to create a better scroll snapping experience.
💡 Syntax
The syntax for the scroll-padding-block
property is straightforward. It can accept one or two values.
element {
scroll-padding-block: | ;
}
- If one value is specified, it applies the same padding to both the start and end.
- If two values are specified, the first value applies to the start, and the second value applies to the end.
🎛️ Default Value
The default value for the scroll-padding-block
property is auto, which means that no extra padding is applied.
🏠 Property Values
Value | Description |
---|---|
length | Defines a fixed amount of padding using units like px, em, rem, etc. |
percentage | Defines padding as a percentage of the containing block's size. |
auto | The browser automatically calculates the padding. |
📄 Example
In this example, we'll apply scroll padding to a container to ensure that items snap with some padding from the top and bottom edges.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS scroll-padding-block Example</title>
<style>
.scroll-container {
width: 300px;
height: 200px;
overflow: auto;
scroll-snap-type: y mandatory;
border: 1px solid #ccc;
scroll-padding-block: 20px;
}
.scroll-item {
width: 100%;
height: 150px;
scroll-snap-align: start;
background: #f0f0f0;
border-bottom: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>Scroll Padding Block Example</h1>
<div class="scroll-container">
<div class="scroll-item">Item 1</div>
<div class="scroll-item">Item 2</div>
<div class="scroll-item">Item 3</div>
<div class="scroll-item">Item 4</div>
</div>
</body>
</html>
🖥️ Browser Compatibility
The scroll-padding-block
property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. It is always recommended to test your website across different browsers to ensure compatibility.
🎉 Conclusion
The scroll-padding-block
property is a useful tool for enhancing the scroll snapping experience on your web pages. By defining padding in the block direction, you can create a smoother and more visually appealing scroll behavior. Experiment with different padding values to see how this property can improve the user experience on your site.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS scroll-padding-block Property), please comment here. I will help you immediately.