CSS @keyframes animations run continuously by default, even when they scroll out of the viewport. Pausing them matters for three reasons: freeing up processing power, giving users control, and — most importantly — accessibility, since heavy motion can cause dizziness in people with conditions such as concussion.

The One True Pause: animation-play-state

The only way to genuinely pause a CSS animation is the animation-play-state property set to paused.

.paused {
  animation-play-state: paused;
}

In JavaScript the same property is camelCased as animationPlayState and can be toggled by reading its current value and assigning the opposite:

const running = element.style.animationPlayState === 'running';
element.style.animationPlayState = running ? 'paused' : 'running';

Two alternatives look like pauses but aren't. Setting animation-duration to 0s leaves the animation technically running — it simply shows no motion, and restoring a value like 3s makes it jump between its start position and its next step in the sequence. Removing the animation via a class works the same way: nothing is actually paused.

.remove-animation {
  animation: none !important;
}

Driving Animations Through Custom Properties

A convenient pattern is to attach a [data-animation] attribute to every element meant to play and pause, keeping it distinguishable from other animations. That selector becomes the hook for the animation shorthand, with custom properties as values and fallbacks for shared settings such as duration and easing:

<div data-animation></div>
[data-animation] {
  animation:
    var(--animn, none)
    var(--animdur, 1s)
    var(--animtf, linear)
    var(--animdel, 0s)
    var(--animic, infinite)
    var(--animdir, alternate)
    var(--animfm, none)
    var(--animps, running);
}

Custom properties earn their place here because both CSS and JavaScript can read and write them, they cut down the amount of CSS required, and — in Chrome at the time of writing — they can even be set from inside @keyframes.

Individual animations then use class selectors, updating only the variables that change. Because the fallbacks already cover common values, often only the animation's name property, --animn, needs overriding:

<div class="circle a-slide" data-animation></div>
/* Animation classes */
.a-pulse {
  --animn: pulse;
}
.a-slide {
  --animdur: 3s;
  --animn: slide;
}

/* Keyframes */
@keyframes pulse {
  0% { transform: scale(1); }
  25% { transform: scale(.9); }
  50% { transform: scale(1); }
  75% { transform: scale(1.1); }
  100% { transform: scale(1); }
}
@keyframes slide {
  from { margin-left: 0%; }
  to { margin-left: 150px; }
}

The .circle class in the example is purely for styling the element and has nothing to do with the animation. The data attribute could be an ordinary class today, but it earns its keep in the more advanced patterns below.

Pausing Everything With the Checkbox Hack

Place a checkbox before the animated elements and update the --animps property when it is checked:

<input type="checkbox" data-animation-pause />
[data-animation-pause]:checked ~ [data-animation] {
  --animps: paused;
}

Clicking the checkbox toggles play and pause for all animations — no JavaScript involved.

Building a CSS-Only Slideshow

The <details> element suits this well. Beyond global attributes and events it exposes just an open attribute and a toggle event, making it as handy as the checkbox hack for state toggling — but simpler:

details[open] {
  --state: 1;
}
details:not([open]) {
  --state: 0;
}

In the slideshow, slides advance automatically through a primary animation called autoplay, while each slide also carries its own secondary animation. The primary animation-play-state is driven by --animps; each slide's unique animation is named in its own --animn, and the secondary animations' play state is controlled by --img-animps. The Ken Burns-style effects come from Animista.

<figure style="--animn:kenburns-top;--index:0;">
  <img src="some-slide-image.jpg" />
  <figcaption>Caption</figcaption>
</figure>

Pausing One Animation From Another

Ideally the primary animation would also pause the secondary ones to avoid overloading the GPU. Only Chrome (at the time of writing, and somewhat shakily) can update a custom property from inside a @keyframes animation. The initial value of --img-animps must therefore be paused even while the primary animation runs:

details[open] ~ .c-mm__inner .c-mm__frame {
  --animps: running;
  --img-animps: paused;
}

The main @keyframes then flips it to running:

@keyframes autoplay {
  0.1% {
    --img-animps: running; /* START */
    opacity: 0;
    z-index: calc(var(--z) + var(--slides))
  }
  5% { opacity: 1 }
  50% { opacity: 1 }
  51% { --img-animps: paused } /* STOP! */
  100% {
    opacity: 0;
    z-index: var(--z)
  }
}

Browsers other than Chrome cannot update a custom property from a @keyframe, so there the initial value has to be running instead.

Respecting prefers-reduced-motion

macOS and Windows both let users signal a preference for reduced motion, which the prefers-reduced-motion feature query exposes:

@media (prefers-reduced-motion) { ... }

The [data-animation] selector can accept values applied when that query matches:

  • alternate — run a different animation
  • once — set animation-iteration-count to 1
  • slow — change animation-duration
  • stop — set animation-play-state to paused

These are only suggestions; the values can be anything.

<div class="circle a-slide"></div>
<div class="circle a-slide"></div>
<div class="circle a-slide"></div>
<div class="circle a-slide"></div>
@media (prefers-reduced-motion) {
  [data-animation="alternate"] {
   /* Change animation duration AND name */
    --animdur: 4s;
    --animn: opacity;
  }
  [data-animation="slow"] {
    /* Change animation duration */
    --animdur: 10s;
  }
  [data-animation="stop"] {
    /* Stop the animation */
    --animps: paused;
  }
}

To give each animation class its own alternate treatment, group the selectors instead:

.a-slide[data-animation="alternate"] { /* etc. */ }

Scripted Play, Pause and Viewport Awareness

The checkbox behaviour can be reproduced in JavaScript by iterating every [data-animation] element and toggling the same --animps custom property — identical concept, different driver:

<button id="js-toggle" type="button">Toggle Animations</button>
const animations = document.querySelectorAll('[data-animation');
const jstoggle = document.getElementById('js-toggle');

jstoggle.addEventListener('click', () => {
  animations.forEach(animation => {
    const running = getComputedStyle(animation).getPropertyValue("--animps") || 'running';
    animation.style.setProperty('--animps', running === 'running' ? 'paused' : 'running');
  })
});

For older browsers, toggle a class that sets animation-play-state instead.

Automatic Pausing With IntersectionObserver

An IntersectionObserver lets animations start only when they enter the viewport, so offscreen animations never waste GPU time. Begin by ensuring nothing runs at all:

[data-animation] {
  /* Change 'running' to 'paused' */
  animation: var(--animps, paused); 
}

The observer triggers at 25% and 75% visibility: the latter starts playback, anything else pauses it. All [data-animation] elements are observed by default, but when reduced motion is requested, elements marked [data-animation="stop"] are skipped.

const IO = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    if (entry.isIntersecting) {
      const state = (entry.intersectionRatio >= 0.75) ? 'running' : 'paused';
      entry.target.style.setProperty('--animps', state);
    }
  });
}, {
  threshold: [0.25, 0.75]
});

const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
const elements = mediaQuery?.matches ? document.querySelectorAll(`[data-animation]:not([data-animation="stop"]`) : document.querySelectorAll('[data-animation]');

elements.forEach(animation => {
  IO.observe(animation);
});

The threshold values, whether to unobserve elements after firing, and handling of dynamically loaded content all need tuning per project. Treat the observer as a foundation rather than a finished solution.

Extra: Audio With Minimal JavaScript

Adding music to the slideshow takes an audio tag:

<audio src="https://css-tricks.com/asset/audio/slideshow.mp3" hidden loop></audio>

and a short script:

const audio = document.querySelector('your-audio-selector');
const details = document.querySelector('your-details-selector');
details.addEventListener('toggle', () => {
  details.open ? audio.play() : audio.pause();
})