Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 7
HTML Video & Audio
Embed video and audio content natively in HTML5 without external plugins. Learn the <video> and <audio> elements, multiple source formats for cross-browser support, captions with <track>, and embedding external content with <iframe>.
HTML5 Video & Audio Elements
Before HTML5, embedding media required plugins like Flash. The <video> and <audio> elements provide native browser support. Use multiple <source> elements inside them for format fallback - browsers pick the first format they support. MP4 (H.264) is universally supported for video; WebM (VP9) offers better compression. MP3 is universally supported for audio; OGG Vorbis is the open alternative.
Native HTML5 multimedia replaces third-party plugins with clean markup
Video & Audio Attributes
- controls - Shows play/pause, volume, progress bar, fullscreen. Always include for user accessibility
- autoplay - Starts playing automatically. Most browsers block autoplay with sound - pair with muted for autoplay to work
- muted - Starts with no sound. Required for autoplay to work in modern browsers
- loop - Replays continuously. Good for background videos, bad for content videos
- poster - Image displayed before video plays. Use a high-quality thumbnail for better UX
- preload - 'none' (no preloading), 'metadata' (duration/dimensions only), 'auto' (download the whole file). Use 'metadata' for most cases
Warning
Autoplay with sound is blocked by all modern browsers. If you need autoplay, you MUST include the 'muted' attribute: <video autoplay muted>. This is a user-experience decision by browsers to prevent annoying auto-playing ads and videos. Background videos should always be muted, looped, and have no controls.
Video & Audio Code Examples
<!-- Video with multiple sources and captions -->
<video controls width="640" poster="thumbnail.jpg" preload="metadata">
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
<track src="captions-en.vtt" kind="subtitles" srclang="en" label="English" default>
<track src="captions-hi.vtt" kind="subtitles" srclang="hi" label="Hindi">
Your browser does not support the video element.
</video>
<!-- Audio player -->
<audio controls preload="metadata">
<source src="podcast.mp3" type="audio/mpeg">
<source src="podcast.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
<!-- Background video (autoplay, muted, no controls) -->
<video autoplay muted loop playsinline class="hero-video">
<source src="background.mp4" type="video/mp4">
</video>
<!-- Responsive video embed with iframe -->
<iframe
width="560" height="315"
src="https://player.example.com/embed/VIDEO_ID"
title="Video title for accessibility"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope"
allowfullscreen
loading="lazy"
></iframe>Try It Yourself: HTML Video & Audio
Quick Quiz: Video & Audio
Key Takeaways
- Embed video and audio content natively in HTML5 without external plugins.
- controls - Shows play/pause, volume, progress bar, fullscreen. Always include for user accessibility
- autoplay - Starts playing automatically. Most browsers block autoplay with sound - pair with muted for autoplay to work
- muted - Starts with no sound. Required for autoplay to work in modern browsers
Finished this lesson?