Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS scroll-padding-block-start Property

Posted in CSS Tutorial
Updated on Jul 20, 2024
By Mari Selvan
👁️ 10 - Views
⏳ 4 mins
💬 1 Comment
CSS scroll-padding-block-start Property

Photo Credit to CodeToFun

🙋 Introduction

The scroll-padding-block-start property in CSS is used to set the scroll padding at the start of the block axis.

This property is part of the CSS Scroll Snap Module and helps to control the snapping behavior of scroll containers by defining a safe area that offsets the scroll position. It is particularly useful in web applications where precise control over scroll snapping is required.

💡 Syntax

The syntax for the scroll-padding-block-start property is straightforward. It can be applied to any scroll container to adjust the start padding of the block axis.

Syntax
Copied
Copy To Clipboard
element {
  scroll-padding-block-start: length | percentage | auto;
}

🎛️ Default Value

The default value of the scroll-padding-block-start property is auto, which means no specific padding is applied and the browser uses its default scroll padding behavior.

🏠 Property Values

ValueDescription
lengthSpecifies a fixed amount of padding, such as 10px, 2em, etc.
percentageSpecifies the padding as a percentage of the scroll container's block size, such as 10%.
autoUses the browser's default padding behavior.

📄 Example

In this example, we'll set the scroll-padding-block-start property to 20px to add padding to the start of the block axis of a scroll container.

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-start Example</title>
  <style>
    .scroll-container {
      width: 300px;
      height: 200px;
      overflow: auto;
      scroll-snap-type: y mandatory;
      scroll-padding-block-start: 20px;
    }
    .scroll-item {
      height: 150px;
      scroll-snap-align: start;
    }
  </style>
</head>
<body>
  <h1>Scroll Container with Custom Scroll Padding</h1>
  <div class="scroll-container">
    <div class="scroll-item" style="background-color: lightcoral;">Item 1</div>
    <div class="scroll-item" style="background-color: lightblue;">Item 2</div>
    <div class="scroll-item" style="background-color: lightgreen;">Item 3</div>
  </div>
</body>
</html>

🖥️ Browser Compatibility

The scroll-padding-block-start property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your website across different browsers to ensure compatibility.

🎉 Conclusion

The scroll-padding-block-start property provides a valuable tool for web developers looking to fine-tune the scrolling experience on their websites.

By adjusting the padding at the start of the block axis, you can control the snap position and ensure a smoother, more user-friendly scroll behavior. Experiment with different padding values to see how this property can enhance your web projects.

👨‍💻 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