NewNode 26.14: mockResolvedValue and Friends in node:test →
w3/w3-tutorials

Style video players with CSS :playing, :paused and :buffering

Chrome 158 ships CSS pseudo-classes for media state. Build a video player whose play button, spinner and muted badge need no JavaScript to stay in sync.

Updated Nov 26, 20268 min readIntermediatechrome 158.0.8127.18 · chromium 145.0.7632.6 · node 22.22.3 · ffmpeg 9.0.2

Custom video players usually carry a pile of JavaScript whose only job is to keep CSS in sync: listen for play, pause, waiting and volumechange, then toggle classes like .is-playing or .is-loading. Chrome 158, released on November 17, 2026, removes the need for most of that. It ships seven CSS pseudo-classes that match a <video> or <audio> element based on its playback state.

This tutorial builds a small player whose play button, loading spinner, "slow connection" message and muted badge are all driven by CSS. The only JavaScript left starts and pauses the video. A tiny Node server with a deliberately slow route lets you watch the buffering states happen.

The seven pseudo-classes

The HTML spec defines each one in terms of properties you may already know from HTMLMediaElement:

Pseudo-classMatches when
:playingpaused is false
:pausedpaused is true
:seekingseeking is true
:bufferingnot paused, networkState is NETWORK_LOADING, and readyState is HAVE_CURRENT_DATA or lower
:stalled:buffering matches, and the download has made no progress for a while
:mutedthe element is muted
:volume-lockedthe browser or OS has locked the volume, so scripts can't change it

One detail matters for your styles. :playing means "not paused", not "frames are moving". A video that is waiting for data still matches :playing, because it has not been paused. That is why :buffering exists, and why a spinner should key off :buffering rather than off "not :playing".

Support, from MDN's compatibility data: Safari 15.4, Firefox 150 and now Chrome 158. The feature is part of Interop 2026.

1. Make a test video

Any MP4 works. To follow along exactly, generate a 12-second test pattern with a tone using FFmpeg:

terminal
mkdir media-states && cd media-states
ffmpeg -loglevel error -y -f lavfi -i testsrc2=size=640x360:rate=30:duration=12 \
  -f lavfi -i sine=frequency=440:duration=12 \
  -c:v libx264 -pix_fmt yuv420p -b:v 1500k -c:a aac -shortest \
  -movflags +faststart clip.mp4

+faststart puts the file's index at the start, so playback can begin before the whole file has downloaded. The slow server below depends on that.

2. The markup

The overlays sit next to the <video> inside a wrapper. They are hidden by default, and CSS reveals each one in its state.

index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Media pseudo-classes</title>
  <link rel="stylesheet" href="player.css">
</head>
<body>
  <div class="player">
    <video src="clip.mp4" playsinline muted></video>
    <button class="play" type="button" aria-label="Play">▶</button>
    <span class="spinner" aria-hidden="true"></span>
    <p class="stall-msg">Slow connection. Still trying…</p>
    <span class="muted-badge">Muted</span>
  </div>
  <script src="player.js"></script>
</body>
</html>

3. Style each state

The pseudo-classes match the <video> itself, but the elements you want to show are its siblings. :has() lets the wrapper react to the video's state, so the order of the overlays in the markup doesn't matter.

player.css
.player {
  position: relative;
  width: 640px;
  aspect-ratio: 16 / 9;
  background: #000;
}

.player video {
  width: 100%;
  height: 100%;
}

/* Hidden by default: each overlay appears only in its state */
.play,
.spinner,
.stall-msg,
.muted-badge {
  display: none;
}

/* Big play button while paused */
.player:has(video:paused) .play {
  display: grid;
  place-items: center;
  position: absolute;
  inset: 0;
  margin: auto;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  font-size: 32px;
}

/* Spinner while playback is waiting for data */
.player:has(video:buffering) .spinner {
  display: block;
  position: absolute;
  inset: 0;
  margin: auto;
  width: 48px;
  height: 48px;
  border: 4px solid #fff4;
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* A message once buffering has gone on for a while */
.player:has(video:stalled) .stall-msg {
  display: block;
  position: absolute;
  bottom: 12px;
  width: 100%;
  margin: 0;
  color: #fff;
  text-align: center;
}

/* Dim the frame while the user scrubs */
.player video:seeking {
  filter: brightness(0.6);
}

.player:has(video:muted) .muted-badge {
  display: block;
  position: absolute;
  top: 12px;
  right: 12px;
  padding: 2px 8px;
  background: #000a;
  color: #fff;
}

@keyframes spin {
  to { rotate: 1turn; }
}

There is no .is-playing class anywhere. The browser updates the match every time the element's state changes, including changes you didn't cause, such as the user pressing a media key or the network dropping.

4. The JavaScript that's left

Something still has to call play() and pause(). The script also handles browsers that don't know the new selectors yet.

player.js
const video = document.querySelector('.player video');
const playButton = document.querySelector('.player .play');

// Older browsers don't know these selectors: give them native controls
if (!CSS.supports('selector(:paused)')) {
  video.controls = true;
  playButton.remove();
}

playButton?.addEventListener('click', () => video.play());
video.addEventListener('click', () => {
  if (video.paused) video.play();
  else video.pause();
});

The fallback matters. A browser that doesn't recognize :paused drops the whole rule that contains it, so without this check, Chrome 157 and older would never show the play button. CSS.supports('selector(…)') asks the browser whether it can parse a selector. In Chromium 145 it returns false, the video gets native controls and the custom button goes away. In Chrome 158 it returns true.

5. A server with a slow mode

Buffering is hard to see on localhost because the file arrives instantly. This server has a ?slow mode that sends the first quarter of the video and then stops sending, while it keeps the connection open. That is what a dying mobile connection looks like to the browser.

server.mjs
import { createServer } from 'node:http';
import { readFileSync, statSync } from 'node:fs';

const types = { html: 'text/html', css: 'text/css', js: 'text/javascript', mp4: 'video/mp4' };

createServer((req, res) => {
  const url = new URL(req.url, 'http://localhost');
  const slow = url.searchParams.has('slow');
  const file = url.pathname === '/' ? 'index.html' : url.pathname.slice(1);
  const type = types[file.split('.').pop()];
  if (!type) return res.writeHead(404).end();

  if (file !== 'clip.mp4') {
    let body = readFileSync(file, 'utf8');
    // ?slow on the page makes the video request slow too
    if (slow && file === 'index.html') body = body.replace('clip.mp4', 'clip.mp4?slow');
    return res.writeHead(200, { 'Content-Type': type }).end(body);
  }

  const size = statSync(file).size;
  const [start] = (req.headers.range ?? 'bytes=0-').replace('bytes=', '').split('-').map(Number);
  res.writeHead(206, {
    'Content-Type': type,
    'Accept-Ranges': 'bytes',
    'Content-Range': `bytes ${start}-${size - 1}/${size}`,
    'Content-Length': size - start,
  });
  const data = readFileSync(file).subarray(start);
  // ?slow: send the first 25% of the file, then stop sending and leave the connection open
  res.write(slow ? data.subarray(0, Math.floor(size / 4)) : data);
  if (!slow) res.end();
}).listen(8080, () => console.log('http://localhost:8080'));
terminal
node server.mjs
text
http://localhost:8080

6. Watch the states change

Open http://localhost:8080 in Chrome 158 or later and paste this into the DevTools console. Once a second, it logs the playback position and every pseudo-class the video matches.

log-states.js
const video = document.querySelector('video');
const states = [':playing', ':paused', ':seeking', ':buffering', ':stalled', ':muted'];

setInterval(() => {
  const now = video.currentTime.toFixed(1);
  console.log(`${now}s`, states.filter((s) => video.matches(s)).join(' '));
}, 1000);

Click the play button. Then run video.currentTime = 9 in the console to jump ahead, run video.muted = false to unmute, and click the video to pause it. This run used Chrome 158.0.8127.18:

text
0.0s :paused :muted
0.5s :playing :muted
1.1s :playing :muted
9.4s :playing :muted
10.0s :playing
10.7s :paused

The muted badge disappears as soon as the video is unmuted, and the play button returns on pause. :seeking only lasts while the browser fetches and decodes the new position. On localhost that takes less than the one-second polling interval, so check it right after the jump:

js
video.currentTime = 9;
video.matches(':seeking'); // true

In that moment, getComputedStyle(video).filter returned brightness(0.6).

Now open http://localhost:8080/?slow, paste the logger again and press play:

text
0.3s :playing :muted
0.9s :playing :muted
1.4s :playing :muted
1.9s :playing :muted
2.4s :playing :muted
3.0s :playing :muted
3.0s :playing :buffering :stalled :muted
3.0s :playing :buffering :stalled :muted

A quarter of a 12-second file is about 3 seconds of video. At 3.0 seconds playback runs out of data, the spinner appears and currentTime stops moving. Notice that :playing still matches the whole time.

:stalled appeared at the same moment as :buffering here, because the download had stopped long before playback caught up. On a connection that is merely slow, :buffering comes first. Under the spec, :stalled follows only when no data arrives for a while, which makes it a good trigger for a "still trying" message that shouldn't flash on every short wait. Note that it isn't the same as the stalled event, which fires when the download stalls even if playback is fine.

When to keep the JavaScript

  • Screen reader announcements. CSS can show a spinner, but it can't tell assistive technology that the video is loading. If you announce state changes through an aria-live region, you still need the waiting and playing events.
  • Analytics and logic. Counting rebuffer events, or switching to a lower-quality stream after a stall, is application logic. Listen for the events.
  • Older browsers. Chrome 157 and older ignore these selectors entirely. Keep a fallback like the native-controls switch above until your analytics say it's safe to drop it.
  • Native controls. If you use the controls attribute, the browser already draws all of this. The pseudo-classes are for custom players.

:volume-locked is the one you'll use least. It matches when the volume is locked by a user or OS preference and scripts can't change it. Use it to hide a volume slider that would do nothing.

Next steps

  • Read the :buffering and :stalled reference pages on MDN.
  • See what else shipped in the Chrome 158 release notes.
  • Replace one .is-* class in your own player with a pseudo-class, and delete the event listener that set it.

Related tutorials