HTML Audio & Video Elements
Before HTML5, embedding audio or video in a webpage required third-party plugins
like Flash — which were slow, insecure, and unsupported on mobile. HTML5 introduced the native <audio> and <video>
elements, letting browsers play media directly. In this chapter you'll master both elements, all their
attributes, format fallbacks with <source>, captioning with <track>, and media accessibility best practices.
<audio> element — all
attributes and usage<video> element —
poster, dimensions, accessibility<source><track> and WebVTTThe <audio> Element
The <audio> element embeds sound content
directly in a web page. It renders the browser's native audio player when the controls attribute is present. Without controls, the audio is completely invisible — useful for background music
controlled by JavaScript.
Your browser does not support the audio element.
</audio>
<!-- Basic audio player with controls -->
<audio src="lecture.mp3" controls>
Your browser does not support the audio element.
</audio>
<!-- Multiple formats for browser compatibility -->
<audio controls preload="metadata">
<source src="lecture.ogg" type="audio/ogg">
<source src="lecture.mp3" type="audio/mpeg">
Your browser does not support HTML5 audio.
</audio>
<!-- Background audio (JS-controlled, no UI) -->
<audio id="bg-music" src="ambient.mp3"
loop muted preload="auto">
</audio>
The <video> Element
The <video> element embeds video content
natively — no Flash, no plugins. It supports all the same attributes as <audio>, plus video-specific attributes like poster, width, and height. Here is a mock player showing what the element renders:
<!-- Basic responsive video with controls -->
<video
src="course-preview.mp4"
controls
poster="thumbnail.jpg"
width="800"
height="450"
preload="metadata"
>
Your browser does not support the video element.
</video>
<!-- Multiple formats + captions -->
<video controls poster="thumb.jpg" width="100%">
<source src="video.webm" type="video/webm">
<source src="video.mp4" type="video/mp4">
<track
src="captions-en.vtt"
kind="captions"
srclang="en"
label="English"
default
>
Your browser does not support HTML5 video.
</video>
<!-- Autoplaying hero video (muted required) -->
<video
src="hero-bg.mp4"
autoplay
muted
loop
playsinline
preload="auto"
aria-hidden="true"
></video>
All <audio> & <video> Attributes
Here is a complete reference of every attribute supported by the audio and video elements:
video.mp4) or absolute. Omit when using multiple <source> child elements for format fallbacks — the browser picks
the first format it supports.muted. Use sparingly — autoplaying media is disruptive and an
accessibility failure for some users.muted for background videos.none (load nothing until play), metadata (load duration, dimensions only), auto (load the full file). Default varies by browser. Use metadata for performance.width="100%" in CSS and use aspect-ratio: 16/9 instead of fixed pixel values.playsinline Attribute
playsinline to a <video> element
makes it play inline within the page — essential for background hero videos and inline video players. Always
include it alongside autoplay muted loop for background videos.
The <source> Element — Format Fallbacks
Different browsers support different media formats. The <source> element lets you provide multiple format
options — the browser picks the first format it can play and ignores the rest. This ensures
maximum compatibility without JavaScript.
<!-- Video fallback: WebM → MP4 -->
<video controls poster="thumb.jpg" width="800" height="450">
<!-- Browser tries WebM first (smaller file size) -->
<source src="video.webm" type="video/webm">
<!-- Falls back to MP4 if WebM unsupported -->
<source src="video.mp4" type="video/mp4">
<!-- Text shown only in non-HTML5 browsers -->
<p>Your browser doesn't support HTML5 video.
<a href="video.mp4">Download it</a>.</p>
</video>
<!-- Audio fallback: OGG → MP3 -->
<audio controls>
<source src="audio.ogg" type="audio/ogg">
<source src="audio.mp3" type="audio/mpeg">
Your browser does not support HTML5 audio.
</audio>
Here is the browser support matrix for the most common media formats:
| Format | Type | MIME type | Chrome | Firefox | Safari | Best Use |
|---|---|---|---|---|---|---|
| MP4 (H.264) | Video | video/mp4 |
✓ | ✓ | ✓ | Universal fallback — widest support |
| WebM (VP9) | Video | video/webm |
✓ | ✓ | Partial | Smaller file size — use first, fall back to MP4 |
| OGG Theora | Video | video/ogg |
✓ | ✓ | ✗ | Open-source — not recommended as primary format |
| MP3 | Audio | audio/mpeg |
✓ | ✓ | ✓ | Universal audio — safe default for all browsers |
| OGG Vorbis | Audio | audio/ogg |
✓ | ✓ | ✗ | Open-source audio — use before MP3 as first source |
| WAV | Audio | audio/wav |
✓ | ✓ | ✓ | Uncompressed — large files, use for short sound effects only |
type attribute on each
<source> — without it the browser must download part of each file
to detect the format.
The <track> Element — Captions &
Subtitles
The <track> element adds timed text tracks
to video (and audio) — captions, subtitles, chapter markers, and descriptions. It uses
WebVTT (.vtt) files — a plain-text format for timed text. WCAG 1.2.2 requires captions for
all pre-recorded video with audio.
<video controls poster="thumb.jpg" width="800" height="450">
<source src="course.mp4" type="video/mp4">
<!-- English captions (default) -->
<track
src="captions-en.vtt"
kind="captions"
srclang="en"
label="English"
default
>
<!-- Hindi subtitles -->
<track
src="subtitles-hi.vtt"
kind="subtitles"
srclang="hi"
label="हिंदी"
>
<!-- Gujarati subtitles -->
<track
src="subtitles-gu.vtt"
kind="subtitles"
srclang="gu"
label="ગુજરાતી"
>
<!-- Chapter markers -->
<track
src="chapters.vtt"
kind="chapters"
srclang="en"
label="Chapters"
>
</video>
Here is what a WebVTT caption file (.vtt) looks like:
WEBVTT on the first line. Each cue has an
optional number, a timestamp line (start --> end in HH:MM:SS.mmm
format), and one or more lines of caption text. Blank lines separate cues. Sound effects and non-speech
audio should be included in captions inside square brackets — e.g. [music playing].
Autoplay Policy — Why Autoplay Doesn't Always Work
All major browsers block autoplay with audio. This
is an intentional policy to prevent unwanted noise. Here is exactly how it works and what you can do:
autoplay muted will always
play automatically in all browsers. This is the recommended approach for hero
background videos and introductory animations. The user can unmute with controls if you provide them.
.play() with audio. Use a play button as
the trigger — then call audio.play() on click.<!-- ✅ Background video — always autoplays -->
<video
src="hero-bg.mp4"
autoplay muted loop playsinline
aria-hidden="true"
></video>
<!-- ✅ Audio triggered by user click -->
<button type="button" id="playBtn">▶ Play Intro</button>
<audio id="intro-audio" src="intro.mp3" preload="auto"></audio>
<script>
document.getElementById('playBtn')
.addEventListener('click', () => {
document.getElementById('intro-audio').play();
});
</script>
Responsive & Accessible Video
Making video work perfectly on all screen sizes and for all users requires a few CSS and HTML patterns:
<!-- Responsive wrapper preserving 16:9 aspect ratio -->
<div class="video-wrap">
<video
controls
poster="thumb.jpg"
preload="metadata"
aria-label="K2infocom HTML Tutorial — Chapter 16"
>
<source src="ch16.webm" type="video/webm">
<source src="ch16.mp4" type="video/mp4">
<track src="ch16-en.vtt" kind="captions"
srclang="en" label="English" default>
</video>
</div>
/* CSS — responsive 16:9 video */
.video-wrap {
width: 100%;
aspect-ratio: 16 / 9;
border-radius: 12px;
overflow: hidden;
}
.video-wrap video {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
aria-hidden="true" to hide from screen readers. For informative
videos with speech: add aria-label="Video description" and
always provide a <track kind="captions"> for hearing-impaired
users.
Common Mistakes — Do's & Don'ts
These are the most frequent audio/video mistakes beginners make:
<!-- Provide multiple formats -->
<video controls poster="thumb.jpg">
<source src="v.webm" type="video/webm">
<source src="v.mp4" type="video/mp4">
<!-- Captions for accessibility -->
<track src="caps.vtt"
kind="captions"
srclang="en"
label="English"
default>
</video>
<!-- Background: autoplay muted -->
<video autoplay muted loop
playsinline
aria-hidden="true">
</video>
<!-- Single format only — breaks Firefox -->
<video src="video.ogv" controls>
</video>
<!-- No captions — WCAG 1.2.2 failure -->
<video controls>
<source src="lecture.mp4"
type="video/mp4">
</video>
<!-- Autoplay with audio — blocked -->
<video autoplay src="noisy.mp4">
<!-- Browser silently ignores -->
</video>
Try It Yourself
Edit the media page below and click Open in Editor to see audio and video elements rendered live in full screen with real controls.
autoplay muted loop to the <video> tag and see the video start automatically (muted). Remove
muted — the browser will silently block autoplay with sound.
Quick Check — Chapter 16
Expand each question to check your understanding before moving on to the multiple choice quiz.
controls attribute is a boolean that tells the browser to
render its native media player UI — play/pause button, volume slider, progress/seek
bar, playback speed (in some browsers), download button, and fullscreen toggle (for video). Without
controls, the media element has no visible UI at
all — audio is completely invisible, and video shows only the poster or the first frame. You
would then need to build a custom player with JavaScript. For accessibility, always include controls unless you have a custom player that is fully
keyboard-accessible.
muted attribute: <video autoplay muted loop>. This is the standard pattern for hero
background videos. If you need sound, trigger .play() from a button
click — after user interaction, browsers allow programmatic audio playback.
<source> element provides multiple media file
options for the browser to choose from. The browser evaluates each <source> in order and plays the first one it supports — skipping
the rest. This solves format compatibility: WebM has better compression but Safari has limited support;
MP4 works everywhere but is larger. By listing webm first and mp4 second, Chrome/Firefox get the efficient WebM and Safari falls back
to MP4. Always include the type attribute — without it the browser
must download part of each file to detect the format, wasting bandwidth.
.vtt file starts
with the word WEBVTT, followed by numbered cue blocks each
containing a timestamp range and text. The <track> element
inside <video> links a WebVTT file to the video — the browser
overlays the timed text on the video frame at the correct time. The kind attribute specifies the type: captions (all audio info for deaf users), subtitles (translated dialogue), descriptions (audio descriptions for blind users), chapters (navigation points).
poster attribute specifies the URL of an image to display as the
video thumbnail before playback begins. Without it, the browser shows either a black
frame or the first frame of the video — which often appears blank while the video loads, causing an ugly
flash. Setting a poster: creates a professional first impression, prevents content layout shift (the
element has a defined size immediately), gives SEO crawlers an image to index for the video, and
improves perceived performance since the thumbnail appears instantly. Always design a dedicated poster
image — ideally with a play button overlaid — rather than using a random video frame.
preload="none" tells the browser to download
nothing until the user explicitly presses play — best for pages with many videos or when
bandwidth is precious. preload="metadata" downloads only the file's
metadata (duration, dimensions, first frame for poster) — recommended in most cases as
it enables showing duration without loading the full file. preload="auto" downloads the entire media file in the
background — use only when the video is the primary page content and you're confident the user will
watch it. Note: browsers may ignore the preload hint on mobile to
save cellular data, overriding your choice.
autoplay to guarantee a video plays automatically in all major browsers?
<track> kind value provides all audio information — dialogue AND sound effects —
for deaf users?preload value tells the browser to
download only the duration and dimensions — not the full media file?<audio>embeds sound;<video>embeds video — both render native browser media players whencontrolsis present. Withoutcontrols, media is invisible and must be controlled by JavaScript.- Key shared attributes:
controls,src,autoplay,muted,loop,preload. Video-only:poster,width,height,playsinline. - Use
<source>inside media elements to provide multiple format fallbacks. Browser picks first supported format. Always include thetypeattribute. Recommended: WebM first → MP4 fallback for video; OGG first → MP3 fallback for audio. <track>adds timed text — captions, subtitles, descriptions, chapters — from a WebVTT.vttfile. Adddefaultto enable the track by default. WCAG 1.2.2 requires captions for all pre-recorded video.- Autoplay policy:
autoplayalone is blocked with sound.autoplay mutedalways works. For hero background video always useautoplay muted loop playsinline aria-hidden="true". - Make video responsive with CSS
aspect-ratio: 16/9andwidth: 100%. Usepreload="metadata"for performance. Always set aposterto avoid layout shift and black flash.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions, and stay updated with new tutorials.