HTML Media Elements
Master multimedia in HTML with comprehensive coverage of video, audio, images, and embedded content. Learn modern techniques for responsive, accessible, and performant media.
How do you add audio and video to a web page?
Use the native <video> and <audio> elements with a src (or several <source> children) and the controls attribute to show the browser's player. They play without plugins, support captions through <track>, and can be controlled from JavaScript. For YouTube or Vimeo content, embed their player with an <iframe>.
<video controls width="640" poster="thumb.jpg" preload="metadata">
<source src="intro.webm" type="video/webm">
<source src="intro.mp4" type="video/mp4">
<track kind="captions" src="intro.en.vtt" srclang="en" label="English">
</video>HTML Media Elements — step by step
Embed media with audio and video elements
Use the audio element for sound and the video element for video content. Both support src for a single file or multiple source children for format fallbacks. Add controls to show native playback controls to users.
🎯 Quick Check
Which attribute on the video element specifies a thumbnail image before the video plays?
Modern HTML Media
HTML5 revolutionized web media with native support for video, audio, and advanced image handling. Modern web applications require rich multimedia experiences that are responsive, accessible, and performant across all devices and network conditions.
Video
Native playback
Audio
Sound control
Images
Responsive design
Embedded
External content
Media Element Types
🎥 Video Elements
- • Native HTML5 video playback
- • Multiple format support (MP4, WebM, OGV)
- • Built-in controls and custom styling
- • Responsive and adaptive streaming
- • Poster images and thumbnails
- • Accessibility features (captions, descriptions)
🎵 Audio Elements
- • HTML5 audio with native controls
- • Format flexibility (MP3, OGG, AAC, WAV)
- • Streaming and progressive download
- • Volume control and muting
- • Loop and autoplay capabilities
- • Preload strategies for performance
🖼️ Advanced Images
- • Responsive image techniques
- • Picture element for art direction
- • Srcset for resolution switching
- • Modern formats (WebP, AVIF)
- • Lazy loading strategies
- • Image maps and interactions
🌐 Embedded Content
- • YouTube and Vimeo integration
- • Interactive maps and widgets
- • Social media embeds
- • Third-party content security
- • Responsive iframe techniques
- • Performance considerations
Complete Media Implementation
Explore a comprehensive implementation showcasing all major HTML media elements with modern best practices:
Interactive media showcase with all elements:
Code Editor
<h3>Image</h3>
<img src="https://picsum.photos/id/1080/300/150" alt="Strawberries" width="300" height="150">
<h3>Audio</h3>
<audio controls src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>
<h3>Video</h3>
<video controls width="300" playsinline
src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4"></video>
<h3>Embed (iframe)</h3>
<iframe src="https://www.openstreetmap.org/export/embed.html?bbox=74.3,31.5,74.4,31.6" width="300" height="150" title="Map" loading="lazy"></iframe>Preview
Implementation Features:
- • Multiple video formats and fallbacks
- • Audio with preload strategies
- • Responsive image techniques
- • Embedded YouTube and maps
- • Accessibility considerations
- • Performance optimizations
Browser Compatibility:
- • HTML5 video/audio: Universal support
- • Picture element: Modern browsers
- • WebM format: Chrome, Firefox, Edge
- • Responsive techniques: All browsers
- • Iframe embedding: Universal support
- • Modern image formats: Progressive enhancement
Performance & Accessibility
Performance Tips
- • Optimize file sizes and compression
- • Use appropriate preload strategies
- • Implement lazy loading for images
- • Choose efficient formats (WebP, AV1)
- • Use CDN for media delivery
- • Consider bandwidth limitations
♿ Accessibility
- • Provide captions for video content
- • Include transcripts for audio
- • Use descriptive alt text for images
- • Ensure keyboard navigation works
- • Provide pause/stop controls
- • Respect user preferences
📱 Mobile Considerations
- • Avoid autoplay on mobile devices
- • Provide multiple format options
- • Optimize for touch interactions
- • Consider data usage costs
- • Test across various devices
- • Use appropriate sizing
🎯 Best Practices Summary
Content Strategy:
- • Always provide fallback content
- • Use semantic HTML elements
- • Implement progressive enhancement
- • Test across different browsers
User Experience:
- • Respect user control and preferences
- • Provide clear loading indicators
- • Ensure content is accessible to all
- • Optimize for performance
Technical Specifications
Video Formats & Codecs
MP4 (H.264)
Universal support, good compression
WebM (VP9/AV1)
Modern browsers, excellent compression
OGV (Theora)
Open source, legacy support
Audio Formats
MP3
Universal support, good for music
OGG Vorbis
Open source, high quality
AAC
High quality, efficient compression
Common Attributes Reference
Video Attributes
src- Video source URLcontrols- Show controlsautoplay- Auto-startmuted- Start mutedloop- Repeat videoposter- Thumbnail imagewidth/height- Dimensions
Audio Attributes
src- Audio source URLcontrols- Show controlsautoplay- Auto-startloop- Repeat audiopreload- Loading strategyvolume- Default volume
Image Attributes
src- Image source URLalt- Alternative textwidth/height- Dimensionssrcset- Multiple sourcessizes- Size hintsloading- Lazy loading
Media Mastery Achieved! 🎬
Outstanding! You've mastered HTML media elements with comprehensive coverage of video, audio, images, and embedded content. You're now ready to create rich, multimedia web experiences. Let's continue with specialized audio techniques!
HTML Media Elements: summary
Key points
- Browsers only autoplay media that is
muted. preload="none"or"metadata"avoids downloading large files until they're needed.- Captions (WebVTT files) make video accessible and also help people watching without sound.
Frequently asked questions
›Why won't my video autoplay?
Browsers block autoplay with sound. Add muted (and playsinline for iOS) together with autoplay.
›Should I host video myself or use YouTube?
Large or frequently watched videos are usually cheaper and faster on a video platform. Self-host short clips and background loops in WebM or MP4 format.