This note has an interactive demo. Try it in the original
CSS can now ask an <audio> or <video> element whether it's playing, paused, seeking, buffering or muted, and in November 2026 that became Baseline. That means the classic custom-player chore, listening to play, pause and volumechange just to flip .is-playing classes, can go away.
In the demo above, JavaScript does only three things: play(), pause() and toggling muted. The spinning disc, the tonearm, the equalizer bars, the play/pause icon and the glow all come from CSS reading the state of a hidden <audio> element. The panel on the right asks the browser, live, which pseudo-classes match. Drag the timeline and :seeking flashes for an instant.
Seven pseudo-classes, one per state
The HTML spec defines them in a few lines, and they map straight onto properties you already know from HTMLMediaElement:
-
:playingmatches when thepausedattribute is false.:pausedmatches when it's true. -
:seekingmatches when theseekingattribute is true. -
:bufferingmatches when the element isn't paused,networkStateisNETWORK_LOADINGand the ready state isHAVE_CURRENT_DATAor less. In other words, it wants to play but doesn't have the data yet. -
:stalledmatches elements that are:bufferingand "currently stalled". The spec says it's meant for player UI when buffering has gone on too long, unlike thestalledevent, which fires whenever the download stalls, whatever the playback state. -
:mutedmatches media elements that are muted. -
:volume-lockedmatches when the user agent has locked the volume. MDN explains that a locked element can't be muted, unmuted or have its volume changed from JavaScript, because of an operating system or browser preference.
How the demo uses them
The player is a container with custom properties, and :has() flips them depending on the state of the <audio> inside it. Defaults live on the container and the overrides live in :has(...). This is a simplified version: the demo wraps each pseudo-class in :where() for a reason I explain below.
.player {
--accent: var(--muted);
--spin: paused; /* animation-play-state of the disc */
--arm: -12deg; /* tonearm away from the record */
}
.player:has(audio:playing) {
--accent: var(--orange);
--spin: running;
--arm: 26deg; /* tonearm drops onto the record */
}
.player:has(audio:muted) .bars i { animation: none; }
/* the icons swap without a single class toggle */
.i-pause { display: none; }
.player:has(audio:playing) .i-play { display: none; }
.player:has(audio:playing) .i-pause { display: block; }
The JavaScript left over is just the media action:
playBtn.addEventListener('click', () => {
if (audio.paused) audio.play().catch(() => {});
else audio.pause();
});
muteBtn.addEventListener('click', () => { audio.muted = !audio.muted; });
The live panel uses the same selectors from JavaScript: audio.matches(':playing') returns what CSS sees. The track itself is a short loop synthesized in the browser as a WAV Blob, so the demo doesn't load any external files.
Browser support and gotchas
According to MDN's compatibility data, the seven pseudo-classes are supported in Safari 15.4, Firefox 150 and Chrome and Edge 158. With that last pair the feature became Baseline newly available on 19 November 2026. The Firefox 150 release notes list all seven at once. Here are the things I ran into while building the demo:
An unsupported pseudo-class kills the whole rule. In a browser without :playing, .player:has(audio:playing), .other { … } is thrown away entirely, .other included. I checked it in a Chromium 151 build, which doesn't have these selectors yet. The fix is to wrap the pseudo-class in :where(), whose selector list is forgiving, and add a class that JavaScript sets only where support is missing:
.player:has(audio:where(:playing, .is-playing)) { --spin: running; }
if (!CSS.supports('selector(:playing)')) {
const sync = () => {
audio.classList.toggle('is-playing', !audio.paused);
audio.classList.toggle('is-muted', audio.muted);
};
['play', 'pause', 'volumechange'].forEach((t) => audio.addEventListener(t, sync));
}
Test the bare pseudo-class in that check. In the same Chromium build, CSS.supports('selector(:has(audio:where(:playing, .is-playing)))') returned false even though the rule did apply, because CSS.supports() doesn't treat the selector forgivingly. And element.matches(':paused') throws a SyntaxError there, so only call it after detecting support.
:playing doesn't mean sound is coming out. MDN notes that an element still counts as playing while it's buffering or stalled. If :buffering matches, :playing matches too. To show "really playing" and a spinner separately, use :playing:not(:buffering) and :buffering.
:paused matches before anything happens. A freshly loaded element that hasn't started is :paused, because paused starts out true. Style the initial state with that in mind.
The fallback is small. You don't need a polyfill. A handful of event listeners covers :playing, :paused, :seeking and :muted. :volume-locked has no JavaScript equivalent, so treat it as a progressive enhancement.
What to keep an eye on
- Baseline data can change. The web-features entry for these selectors records that on 17 September 2026 their status was pulled back after an upstream data error showed them shipping too early. The date it shows now is 19 November 2026. Check the compatibility table before you drop the fallback.
-
Widely available is expected on 19 May 2029, according to the web-features explorer. Until then, keep the
:where()plus class pattern if your audience includes older Chrome or Firefox versions. -
:volume-lockeddepends on the system and the browser. It's worth designing for it (hide the volume slider, for example), but you won't see it on most desktops.
Sources
- HTML Standard: pseudo-classes
- :playing, MDN
- :buffering, MDN
- Firefox 150 release notes for developers, MDN
- Media element pseudo-classes, web-features explorer
Originally published at richi.codes.
Top comments (0)