Scroll-snap containers help you control how scrolling stops on a page. They make scrolling feel smooth and neat by snapping to specific points.
Scroll-snap containers in Tailwind
scroll-snap-type: <axis> <mandatory | proximity>; scroll-snap-align: <start | center | end>;
scroll-snap-type goes on the container that scrolls.
scroll-snap-align goes on the child elements inside the container.
scroll-snap-type: x mandatory; scroll-snap-align: start;
scroll-snap-type: y proximity; scroll-snap-align: center;
scroll-snap-type: both mandatory; scroll-snap-align: end;
This example creates a horizontal scroll container with three colored slides. The container uses Tailwind's scroll snap classes to snap horizontally to each slide's start edge. You can scroll sideways and the view will stop exactly at each slide.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Tailwind Scroll Snap Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex items-center justify-center min-h-screen bg-gray-100"> <div class="w-80 h-48 overflow-x-auto scroll-snap-x-mandatory flex border border-gray-300 rounded-lg bg-white"> <div class="flex-none w-80 h-48 scroll-snap-align-start bg-red-400 flex items-center justify-center text-white text-2xl font-bold">Slide 1</div> <div class="flex-none w-80 h-48 scroll-snap-align-start bg-green-400 flex items-center justify-center text-white text-2xl font-bold">Slide 2</div> <div class="flex-none w-80 h-48 scroll-snap-align-start bg-blue-400 flex items-center justify-center text-white text-2xl font-bold">Slide 3</div> </div> </body> </html>
Tailwind uses special classes like scroll-snap-x-mandatory for scroll-snap-type: x mandatory;.
Make sure the container has overflow-auto or overflow-scroll to enable scrolling.
Each child inside the container needs a scroll snap alignment class like scroll-snap-align-start.
Scroll-snap containers control where scrolling stops for a smooth experience.
Use scroll-snap-type on the container and scroll-snap-align on children.
Tailwind provides easy classes to add scroll snapping without writing custom CSS.