Color scheme

Scroll Galleries and SVG Controls

A horizontal card gallery with snap behavior, prev/next controls, smooth in-page scrolling, and a fix for the modal scroll chaining bug: four CSS and JavaScript techniques that work together naturally on most portfolio and project pages. This page focuses on the parts that go beyond basic scroll snap: building accessible SVG arrow controls, wiring them to the scroll container with JavaScript, and applying scroll-behavior and overscroll-behavior for a polished result.

Scroll Snap Basics

If you studied the Scroll Snap Carousel choose topic, the scroll-snap-type and scroll-snap-align syntax will be familiar. If not, the Quick Reference below covers what you need.

Scroll Snap Quick Reference

Two properties do the work. Set scroll-snap-type on the scroll container and scroll-snap-align on the children:


    .gallery {
        display: flex;
        overflow-x: auto;
        scroll-snap-type: x mandatory;
        gap: var(--spacing-lg);
        padding-inline: var(--spacing-xl);
    }

    .gallery-card {
        flex-shrink: 0;
        width: 280px;
        scroll-snap-align: start;
    }

mandatory means the container always rests on a snap point after scrolling. proximity only snaps when the scroll ends near a snap point. For a gallery with full-card snapping, mandatory is almost always the right choice.

SVG Arrow Controls

Prev/next buttons let keyboard users and mouse users navigate the gallery without swiping. The controls are <button> elements, not links, because they trigger an action (scrolling) rather than navigating to a URL. Each button contains an inline SVG chevron icon.

Button vs. Link Semantics

The rule is straightforward: if clicking takes you somewhere, use <a href>. If clicking does something, use <button>. Scroll controls do something, so they are buttons. Screen readers announce buttons as "button" and links as "link". The wrong choice confuses users who navigate by element type.

Accessible SVG in Buttons

The SVG icon is decorative because the button's aria-label provides the accessible name. Hide the SVG from assistive technology and label the button directly. See SVG Accessibility for the full aria-label and aria-hidden patterns.


    <div class="gallery-controls">
        <button type="button" class="gallery-btn" id="prev-btn" aria-label="Previous">
            <svg aria-hidden="true" focusable="false" viewBox="0 0 24 24"
                 fill="none" stroke="currentColor" stroke-width="2"
                 stroke-linecap="round" stroke-linejoin="round">
                <path d="M15 18l-6-6 6-6"></path>
            </svg>
        </button>
        <button type="button" class="gallery-btn" id="next-btn" aria-label="Next">
            <svg aria-hidden="true" focusable="false" viewBox="0 0 24 24"
                 fill="none" stroke="currentColor" stroke-width="2"
                 stroke-linecap="round" stroke-linejoin="round">
                <path d="M9 18l6-6-6-6"></path>
            </svg>
        </button>
    </div>

Wiring the Controls with JavaScript

Each button scrolls the container by one card width using scrollBy(). Reading the first child's offsetWidth gives the card width including gap:


    const gallery  = document.querySelector('.gallery');
    const prevBtn  = document.getElementById('prev-btn');
    const nextBtn  = document.getElementById('next-btn');
    const cardWidth = gallery.querySelector('.gallery-card').offsetWidth
                    + parseInt(getComputedStyle(gallery).gap);

    prevBtn.addEventListener('click', () => {
        gallery.scrollBy({ left: -cardWidth, behavior: 'smooth' });
    });

    nextBtn.addEventListener('click', () => {
        gallery.scrollBy({ left: cardWidth, behavior: 'smooth' });
    });
Keyboard Testing

Tab to each button and press Enter or Space. The gallery should scroll one card in the correct direction. If it does not respond, check that the buttons are not inside an element with tabindex="-1" and that no JavaScript error is preventing the event listener from attaching. The browser's Accessibility panel in DevTools should show each button with a computed accessible name of "Previous" or "Next".

scroll-behavior: smooth

scroll-behavior: smooth on the <html> element makes all in-page anchor link jumps animate smoothly instead of snapping. It applies whenever the browser scrolls to a fragment URL like #section-id:


    html {
        scroll-behavior: smooth;
    }

This is a page-level setting. It does not affect programmatic scrolls from JavaScript unless you also pass behavior: 'smooth' to scrollTo() or scrollBy() explicitly, as shown in the gallery button code above.

overscroll-behavior and Scroll Chaining

Scroll chaining is what happens when a user scrolls to the end of a scrollable container and the scroll propagates to the parent. In a modal or drawer with its own scrollable content, reaching the bottom of the modal content starts scrolling the page behind it. This is almost never what you want.

overscroll-behavior: contain on the inner container stops the chain. The scroll stays inside the element and does not propagate when it hits the boundary:


    .modal-body {
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    .gallery {
        overflow-x: auto;
        overscroll-behavior-x: contain;
    }

The longhand overscroll-behavior-x and overscroll-behavior-y let you target one axis independently. For a horizontal gallery you only need to contain the x axis, leaving vertical overscroll (pull-to-refresh on mobile) unaffected.

prefers-reduced-motion Guard

Both scroll-behavior: smooth and the behavior: 'smooth' passed to scrollBy() produce motion. For users who prefer reduced motion, scrolling should jump instantly. Handle both in CSS and JavaScript:


    @media (prefers-reduced-motion: no-preference) {
        html {
            scroll-behavior: smooth;
        }
    }

    const prefersReduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

    prevBtn.addEventListener('click', () => {
        gallery.scrollBy({
            left: -cardWidth,
            behavior: prefersReduced ? 'instant' : 'smooth'
        });
    });

    nextBtn.addEventListener('click', () => {
        gallery.scrollBy({
            left: cardWidth,
            behavior: prefersReduced ? 'instant' : 'smooth'
        });
    });

Scroll Gallery Demo

Swipe or drag the card strip, use the arrow buttons on either side, or Tab to a button and press Enter. Use Prefers Reduced Motion to emulate the guard students add in their portfolio demo: scrolling jumps instantly instead of animating.

Build Your Demo

Create a new directory at unit-6/scroll-gallery/ in your Practice Site with index.html, gallery.css, and gallery.js. Your demo must include:

  • A horizontal scroll gallery using scroll-snap-type: x mandatory on the container and scroll-snap-align: start on each card. At least five cards so the gallery actually needs scrolling.
  • Prev and next <button> elements with inline SVG chevron icons, each with an aria-label. The SVG must have aria-hidden="true" and focusable="false".
  • overscroll-behavior-x: contain on the gallery container.
  • scroll-behavior: smooth for anchor links guarded inside @media (prefers-reduced-motion: no-preference), and behavior: 'instant' passed to scrollBy() when the reduced-motion media query matches.
  • At least one in-page anchor link somewhere on the page that demonstrates scroll-behavior: smooth.

Add a link to your demo on your Practice Site homepage and in your README.md.

Teaching Video Checklist

When you record your teaching video, make sure your teammates come away understanding these concepts:

  • Why prev/next controls use <button> and not <a>, and what screen readers announce differently for each. Show the Accessibility panel in DevTools with the computed accessible name visible.
  • How scrollBy() with behavior: 'smooth' works and why reading offsetWidth plus gap gives the correct scroll distance per card.
  • The scroll chaining bug: scroll a modal or nested container to the bottom and show the page behind it starting to scroll. Then add overscroll-behavior: contain and show the bug is gone.
  • The prefers-reduced-motion guard for both the CSS anchor smooth scroll and the JavaScript scrollBy() call. Toggle the reduced-motion emulation in DevTools and show the difference.

Record the video with the gallery on screen throughout. Start with no controls and no snap, add snap, then add the controls, then add smooth scroll and the reduced-motion guard. Building up in layers is the clearest way to show what each feature contributes.

Resources