Long rows of cards can stop with text cut off at the edge of the screen. CSS scroll snapping lets the browser settle on a card after horizontal scrolling.
Snap behavior belongs to both the scrolling container and its children. The container chooses the scrolling axis and snapping strength, while each card supplies an alignment point. Scroll padding keeps that point inset from the container's edge.
Mandatory snapping suits short cards that fit within the scrolling area. Long or uneven panels may need proximity snapping so the browser can stop between targets. Normal overflow scrolling and a visible keyboard focus indicator remain part of an accessible card row.
Steps to create CSS scroll snapping:
- Create index.html in your text editor with the page structure below.
- index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Scroll snap cards</title> <link rel="stylesheet" href="styles.css"> </head> <body> <main> <h1>Featured products</h1> <p>Scroll sideways to explore the cards.</p> </main> </body> </html>
- Insert the card row immediately before the closing </main> tag in index.html.
<div class="snap-gallery" role="region" aria-label="Featured products" tabindex="0"> <article class="snap-card"> <h2>Suite</h2> <p>Plan projects and keep your team together.</p> </article> <article class="snap-card"> <h2>Reports</h2> <p>Explore monthly totals and recent trends.</p> </article> <article class="snap-card"> <h2>Audit</h2> <p>Review changes and track important events.</p> </article> </div>
tabindex=“0” makes the named region reachable with the Tab key. Its cards stay in document order.
- Create styles.css beside index.html with the horizontal layout below.
- styles.css
* { box-sizing: border-box; } body { margin: 0; padding: 2rem 1rem; font: 1rem/1.5 system-ui, sans-serif; color: #172b4d; background: #f4f7fb; } main { max-width: 38rem; margin-inline: auto; } .snap-gallery { display: flex; gap: 1rem; overflow-x: auto; padding: 1rem; border: 2px solid #526783; } .snap-card { flex: 0 0 100%; padding: 1.5rem; border: 2px solid #2656a5; border-radius: 0.5rem; background: #fff; } .snap-gallery:focus-visible { outline: 3px solid #a33d00; outline-offset: 4px; }
flex: 0 0 100% gives each card the row's content width without shrinking. The cards overflow inside the row rather than widening the page. At this point the row scrolls without snapping.
- Append the snapping rules at the end of styles.css.
.snap-gallery { scroll-snap-type: x mandatory; scroll-padding-inline: 1rem; } .snap-card { scroll-snap-align: start; }
x mandatory makes horizontal scrolling settle on a snap target, and start aligns the target's starting edge. The scroll padding matches the row's visible padding. If free stopping between cards is preferable, x proximity makes snapping less strict. Browsers without scroll snap support retain the overflow layout.
- Save both files in your text editor.
- Open index.html in your browser.

- Drag the row's horizontal scrollbar toward the middle card.
Reports is the middle card. A horizontal trackpad gesture also scrolls the row; with the row focused, arrow keys provide keyboard scrolling.
- Check that the Reports card settles at the left inset with its text fully visible.
A card left halfway across the row indicates that snapping did not take effect. Both the container's scroll-snap-type and the cards' scroll-snap-align are needed.