DEV Community

Paul Golubkov
Paul Golubkov

Posted on

10 5

Improving Horizontal Scroll with CSS Scroll Snap

The pattern with horizontal scroll is frequently found in mobile versions of sites.

For example, on Airbnb's home page:

Airbnb's vertical scroll block example

It looks smooth and the scroll always stops at the beginning of a card, but there are many bad examples on the Internet when the scroll stops in random positions. I'll not provide links to these sites, but I've created a demo for you, please see it on your phone:

codepen

Try to enable and disable CSS Scroll Snap behaviour by toggling the checkbox on top of the page and see a difference when scrolling. The difference is enormous. When CSS scroll snap is enabled, the scroll behaviour is more smooth and looks like a native app.

This behaviour is achieved by 2 simple CSS rules:

.scroll-container {
  scroll-snap-type: x mandatory;
}

.scroll-container-child {
  scroll-snap-align: start;
}
Enter fullscreen mode Exit fullscreen mode

Cool, right? You can read more about CSS Scroll Snap on mdn.

Browser's support

Browser's support table

It has nice browser's support and can be used in production. Can I Use.

That's it

Thank you for reading! Add your reactions and comments.

Follow me on Twitter.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
thomasbnt profile image
Thomas Bnt • Edited

This scroll is very nice, I already added to my website for social network part. Usefull, but I searching how scroll horizontaly on desktop, some posts for that but not really correspond to what I would like to achieve. Scroll with the mouse could be awesome!

Preview of scroll horizontaly on small devices at my website

Collapse
 
paulcodes profile image
Paul Golubkov

Your carousel looks cool, I think it can be useful on desktops

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay