Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

Motion and scrolling in CSS

CSS motion includes transitions between states, keyframe animations, and effects linked to scrolling. Distinguish changes to an element’s appearance from changes to scroll position, and respect the user’s preference for reduced motion.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

.motion {
  transition: transform 200ms ease;
  animation: move 1s linear;
}

@keyframes move {
  from { transform: translateX(0); }
  to { transform: translateX(24px); }
}

.scroll-container {
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) { /* reduce non-essential motion */ }

“motion and scrolling” names a group of concepts, not a CSS property. transition describes a transition after a property value changes, for example when an element receives focus. animation together with @keyframes defines a sequence of values and controls aspects such as duration and repetition. Whether smooth interpolation is possible depends on the animated property and its values.

Scrolling changes the visible portion of a scrollable area. scroll-behavior: smooth specifies scrolling behavior for navigation or CSSOM interfaces; it does not directly control user scrolling. The browser chooses the duration and progression of smooth scrolling and may ignore this property. This is not a transform animation.

In scroll-driven animations, animation-timeline ties animation progress to a scroll progress or view progress timeline rather than to elapsed time. The scroll() and view() functions create anonymous timelines; animation-timeline can also reference a named timeline of either type. scroll() supplies a scroll progress timeline that tracks a container’s scrolling progress, while view() supplies a view progress timeline that tracks an element’s passage through a container’s scrollport. Each function takes a separate, optional axis parameter. This differs from a time-based animation merely triggered by scrolling. Draft specifications do not guarantee support in a particular browser.

prefers-reduced-motion: reduce signals a preference to reduce non-essential motion; the query itself does not disable author effects. The example adds motion only for no-preference, and the button remains keyboard-accessible. Verify both settings before deployment.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>CSS motion with a stable button target</title>
  <style>
    #motion {
      margin: 1rem;
      padding: 0.5rem 2.25rem 0.5rem 0.75rem;
    }
    #motion > span {
      display: inline-block;
      pointer-events: none;
    }
    @media (prefers-reduced-motion: no-preference) {
      #motion > span { transition: transform 200ms linear; }
      #motion[aria-pressed="true"] > span {
        transform: translateX(24px);
      }
    }
  </style>
</head>
<body>
  <h1>CSS motion with a stable button target</h1>
  <p>Activate the button to shift the label, then activate it again to move the label back. The label stays still when reduced motion is requested.</p>
  <button id="motion" type="button" aria-pressed="false"><span>Shift label</span></button>
  <script>
    const button = document.getElementById('motion');
    const label = button.firstElementChild;
    button.addEventListener('click', () => {
      button.setAttribute('aria-pressed', String(button.getAttribute('aria-pressed') !== 'true'));
    });

    function readShift() {
      // Flush styles and finish any transition before reading its endpoint.
      const animations = label.getAnimations();
      animations.forEach(animation => animation.finish());
      const transform = getComputedStyle(label).transform;
      return transform === 'none' ? 0 : new DOMMatrixReadOnly(transform).m41;
    }

    const motionAllowed = matchMedia('(prefers-reduced-motion: no-preference)').matches;
    const duration = getComputedStyle(label).transitionDuration;
    button.click();
    const shiftOn = readShift();
    const onPassed = button.getAttribute('aria-pressed') === 'true'
      && shiftOn === (motionAllowed ? 24 : 0);
    button.click();
    const shiftOff = readShift();
    const offPassed = button.getAttribute('aria-pressed') === 'false'
      && shiftOff === 0;
    const stableTarget = getComputedStyle(button).transform === 'none'
      && getComputedStyle(label).pointerEvents === 'none';
    const passed = stableTarget && onPassed && offPassed
      && duration === (motionAllowed ? '0.2s' : '0s');
    console.log(passed ? 'Motion check passed: the button toggles and respects reduced motion'
      : 'Motion check failed');
  </script>
</body>
</html>

Result

Motion check passed: the button toggles and respects reduced motion

The listed console message is the expected output when all of the script’s checks pass.

Sources