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.
Practice
snap-x do in a scroll container?Solution
Step 1: Understand the
Thesnap-xclasssnap-xclass in Tailwind setsscroll-snap-type: x mandatory;which means horizontal snapping.Step 2: Compare with other options
Vertical snapping usessnap-y, disabling snapping usessnap-none, and mandatory snapping on all sides is not a valid Tailwind class.Final Answer:
It enables horizontal scroll snapping inside the container. -> Option AQuick Check:
snap-x = horizontal scroll snap [OK]
- Confusing snap-x with vertical snapping
- Thinking snap-x disables snapping
- Assuming snap-x applies to all directions
Solution
Step 1: Identify scroll snap alignment classes
Tailwind usessnap-start,snap-center, andsnap-endto align child elements at the start, center, or end of the snap point.Step 2: Match the requirement
The question asks for alignment at the container's snap point start, sosnap-startis correct.Final Answer:
snap-start -> Option CQuick Check:
snap-start = align at snap point start [OK]
- Choosing snap-center instead of snap-start
- Using snap-none which disables snapping
- Confusing snap-end with snap-start
<div class="overflow-x-auto snap-x"> <div class="snap-center w-64 h-40 bg-blue-400">Item 1</div> <div class="snap-center w-64 h-40 bg-green-400">Item 2</div> <div class="snap-center w-64 h-40 bg-red-400">Item 3</div> </div>What will happen when you scroll horizontally inside the container?
Solution
Step 1: Analyze container classes
The container hasoverflow-x-auto(horizontal scroll),snap-x(horizontal mandatory snap).Step 2: Analyze child classes
Each child hassnap-center, so snapping aligns each item centered in the viewport.Final Answer:
Scrolling will snap horizontally to center each item smoothly. -> Option BQuick Check:
snap-x + snap-center = horizontal center snap [OK]
- Thinking snap-x snaps vertically
- Ignoring snap-mandatory effect
- Assuming snap-start alignment instead of center
<div class="overflow-y-auto snap-x"> <div class="snap-start h-40 bg-yellow-300">Item A</div> <div class="snap-start h-40 bg-yellow-500">Item B</div> </div>What is the main issue preventing scroll snapping?
Solution
Step 1: Check scroll direction and snap type
The container scrolls vertically (overflow-y-auto) but usessnap-xwhich is for horizontal snapping.Step 2: Understand snapping mismatch
Scroll snapping only works if the snap axis matches the scroll direction. Here, vertical scroll with horizontal snap causes no snapping.Final Answer:
Usingsnap-xwith vertical scrolling (overflow-y-auto). -> Option DQuick Check:
Scroll axis must match snap axis [OK]
- Mixing horizontal snap with vertical scroll
- Ignoring child element sizing
- Thinking snap-start vs snap-center affects snapping presence
Solution
Step 1: Identify scroll direction and snapping type
Vertical scroll requiresoverflow-y-autoand vertical snap withsnap-y.Step 2: Choose snapping behavior
Tailwind uses mandatory snapping, sosnap-y snap-mandatoryis correct.Step 3: Set child alignment
Children aligned at the start usesnap-start.Final Answer:
Container: overflow-y-auto snap-y snap-mandatory, Children: snap-start -> Option AQuick Check:
Vertical + start alignment = Container:overflow-y-auto snap-y snap-mandatory, Children:snap-start[OK]
- Using snap-x with vertical scroll
- Confusing snap-mandatory with snap-proximity
- Misaligning children with snap-center instead of snap-start
Ready to prove it?
Pass the test and this pattern moves unknown
