Beyond text and images, modern websites include audio, video, and interactive media. This tutorial covers embedding and optimizing media content.
What you’ll learn:
- The
<audio> and <video> elements
- Video and audio formats and codecs
- Using multiple source files for compatibility
- Responsive images with
<picture> and srcset
- The
<figure> element for illustrations
- Accessibility for media content
- Optimizing media file sizes
Prerequisites:
The <audio> element plays sound files:
The controls attribute adds play, pause, and volume buttons. The fallback text appears if the browser doesn’t support HTML5 audio.
Different browsers support different formats. Provide multiple sources:
Browsers use the first format they support. Common formats:
- MP3 (
audio/mpeg) — Most widely supported
- WAV (
audio/wav) — High quality, larger files
- OGG (
audio/ogg) — Open source, good compression
- AAC (
audio/aac) — Used by iTunes
controls: Shows play/pause buttons
autoplay: Starts playing automatically
loop: Repeats when it ends
muted: Starts muted (required for autoplay in many browsers)
preload="auto|metadata|none": How much to load before playing
The <video> element plays video files:
controls: Shows play, pause, progress bar, volume
width/height: Video dimensions (maintain aspect ratio)
poster: Image to show before playing
autoplay: Starts playing automatically
loop: Repeats when it ends
muted: Required for autoplay in most browsers
Common formats:
- MP4 (
video/mp4) — Most widely supported, good compression
- WebM (
video/webm) — Open source, excellent compression
- Ogg/Theora (
video/ogg) — Open source, older browsers
Always include MP4 first for compatibility.
The <picture> element lets you serve different images for different screen sizes:
The browser:
- Checks each
<source> media query from top to bottom
- Uses the first matching source
- Falls back to
<img> if nothing matches
- The
<img> alt text is used for accessibility
The srcset attribute specifies multiple image sizes:
The browser downloads only the image that matches:
- The viewport size
- The device pixel ratio (2x for retina)
This saves bandwidth on mobile devices.
For simple cases, use srcset directly on <img>:
The sizes attribute tells the browser what width the image will be displayed at:
- On small screens (under 600px): 100% of viewport width
- On medium screens (under 1200px): 50% of viewport width
- On large screens: 33% of viewport width
Always provide alternatives for media content:
The <track> element adds captions, subtitles, or descriptions:
kind="captions" — Captions for people who are deaf or hard of hearing
kind="subtitles" — Translations or dialog
kind="descriptions" — Describes what’s happening in the video
srclang — Language code (en, es, fr, etc.)
Media files can be large. Here are optimization tips:
<!-- Use modern formats -->
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="description">
WebP images are 25-35% smaller than JPEG while maintaining quality.
When preparing videos:
- Use H.264 codec for MP4 (best compatibility)
- Use VP8/VP9 for WebM (better compression)
- Aim for 1-2 Mbps bitrate for web
- Use a service like Handbrake to compress
- MP3: 128 kbps is acceptable for most audio
- Use 44.1 kHz sample rate (standard)
- Mono for voice, stereo for music
Create a media-rich article:
- Use
<audio> and <video> for media content
- Provide multiple formats for compatibility (MP4 + WebM for video, MP3 + OGG for audio)
- Use
<picture> and srcset for responsive images
- Add
<track> elements for captions and descriptions
- Always include alt text for images
- Optimize file sizes for web delivery
- Provide transcripts for audio and captions for video
- Use
poster attribute to show a preview image
- Include fallback text for older browsers
Metadata & SEO
Optimize your pages with meta tags and structured data.
Continue →
Accessibility
Deep dive into WCAG compliance and ARIA attributes.
Continue →