HTML5 Media: Images, Responsive Picture & Video
Master high-performance image loading, responsive picture art direction, resolution switching (srcset & sizes), and audio/video streaming tags.
1. The Modern <img> Element & Core Web Vitals
The <img> tag is a replaced void element that embeds raster or vector graphic assets into the document. In modern frontend development, proper image attributes directly dictate Core Web Vitals scores (LCP and CLS):
💡 Real-World Analogy: Custom Framing in an Art Gallery
Think of web browser image rendering like hanging paintings in an art gallery:
• Width & Height Attributes are pre-reserved wall frames. The browser reserves the space before the canvas arrives, preventing the entire gallery wall from jumping when loaded (prevents Cumulative Layout Shift)!
• loading="lazy" is on-demand delivery: only bring the painting from the warehouse when the visitor walks into that specific showroom!
• alt text is the museum audio tour guide describing the masterpiece for visually impaired guests.
The alt Attribute
Screen Readers & SEO
- Informative: Describe meaning and context (e.g.
alt="Quarterly engineering growth graph 2026"). - Decorative: Use
alt=""so screen readers skip background patterns. - Never omit
altentirely!
Intrinsic Ratio (CLS)
Zero Layout Shifts
- Always supply raw
width="800" height="600"attributes. - Browser calculates
aspect-ratio: 800/600automatically. - Prevents janky content jumps as images download.
Fetch Optimization
Speed & Battery Saver
loading="lazy": Defers offscreen image loading until near viewport.decoding="async": Decodes image on background thread without blocking main thread.fetchpriority="high": Prioritizes above-the-fold hero images.
<figure>
<img src="https://images.unsplash.com/photo-1517694712202-14dd9538aa97?w=600"
alt="Modern programming workstation"
loading="lazy"
decoding="async">
<figcaption>
Figure 1: High performance developer workspace.
</figcaption>
</figure>
2. Resolution Switching with srcset & sizes
Delivering a 4K 4000px image to a 375px mobile phone wastes bandwidth, slows downloads, and degrades battery life. srcset lets you provide a portfolio of image widths so the browser automatically downloads the perfect size:
<!-- Browser reads viewport width + screen pixel density to pick smallest optimal file -->
<img src="photo-800.jpg"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1200.jpg 1200w,
photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw,
(max-width: 1024px) 50vw,
800px"
alt="Software development team collaboration workshop"
loading="lazy">
📐 How sizes & srcset Math Works
• 400w, 800w, 1200w informs the browser of the exact pixel width of each image file on the server.
• sizes="(max-width: 600px) 100vw, 800px" tells the browser what CSS display slot width the image will occupy before CSS loads.
• On a 375px mobile device with 2x Retina screen, required pixels = 375px * 2 = 750px. The browser smartly downloads photo-800.jpg!
3. Art Direction & Next-Gen Formats with <picture>
While srcset on <img> handles resolution switching, the <picture> element is required when you need to crop the image differently on mobile (Art Direction) or serve cutting-edge formats (AVIF / WebP) with legacy fallbacks:
<picture>
<!-- 1. Mobile Portrait Crop in Ultra-Compressed AVIF format -->
<source media="(max-width: 640px)" srcset="hero-square.avif" type="image/avif">
<!-- 2. Mobile Crop fallback in WebP -->
<source media="(max-width: 640px)" srcset="hero-square.webp" type="image/webp">
<!-- 3. Desktop Widescreen in AVIF -->
<source srcset="hero-wide.avif" type="image/avif">
<!-- 4. Desktop Widescreen in WebP -->
<source srcset="hero-wide.webp" type="image/webp">
<!-- 5. Mandatory Fallback <img> (receives the rendered attributes) -->
<img src="hero-wide.jpg"
alt="Modern corporate campus"
width="1600"
height="900"
loading="lazy">
</picture>
4. HTML5 Video & Audio Streaming Elements
Native HTML5 audio and video elements completely replace legacy Flash plugins with hardware-accelerated media players:
| Attribute | Target Element | Functional Behavior |
|---|---|---|
controls |
<video> / <audio> |
Renders native browser playback UI (play/pause button, scrubber, volume, fullscreen). |
poster="thumb.jpg" |
<video> |
Displays placeholder cover art image before video playback initiates. |
muted + autoplay |
<video> |
Mandatory pair for autoplay. Modern browsers block all audio-enabled autoplay to prevent startling users. |
playsinline |
<video> |
Forces video to play inline within the webpage on iOS Safari instead of taking over fullscreen. |
<track kind="subtitles"> |
<video> |
Provides WebVTT captions, subtitles, and descriptions for accessibility compliance. |
<div class="media-card">
<h4>Frontend Mock Interview Prep</h4>
<audio controls style="width: 100%;">
<source src="interview-prep.mp3" type="audio/mpeg">
Your browser does not support audio element.
</audio>
</div>
🎙️ Frontend Mock Interview Prep
5. HTML5 Media Placement Interview Quiz Deck
Test your knowledge on image optimization, responsive design, and video playback rules:
🎯 Placement Interview Takeaways
- Q: Should lazy loading (
loading="lazy") be added to the Hero image?
Ans: NO! Addingloading="lazy"to the primary above-the-fold hero image delays Largest Contentful Paint (LCP) because the browser defers loading until layout recalculation. Useloading="eager"andfetchpriority="high"for hero images. - Q: What is the difference between WebP and AVIF image formats?
Ans: Both are modern compressed image codecs. WebP offers ~30% smaller sizes than JPEG. AVIF (based on the AV1 video codec) achieves up to ~50% smaller sizes with superior HDR and color fidelity, but requires slightly more CPU decoding time. - Q: Why is an
<img>element still required inside a<picture>tag?
Ans: The<picture>element itself is an invisible wrapper providing candidate rules. The internal<img>tag is what is actually rendered in the DOM tree, displaying the chosen source. Without an<img>, nothing renders!