HTML Audio/Video Reference
In this page:
Standard Audio & Video Elements
HTML5 introduced native audio and video elements to replace insecure, third-party plug-ins, allowing you to embed media files directly onto your webpage.
- Offer both MP4 and WebM formats to ensure your videos play on all devices.
- Accepted attributes:
- src — media file URL
- controls / autoplay / loop / muted — booleans
- preload — none | metadata | auto
- width / height / poster / playsinline — video only
Example: Standard Audio & Video Elements
<video src="movie.mp4" controls></video>
<audio src="song.mp3" controls></audio>
Playback Control Attributes
You can customize how your media players behave by using built-in attributes: controls displays playback buttons, autoplay starts playback automatically, loop repeats the file, and muted silences the audio.
- Always include the controls attribute so your users can pause and play the media.
- Accepted attributes:
- controls — boolean
- autoplay — boolean
- loop — boolean
- muted — boolean
- playsinline — boolean (video)
- poster — image URL (video)
Example: Playback Control Attributes
<video src="movie.mp4" controls autoplay loop muted></video>
Optimizing with preload
The preload attribute tells the browser how much of the media file to download in the background when the page loads, allowing you to choose between none, metadata, or auto settings.
- Use preload="none" on pages with multiple media players to keep your site loading quickly.
- Accepted values:
- none
- metadata
- auto
Example: Optimizing with preload
<audio src="song.mp3" controls preload="metadata"></audio>
Adding Subtitles with track
The track element is used to add timed text subtitles or captions to your videos, referencing a standard WebVTT (.vtt) file to display synchronised text on top of your video.
- Set the default attribute on your track tag to display subtitles automatically.
- Accepted attributes:
- kind — subtitles | captions | descriptions | chapters | metadata
- src — .vtt URL
- srclang — language code
- label — menu text
- default — boolean
Example: Adding Subtitles with track
<video src="movie.mp4" controls>
<track src="captions.vtt" kind="subtitles" srclang="en" default>
</video>
Media Source Fallback Configurations
To ensure your media plays on all devices, you can provide multiple source elements inside your media tags, allowing the browser to select and play the first compatible format it finds.
- Offer both MP4 and WebM formats for videos, and MP3 and OGG formats for audio files.
- Accepted attributes:
- source src — file URL
- source type — MIME type such as video/mp4 | video/webm | audio/mpeg
- source media — media query (optional)
Example: Media Source Fallback Configurations
<video controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.webm" type="video/webm">
</video>
- Forgetting to include the controls attribute, leaving users unable to interact with your media players.
- Attempting to auto-play media files with sound enabled, which modern browsers will block.
- Using large, uncompressed raw file formats (like AVI or WAV) that are not widely supported by modern browsers.
- Modern HTML5 native media elements (audio and video) have replaced old third-party plugins.
- Customize how your media players behave by using attributes: controls, autoplay, loop, and muted.
- Use the preload attribute to optimize background loading speeds, and track tags to add timed text subtitles for web accessibility.
Standard HTML5 audio and video elements are supported natively by all modern web browsers.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep