Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS scroll-padding-block Property

Posted in CSS Tutorial
Updated on Jul 20, 2024
By Mari Selvan
👁️ 8 - Views
⏳ 4 mins
💬 1 Comment
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.

Syntax
Copied
Copy To Clipboard
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

ValueDescription
lengthDefines a fixed amount of padding using units like px, em, rem, etc.
percentageDefines padding as a percentage of the containing block's size.
autoThe 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.

index.html
Copied
Copy To Clipboard
<!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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy