Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS overscroll-behavior Property

Posted in CSS Tutorial
Updated on Oct 07, 2024
By Mari Selvan
👁️ 27 - Views
⏳ 4 mins
💬 1 Comment
CSS overscroll-behavior Property

Photo Credit to CodeToFun

🙋 Introduction

The overscroll-behavior property in CSS is used to control the behavior of scroll overflow on a webpage. It defines how the browser handles scrolling when the content reaches the end of a scrollable area.

This property is particularly useful for preventing scroll chaining, where a scroll action in one area can inadvertently scroll other areas.

💡 Syntax

The syntax for the overscroll-behavior property is straightforward. It can be applied to any scrollable element.

Syntax
Copied
Copy To Clipboard
element {
  overscroll-behavior: value;
}

🎛️ Default Value

The default value of the overscroll-behavior property is auto, which means the default browser behavior for overscroll will occur.

🏠 Property Values

ValueDescription
autoThe default behavior, where the page scrolls normally and allows scroll chaining.
containPrevents scroll chaining by containing the scroll action within the element. The element won't scroll other elements.
noneDisables scroll chaining and overscroll bounce effects. The element and the page won't respond to overscroll actions.

📄 Example

In this example, we'll apply the overscroll-behavior property to a scrollable div to prevent scroll chaining.

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 overscroll-behavior Example</title>
  <style>
    .scroll-container {
      width: 300px;
      height: 150px;
      overflow: auto;
      border: 1px solid #ccc;
      overscroll-behavior: contain;
    }
    .content {
      height: 300px;
      background: linear-gradient(to bottom, #f06, #f9a);
    }
  </style>
</head>
<body>
  <h1>Overscroll Behavior Example</h1>
  <div class="scroll-container">
    <div class="content"></div>
  </div>
</body>
</html>

In this example, the overscroll-behavior: contain; style prevents the parent page from scrolling when the user reaches the top or bottom of the scrollable area.

🖥️ Browser Compatibility

The overscroll-behavior property is widely supported in modern browsers, including the latest versions of Chrome, Firefox, Edge, and Safari. However, it's always a good practice to test your website across different browsers to ensure consistent behavior.

🎉 Conclusion

The overscroll-behavior property is a useful tool for controlling scroll behavior on web pages, especially in complex layouts where multiple scrollable areas are present.

By using this property, you can prevent unintended scroll chaining and provide a smoother user experience. Experiment with different values to see how they affect the scrolling behavior 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