Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS scroll-snap-type Property

Posted in CSS Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 16 - Views
⏳ 4 mins
💬 1 Comment
CSS scroll-snap-type Property

Photo Credit to CodeToFun

🙋 Introduction

The scroll-snap-type property in CSS is a powerful feature that allows developers to create smooth, scroll-snapping behavior for containers with overflow content.

This property is particularly useful for creating carousels, galleries, or any interface where precise control over scroll positions is desired. With scroll-snap-type, you can control the snapping behavior along the horizontal, vertical, or both axes.

💡 Syntax

The syntax for the scroll-snap-type property is straightforward and can be applied to any scroll container.

Syntax
Copied
Copy To Clipboard
element {
  scroll-snap-type: axis snap-strictness;
}
  • axis: Specifies the axis along which snapping occurs. Possible values are x, y, or both.
  • snap-strictness: Specifies the strictness of the snapping behavior. Possible values are mandatory or proximity.

🎛️ Default Value

The default value of the scroll-snap-type property is none, which means that no snapping will occur.

🏠 Property Values

ValueDescription
noneNo snapping will occur.
xSnapping occurs along the horizontal axis.
ySnapping occurs along the vertical axis.
bothSnapping occurs along both the horizontal and vertical axes.
mandatoryThe scroll container must snap to the defined points.
proximityThe scroll container will snap to the defined points if the scrolling is near them.

📄 Example

In this example, we'll create a horizontal scroll container that snaps to each child element.

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-snap-type Example</title>
  <style>
    .scroll-container {
      display: flex;
      overflow-x: scroll;
      scroll-snap-type: x mandatory;
      width: 100%;
    }
    .scroll-item {
      flex: none;
      scroll-snap-align: start;
      width: 100%;
      height: 200px;
      background-color: #f5a623;
      margin-right: 10px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 2em;
      color: white;
    }
  </style>
</head>
<body>
  <h1>Horizontal Scroll Snap</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-snap-type property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. It is always a good practice to test your website across different browsers to ensure compatibility.

🎉 Conclusion

The scroll-snap-type property is a valuable tool for web developers looking to create intuitive and user-friendly scrolling experiences.

By controlling the snapping behavior of scroll containers, you can enhance the navigational flow of your website. Experiment with different configurations to see how this property can improve the usability and aesthetics of 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