Menu

How to Create Horizontal Scroll Snap in CSS

How to Create Horizontal Scroll Snap in CSS

Scrolling is an essential part of modern web browsing, but when done poorly, it can result in a clunky or frustrating user experience. Think about websites that have endless scrolling, jittery transitions, or inconsistent movement between sections. That’s where CSS Scroll Snap comes into play. It allows developers to create precise and smooth scrolling interactions that snap to elements, improving the overall user experience. 

In this article, I shall break down what CSS Scroll Snap is, how it works, and the key properties you need to know to start using it effectively. By the end, you'll have a solid foundation to create seamless scroll snap layouts for your next project.

What is CSS scroll snap?

CSS Scroll Snap is a more modern feature of CSS that gives developers control over how the browser scrolls between different sections or elements on a web page. It ensures that the scroll action stops at specific points (snap points), providing a more intentional and fluid interaction compared to the traditional free-flowing scroll.

Item 1
Item 2
Item 3
Item 4
Item 5

With this technique, sections of a page can "snap" into place when the user scrolls, improving the clarity and aesthetics of navigation, especially on touch devices or when working with large content sections. This is ideal for image carousels, multi-section layouts, and product displays.

Note that you can setup scroll snapping both horizontally and vertically, but in this article I will just be breaking down the horizontal version. Though the same principles will apply to the vertical.

How does CSS scroll snap work?

CSS Scroll Snap relies on a combination of container and child element properties. These properties define how and where the snapping occurs, creating a grid or axis that the scroll movement follows.

There are two main parts to a scroll snap:

Scroll container: This is the parent element that controls the scrolling behavior.

Scroll snap child: These are the elements inside the container that will snap into position.

Key CSS properties for scroll snap

.scroll-container {
            display: flex;
            overflow-x: scroll;
            scroll-snap-type: x mandatory;
            scroll-behavior: smooth;
            width: 100vw;
            height: 300px;
        }

        .scroll-item {
            scroll-snap-align: start;
            flex: none;
            width: 100vw;
            height: 100%;
            background-color: #ccc;
            font-size: 24px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

To achieve the smooth scroll effect you see above, you need to use several key CSS properties. Here are the more important onse:

1. scroll-snap-type - The scroll-snap-type property is applied to the container and defines the snapping axis and behavior.

x or y defines the axis (horizontal or vertical). mandatory forces the scroll to always snap to a point. proximity allows the scroll to snap only if it's close enough to the snap point.

2. scroll-snap-align - The scroll-snap-align property is applied to the child elements (the ones you want to snap) and determines the snap alignment.

Possible values: start: Aligns the snap element with the start of the scroll container.

center: Centers the snap element in the container.

end: Aligns the snap element with the end of the container.

3. scroll-snap-stop - This optional property enhances the control over snapping by specifying whether an element can skip snapping or if it must always snap into place.

The default value is normal, which allows skipping some snap points based on scrolling speed, while always enforces the snap to each element.

4. overflow - For scroll snapping to work, you need to ensure that the container has scrolling enabled. This is done using overflow.

This will ensure the container scrolls horizontally but not vertically.

Creating a simple scroll snap layout

Let’s put these concepts into practice by creating a basic example of a horizontal image gallery that snaps to each image as the user scrolls.

<!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</title>
    <style>
        .scroll-container {
            display: flex;
            overflow-x: scroll;
            scroll-snap-type: x mandatory;
            scroll-behavior: smooth;
            width: 100vw;
            height: 300px;
        }

        .scroll-item {
            scroll-snap-align: start;
            flex: none;
            width: 100vw;
            height: 100%;
            background-color: #ccc;
            font-size: 24px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .scroll-item:nth-child(odd) {
            background-color: #6ec1e4;
        }
    </style>
</head>
<body>

<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 class="scroll-item">Item 5</div>
</div>

</body>
</html>

Explanation:

Container: The .scroll-container is the parent element that scrolls horizontally and snaps to each item with the scroll-snap-type property.

Items: Each .scroll-item uses the scroll-snap-align: start rule to snap to the start of the container when scrolled.

The scroll-behavior: smooth rule ensures that the scroll action is visually appealing.

Enhancing the scroll experience with scroll-behavior

In addition to CSS Scroll Snap, you can improve the user experience by making scroll behavior smoother using the scroll-behavior property.

html {
    scroll-behavior: smooth;
}

This simple rule makes all scroll interactions on your webpage smooth and fluid, eliminating abrupt jumps between sections. It works well in combination with Scroll Snap for even more intuitive navigation.

Best practices

To make the most of CSS Scroll Snap, here are some best practices to follow:

Test Across Devices: Ensure your scroll snap interactions are smooth on both desktop and mobile devices. Touch interactions can sometimes feel different from mouse-driven scrolling.

Use Snap Sparingly: While scroll snapping can create a sleek experience, overuse can become overwhelming. Be strategic and use it only where it improves clarity, like in carousels or key sections.

Combine with JavaScript for Enhanced Effects: Although CSS Scroll Snap is powerful, combining it with JavaScript can allow for more complex behaviors, like triggering animations or events when sections snap into place.

Ensure Accessibility: Keep in mind users who might rely on keyboard navigation or screen readers. Make sure your scroll snapping doesn't hinder their experience.

Conclusion

CSS Scroll Snap is a useful feature for developers who want to create smooth, natural, and user-friendly scrolling experiences. Whether you’re building a visually immersive product page or a simple horizontal gallery, Scroll Snap makes it easy to control the scroll action. But, you should consider the context and use cases carefully as taking over the scroll behavior may not always be the idea solution.

Walter G. author of blog post
Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.

Get the latest programming news directly in your inbox!

Have a question on this article?

You can leave me a question on this particular article (or any other really).

Ask a question

Community Comments

No comments posted yet
Ad Unit

Current Poll

Total Votes:
Q:
Submit

Add a comment