research/serving-a-smaller-video-to-phones

Serving a Smaller Video to Phones with <source media>: What Chromium Actually Loads

Engineering Note · November 11, 2026 · 6 minute read · Video · HTML · Performance

Yes. A media attribute on a <source> inside <video> lets the browser pick a smaller file for small screens, and in Chromium 153 only the matching file was downloaded. The choice is made once, when the video loads: resizing the window afterwards does not switch files unless you call load() again. Source order decides ties, so the narrow query goes first and a plain fallback goes last.

Everything below was run with Node.js 22.23.4, Playwright 1.63.0 driving Chromium 153.0.8010.12 (headless, on Linux), and ffmpeg 6.1.1 to make the test videos. We did not test Firefox or Safari. For those we quote MDN’s browser compatibility data, version 8.1.8, which we downloaded from npm and read directly.

Why this came up

The reel at the top of some of our Workbench pages uses exactly this pattern. The markup in src/pages/workbench/[slug].astro is:

<video class="reel-video" id="projectReel" muted loop playsinline preload="metadata" poster={project.data.reel.poster} aria-label={project.data.reel.label}>
  {project.data.reel.srcMobile && <source src={project.data.reel.srcMobile} type="video/mp4" media="(max-width: 680px)" />}
  <source src={project.data.reel.src} type="video/mp4" />
</video>

On the one page that sets srcMobile, the two files are a 1920×1080 MP4 of 26,122,221 bytes and a 1280×720 MP4 of 12,645,600 bytes (sizes from ls -l, dimensions from ffprobe). If phones get the second file, they skip about 13.5 MB. We wanted to confirm that they do, and that they do not download both.

What does the HTML standard say?

We read the source of the WHATWG HTML standard from its GitHub repository. On the source element:

The user agent will skip to the next source element if the value does not match the environment.

And, in a note right below:

The media attribute is only evaluated once during the resource selection algorithm for media elements. In contrast, when using the picture element, the user agent will react to changes in the environment.

So <picture> and <video> behave differently. An image swaps when the viewport changes; a video does not.

How we tested it

We made two pairs of three-second test videos with ffmpeg: 1280×720 and 640×360, once as H.264 MP4 and once as VP9 WebM.

ffmpeg -loglevel error -y -f lavfi -i testsrc2=size=1280x720:rate=30 -t 3 -c:v libx264 -pix_fmt yuv420p -an site/desk.mp4
ffmpeg -loglevel error -y -f lavfi -i testsrc2=size=640x360:rate=30 -t 3 -c:v libx264 -pix_fmt yuv420p -an site/phone.mp4
ffmpeg -loglevel error -y -f lavfi -i testsrc2=size=1280x720:rate=30 -t 3 -c:v libvpx-vp9 -b:v 300k -an site/desk.webm
ffmpeg -loglevel error -y -f lavfi -i testsrc2=size=640x360:rate=30 -t 3 -c:v libvpx-vp9 -b:v 150k -an site/phone.webm

A small Node.js HTTP server served a page with this markup and logged every video request:

<video id="v" muted playsinline preload="metadata">
  <source src="/phone.webm" type="video/webm" media="(max-width: 680px)">
  <source src="/desk.webm" type="video/webm">
</video>

For each case, Playwright opened the page at a given viewport width, waited for the video’s metadata, then read currentSrc, videoWidth and networkState from the element. Some cases then resized the viewport and read them again. The core of the check:

const state = () => p.evaluate(() => {
  const v = document.getElementById('v');
  return { currentSrc: v.currentSrc.replace(location.origin, ''), videoWidth: v.videoWidth,
           networkState: v.networkState, error: v.error && v.error.code };
});

What did Chromium load?

The script’s real output, one JSON line per case:

Chromium 153.0.8010.12
{"file":"mp4.html","width":375,"before":{"currentSrc":"/phone.mp4","videoWidth":640,"networkState":1,"error":null},"requested":["/phone.mp4"]}
{"file":"mp4.html","width":1280,"before":{"currentSrc":"/desk.mp4","videoWidth":1280,"networkState":1,"error":null},"requested":["/desk.mp4"]}
{"file":"webm.html","width":375,"before":{"currentSrc":"/phone.webm","videoWidth":640,"networkState":1,"error":null},"requested":["/phone.webm"]}
{"file":"webm.html","width":1280,"before":{"currentSrc":"/desk.webm","videoWidth":1280,"networkState":1,"error":null},"requested":["/desk.webm"]}
{"file":"webm.html","width":1280,"before":{"currentSrc":"/desk.webm","videoWidth":1280,"networkState":1,"error":null},"resizedTo":375,"after":{"currentSrc":"/desk.webm","videoWidth":1280,"networkState":1,"error":null},"requested":["/desk.webm"]}
{"file":"webm.html","width":375,"before":{"currentSrc":"/phone.webm","videoWidth":640,"networkState":1,"error":null},"resizedTo":1280,"after":{"currentSrc":"/phone.webm","videoWidth":640,"networkState":1,"error":null},"requested":["/phone.webm"]}
{"file":"nomatch.html","width":1280,"before":{"currentSrc":"","videoWidth":0,"networkState":3,"error":null},"requested":[]}

In summary:

CaseViewportFile chosenFiles requested
MP4 pair375 pxphone.mp4 (640 wide)phone.mp4 only
MP4 pair1280 pxdesk.mp4 (1280 wide)desk.mp4 only
WebM pair375 pxphone.webmphone.webm only
WebM pair1280 pxdesk.webmdesk.webm only
Load at 1280, resize to 375bothdesk.webm, unchangeddesk.webm only
Load at 375, resize to 1280bothphone.webm, unchangedphone.webm only
Only a phone source1280 pxnonenone

Three points stand out. First, the browser never fetched the file it skipped, so phones really do save the bandwidth. Second, the resize cases match the standard: the first choice sticks. Third, with no matching source, Chromium requested nothing and set networkState to 3 (NETWORK_NO_SOURCE) without setting error, so an error listener on the element would not tell you.

Does source order matter?

Yes. We swapped the order so the desktop source, with no media, came first:

<source src="/desk.webm" type="video/webm">
<source src="/phone.webm" type="video/webm" media="(max-width: 680px)">

At a 375-pixel viewport the output was:

wrong order, 375px: {"currentSrc":"/desk.webm","videoWidth":1280} requested ["/desk.webm"]

The browser takes the first source that matches, and a source without media always matches. Narrowest query first, fallback last.

Can you make it re-choose after a resize?

Calling load() on the video element restarts the selection, and the media query is evaluated again. We loaded at 1280 pixels, resized to 375 and called load():

1280px: {"currentSrc":"/desk.webm","videoWidth":1280}
resized to 375px, then load(): {"currentSrc":"/phone.webm","videoWidth":640} requested ["/desk.webm","/phone.webm"]

It worked, at the cost of a second download and a restart of playback. For a muted background reel, we would not bother. People rarely resize a phone from portrait to desktop width, and rotating a phone does not usually cross a 680-pixel breakpoint. If you do want it, listen to a matchMedia change event for the same query and call load() only when the answer flips.

Which browsers support it?

From MDN’s browser compatibility data, html.elements.source.media, version 8.1.8 as published on npm:

BrowserFirst version listedNote in the data
Chrome3none
Chrome for Android18none
Edge12none
Firefox15“In Firefox 53-119, media is supported only in <source> within <picture>.”
Firefox for Android15Same note, for 53-119
Safari3.1none
Safari on iOS2none

The practical reading: Firefox 120 and later honors media for video. An older Firefox ignores it, so by the standard’s selection rules it should take the first source whose type it can play. With our order, that would be the phone file, even on a desktop. We did not run an older Firefox to confirm this. If older Firefox matters to you, that is an argument for keeping the phone encode good enough to look acceptable on a large screen.

Limitations

  • We tested one browser engine, Chromium 153, headless on Linux. Firefox and Safari behavior here comes from MDN’s data and the standard, not from our own runs.
  • Viewport width in Playwright stands in for a real phone. We did not test on devices, with real network conditions, or with preload="auto" or autoplay.
  • Our test files are three seconds long and small. We measured which files were requested, not how long they took.
  • Our own phone reel is still 12.6 MB at 1280×720. This test shows the right file is sent; it does not show that file is small enough. A lighter phone encode is a separate job.

Frequently asked questions

Can I use the media attribute on a source inside a video element?

Yes. The HTML standard allows a media query on each source element inside video and audio, and the browser skips any source whose query does not match. In our test, Chromium 153 at a 375-pixel-wide viewport loaded only the phone file, and at 1280 pixels loaded only the desktop file.

Does the video switch files when the window is resized?

No. The media query is evaluated once, when the browser chooses a source. The HTML standard says so, and our test agreed: after loading at one width and resizing to the other, the video kept its first file. Calling the video element's load() method made it choose again.

Which browsers support source media for video?

MDN's browser compatibility data (version 8.1.8) lists support in Chrome, Edge and Safari, and in Firefox from version 120. Firefox 53 to 119 only honored media on source elements inside picture. We tested Chromium only, so treat the other browsers as documented rather than verified by us.

What order should the source elements go in?

Put the source with the narrowest media query first and the general fallback, with no media attribute, last. The browser takes the first source that matches. When we put the desktop source first, a 375-pixel viewport still loaded the desktop file.

What happens if no source matches?

Nothing loads. With a single source restricted to small screens and a 1280-pixel viewport, Chromium requested no file, left currentSrc empty and set networkState to 3, the no-source state. Always end with a source that has no media attribute.

More from Research

← All research