Scroll Snap CSS CodePen: Copy-Paste Patterns for WordPress
Key takeaways
- Scroll snap needs only two properties: scroll-snap-type on the scroll container and scroll-snap-align on each child.
- Use mandatory for full-screen sections and carousels, proximity for mixed or variable-height content.
- Fix sticky-header overlap with scroll-padding-top on the container, or scroll-margin-top on individual targets.
- Wrap scroll-behavior: smooth in a prefers-reduced-motion: no-preference query and keep snap containers focusable for keyboard users.
- Every pattern ships as pure CSS you can paste into Additional CSS in Astra, Kadence, or GeneratePress with no plugin.
Scroll snap CSS works with two properties: scroll-snap-type on the scroll container and scroll-snap-align on each child, and every pattern in this guide ships as a forkable CodePen with the CSS annotated line by line. You can paste each one into a custom HTML block or theme stylesheet on Astra, Kadence, or GeneratePress without touching a builder.
What does scroll-snap-align actually do in CSS?
Scroll snap is two properties working together. scroll-snap-type goes on the scrolling container and declares that it snaps at all, plus how strictly. scroll-snap-align goes on the children and marks which elements are snap targets. Put scroll-snap-type: y mandatory on a wrapper and scroll-snap-align: start on each section, and the browser handles the rest. The full syntax lives in the MDN documentation for scroll-snap-type and scroll-snap-align.
The two properties that make snap work
Think of it as a contract: the container says "I snap," the children say "snap to me." If either side is missing, nothing happens, which is the most common reason a copied snippet sits there doing nothing.
Here's a minimal three-card container you can fork and poke at:
<div data-height="300" data-theme-id="dark" data-slug-hash="scroll-snap-align-demo" data-default-tab="css,result" data-user="midground" class="codepen"></div>
Change mandatory to proximity in the fork and you'll feel the difference immediately, which is the subject of the next section.
start vs center vs end, shown side by side
scroll-snap-align: start lines the child's top edge with the container's top, center centers it, end aligns the bottom. Same markup, one word changed per demo. start suits full-height sections; center suits cards shorter than the viewport.
If you'd rather see this applied to a horizontal strip in a WordPress build, we cover that pattern in our guide to horizontal scroll in WordPress.
When should you use mandatory vs proximity snap?
scroll-snap-type takes a second value that decides how aggressively the browser snaps. Flick between these two CodePens to feel the difference: one uses y mandatory, the other y proximity.
mandatory: the container always lands on a snap point
.container {
scroll-snap-type: y mandatory;
}
.section {
scroll-snap-align: start;
}
After every scroll gesture, the container settles on the nearest section, even if the user only flicked halfway.
proximity: snap only when close
.container {
scroll-snap-type: y proximity;
}
The browser snaps when the scroll position is near a snap point and leaves it alone otherwise. Long-form content stops fighting the reader's thumb.
A rule of thumb for client work
| Situation | Value |
|---|---|
| Full-screen sections, carousels, image galleries | mandatory |
| Mixed content: text, forms, variable-height sections | proximity |
| Tall screens or short sections | proximity |
Mandatory traps users on tall screens: if a section is shorter than the viewport, they can see two sections but only ever land on one. For text reveal effects layered over snapping sections, see https://midgroundwp.com/guides/text-reveal-on-scroll-wordpress/.
How do you get smooth scrolling with CSS scroll snap?
scroll-behavior: smooth on the container
Snap positions jump instantly by default, which feels abrupt when keyboard users arrow through sections. Add scroll-behavior: smooth to the snap container itself, not the body, so the easing applies only where you snap. The same rule also smooths anchor links that target sections inside the container. Test it in a forkable CodePen before pasting into a client theme, since smooth scrolling on a full-page snap layout behaves differently than on a carousel.
Per MDN's scroll-behavior documentation, the property is well supported across current browsers, but only when set on the scrolling element.
Respecting prefers-reduced-motion
Wrap smooth scrolling in a media query:
@media (prefers-reduced-motion: no-preference) {
.snap-container { scroll-behavior: smooth; }
}
Users who opt out of motion still get snap positioning, just without the animation. Note the honest limitation: scroll-snap-type itself can't be disabled this way, so mandatory snap still controls where scrolling stops for those users.
How do you build a CSS carousel with scroll snap?
A horizontal carousel is the easiest scroll snap win: one container property, one card property, and browsers handle the rest.
The base carousel CSS, annotated
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x proximity; /* mandatory fights the user's finger mid-flick */
gap: 1rem;
scroll-padding-inline: 1.5rem; /* matches your page gutter so cards don't kiss the edge */
}
.carousel > * {
flex: 0 0 80%; /* one card, 80% wide, so the next card peeks */
scroll-snap-align: start;
}
Three lines do the work. overflow-x: auto makes the strip scrollable, scroll-snap-type on the container tells the browser to settle on snap points, and scroll-snap-align: start on each card marks where it should land. The two lines people skip matter most: scroll-padding-inline shifts the snap target inward so the first card aligns with your layout's gutter instead of the viewport edge, and the 80% flex-basis leaves the next card visible, which is the visual cue that tells visitors the strip scrolls. Skip that peek and a full-width card looks like a static hero; users never discover the carousel exists. Use proximity here, not mandatory, so a fast flick can travel several cards without the browser yanking it back mid-gesture.
Adding prev/next buttons with scrollBy
nextBtn.onclick = () => carousel.scrollBy({ left: cardWidth, behavior: 'smooth' });
prevBtn.onclick = () => carousel.scrollBy({ left: -cardWidth, behavior: 'smooth' });
Buttons are optional, but they help on desktop where trackpad flicks feel imprecise. Read carousel.firstElementChild.offsetWidth for cardWidth so the step follows your responsive sizing.
Hiding the scrollbar without killing keyboard access
scrollbar-width: none and ::-webkit-scrollbar { display: none } clean up the look, but a hidden scrollbar is invisible to keyboard users too. Keep the container focusable with tabindex="0" so arrow keys still scroll it, or skip the hiding entirely. For pinned, scroll-driven scenes layered over carousels, see https://midgroundwp.com/guides/sticky-scroll-animation-wordpress/.
How do you build vertical scroll snap sections in CSS?
Full-page sections with y mandatory
Set scroll-snap-type: y mandatory on html (or a wrapper with overflow-y: scroll and a fixed height), then give each section scroll-snap-align: start. The browser snaps to the nearest section boundary, so a quick flick lands on a full viewport-height panel instead of a half-scrolled mess. This is the pattern behind Apple-style product pages, and it works inside a custom HTML block in Astra, Kadence or GeneratePress with no builder overhead.
scroll-snap-stop: always for kiosk-style pages
By default a fast flick can skip past sections. Adding scroll-snap-stop: always to each section forces the scroll to stop at every one, which suits portfolios and kiosk-style pages where each panel must be seen. Use it sparingly: on long pages it makes scrolling feel like wading.
When not to snap the whole page
Full-page snapping hurts long-form content. A blog post or documentation page with sections shorter than the viewport traps readers who cannot see the bottom of one section and the top of the next. Reserve y mandatory for pages where every section genuinely fills the screen; use y proximity everywhere else. Also respect user settings by wrapping smooth scrolling in a prefers-reduced-motion check, per MDN's scroll-behavior documentation.
If you want this kind of motion in WordPress without hand-managing snap points, Midground's scrollytelling approach handles pinned scenes and section snapping visually, though plain CSS like the above covers simpler cases fine.
Why does scroll snap break with sticky headers and how do you fix it?
Your first snapped section slides perfectly into place, then the header covers the top of it. The browser scrolls the element's edge flush with the viewport top, and your sticky header sits on top of it. The snap is working. The offset is missing.
scroll-padding-top vs scroll-margin-top
Two properties fix this, and they act on different elements. scroll-padding-top goes on the scroll container (usually html or your snap wrapper) and pads the snap port, so every target stops short of the top:
html {
scroll-padding-top: 80px; /* header height */
}
scroll-margin-top goes on individual targets instead, useful when one section needs a different offset. Set the container-level padding once and only override per element when you must. If your header height changes on mobile, use a media query or scroll-padding-top: var(--header-h).
Stopping nested snap with overscroll-behavior
A horizontal carousel inside a vertical snap page has a nasty side effect: reaching the end of the carousel chain-snaps the page too, yanking the reader down mid-scroll. Contain it:
.carousel {
overscroll-behavior-x: contain;
}
Do the same for any nested vertical snapper. One property, and the inner scroller stops at its own edge instead of passing momentum to the page.
Which browsers support CSS scroll snap today?
All current evergreen browsers, including Safari 11 and later, support scroll-snap-type, scroll-snap-align, and scroll-padding, so you can ship these patterns to clients without a fallback. scroll-behavior: smooth is also supported across Chrome, Firefox, Safari, and Edge. The only syntax you may still meet in old code is the pre-2016 IE/Edge form (scroll-snap-type: mandatory with scroll-snap-points-y), which modern browsers ignore; rewrite it against the current spec. Watch Safari's handling of overscroll-behavior, which arrived later there than elsewhere, on the MDN documentation for scroll-padding and overscroll-behavior.
How do you use these patterns inside a WordPress build?
Dropping the CSS into a block theme or HTML widget
In Astra, Kadence or GeneratePress, paste the CSS into Appearance → Customize → Additional CSS and build the sections with the block editor: a Group block per snap section, the scroll-snap-align rules targeting a class you add under Advanced. In Elementor, an HTML widget takes the same markup and a <style> block, so the carousel from earlier drops in unchanged. No slider plugin, no extra JS library, nothing to conflict with your theme's scripts.
Snap vs a scroll-motion builder: an honest comparison
Plain snap covers carousels and full-height sections. It does not do pinned scenes, scrubbed timelines or parallax tied to scroll position; for those you either hand-write a GSAP/ScrollTrigger stack or reach for a purpose-built tool. Midground (https://midgroundwp.com/) is the latter: a WordPress theme and visual builder for Apple-style scroll pages, with a Pro tier for five sites aimed at freelancers. Where it falls short against plain CSS: it is another product in your stack, another thing to learn, and overkill if a snap carousel is all the client asked for. If the brief is "make this page feel like an Apple keynote," snap alone will not get you there.
FAQ
Can I use CSS scroll snap without JavaScript at all?
Yes. The base behavior needs only scroll-snap-type on the container and scroll-snap-align on the children. JavaScript becomes optional, not required: prev/next buttons use scrollBy, but keyboard scrolling and touch flicks work with zero scripts. A carousel or full-page section layout can ship as pure CSS pasted into your theme's Additional CSS.
Does scroll snap work on mobile Safari and touch devices?
Yes. Safari has supported the modern syntax since version 11, and touch flicks are where snap feels best, since the browser settles your finger's momentum onto the nearest snap point. Two mobile caveats: use proximity on carousels so a fast flick is not yanked back mid-gesture, and test Safari's later-arriving overscroll-behavior support on nested scrollers.
How do I snap to the center of an element instead of its top edge?
Set scroll-snap-align: center on the child instead of start. The browser lines the element's middle with the container's middle. This suits cards shorter than the viewport, like a gallery strip. For full-height sections, stick with start. You can also pair it with scroll-padding on the container to offset around a sticky header.
Why does my scroll snap container jump or stutter when scrolling fast?
Usually mandatory is fighting the gesture: the browser snaps to a point before the user finishes flicking, which reads as a jump. Switch the container to proximity. A second cause is a nested snapper chain-snapping the page; contain it with overscroll-behavior: contain on the inner scroller. Missing scroll-padding-top causes apparent jumps under sticky headers.
Is scroll snap bad for SEO or accessibility?
No. Snap changes scroll behavior only, not content or DOM order, so crawlers see a normal page. For accessibility, keep the container focusable with tabindex="0" if you hide its scrollbar, wrap scroll-behavior: smooth in a prefers-reduced-motion: no-preference query, and avoid mandatory on pages with sections shorter than the viewport, which traps keyboard and touch readers.
