Sub topic 5
Fallback content for unsupported browsers
This chapter assumes you know basic HTML attributes, how source elements list alternative formats, and that a video or audio element can contain child elements. Here you will learn what fallback content is, when a browser shows it, and how to write fallback that helps people.
What fallback content is
Anything inside audio or video that is not a source or track element is fallback content. A browser that supports the media element ignores it. A browser that does not support the element renders it as ordinary HTML.
<video controls width="640" poster="poster.jpg" preload="metadata">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
<track kind="captions" src="clip.en.vtt" srclang="en" label="English" default>
<p>Your browser cannot play this video. <a href="clip.mp4">Download it instead</a>.</p>
</video>The source and track elements are consumed by the media element. The p is the fallback.
What triggers it, and what does not
Fallback shows only when the browser does not recognize the element at all. Every current browser supports audio and video, so you will rarely see fallback today. It matters for very old browsers, some embedded or reader-mode renderers, and tools that strip media.
Fallback does not show when the element exists but the file fails. If no source has a type the browser can play, or the file is missing, the browser keeps the element and fires an error event. The user sees a broken player, not your fallback paragraph.
<audio controls src="podcast-episode.mp3">
<a href="podcast-episode.mp3">Download the episode</a>
</audio><br>
Writing useful fallback
Good fallback gives the user another way to get the content.
- Link to the file so the user can download it or open it in another app.
- Name the format and, if helpful, the size, so the user knows what they are getting.
- Offer a transcript link for audio, since it helps in any situation where playback fails.
- Keep it short. Fallback is plain HTML and can hold links, text, or an image, but it should not duplicate the whole page.
A link to a single format is enough. If you offer several formats, link to the one most devices open, usually MP4 for video and MP3 for audio.
Common mistakes
- Putting fallback before
sourceelements. Order does not change what the browser does, but keepingsourceandtrackfirst makes the markup easier to read. - Writing "Your browser does not support video" with no link. The user is left with no way forward.
- Linking to a file in a format the user's device also cannot open. Choose the most widely supported format.
- Placing fallback outside the element. Only content inside
audioorvideocounts as fallback.
Exercise
A page has a video element with one source of type video/webm. A visitor on a browser that supports video but cannot decode WebM opens the page. Does the fallback paragraph inside the element appear? What should you change?
Show answer
No. The browser recognizes the video element, so it ignores the fallback and shows a player that cannot play the file. Add a second source in a widely supported format such as MP4, and listen for the error event if you want to show a message when no source works.