πΈ Images & Responsive Media — A Modern HTML Playbook
TL;DR: Choose the right format, add
srcset
andsizes
, sprinkleloading="lazy"
—and your pages will load faster, look sharper, and rank higher.
1 · Why Images Deserve Extra Care
-
Performance: Images often make up 60-80 % of total page weight. Shrink them and Core Web Vitals smile.
-
Accessibility: Well-written
alt
text is critical for screen-reader users and poor-network scenarios. -
SEO & Sharing: Search engines use captions, filenames, and structured data to fuel Image Search and social previews.
-
Responsiveness: From 320-px phones to 5-k monitors, the same file won’t fit all.
2 · Image Formats 2025 Cheat-Sheet
Format | Best For | Pros | Cons |
---|---|---|---|
JPEG | Photos with gradients | Widely supported, small | No transparency |
PNG-8/24 | Logos, UI icons needing alpha | Lossless, crisp | Larger than JPEG/WebP |
SVG | Logos, flat art, charts | Infinitely scalable, editable | Can bloat if over-complex |
WebP | Photos & illustrations | 25–35 % smaller than JPEG/PNG | IE & old Safari need fallback |
AVIF | High-quality photos | Up to 50 % smaller than JPEG | Slower encode, partial browser support |
GIF | Tiny animations | Universal | 256-color limit, heavy |
Rule of thumb:
Vector? → SVG. Raster? → AVIF/WebP first, JPEG fallback. Need transparency? → WebP/PNG.
3 · Basic <img>
Anatomy
<img
src="hero-800.jpg"
alt="Sunrise over the Himalayas"
width="800"
height="533"
loading="lazy"
/>
Attribute | Purpose |
---|---|
src |
Default source |
alt |
Text alternative (required!) |
width / height |
Prevents Cumulative Layout Shift |
loading="lazy" |
Defers off-screen images until near viewport |
decoding="async" (optional) |
Lets browser decode in parallel |
4 · Responsive Images with srcset
+ sizes
4.1 Density Switching (retina support)
Browser picks avatar-2x.jpg
on high-DPI screens.
4.2 Art Direction with <picture>
Mobile first fallback <img>
plus separate desktop art.
4.3 Fluid Layouts (sizes
)
Reads: on big screens take 33 % of viewport width; else full width. Browser chooses the nearest larger candidate.
5 · Lazy-Loading Strategies
Technique | When to Use |
---|---|
Native loading="lazy" |
Most images below the fold—zero JS |
lazysizes | Need IE or extra features (blur-up) |
Intersection Observer | Custom fade-in, analytics events |
Tip: Keep critical (above-the-fold) hero images eager to avoid blank flashes.
6 · Background Images
Never encode text inside background images—screen-readers can’t reach them.
7 · Video & Audio Media
7.1 Native <video>
-
Use captions:
<track kind="captions" src="intro.vtt" srclang="en">
-
For mute-autoplay inline hero loops: add
muted autoplay loop playsinline
.
7.2 External Hosts
-
YouTube: Great reach, auto-adaptive but heavier privacy; use
"youtube-nocookie.com"
+loading="lazy"
. -
Mux / Cloudflare Stream: Pay-as-you-go, HLS & DASH, fine-grained controls.
8 · Accessibility Checklist β
-
Meaningful
alt
text — describe purpose, not decoration. -
role="img"
for CSS background icons in SVG. -
Captions & transcripts for video/audio.
-
Contrast in overlaid text—meets WCAG AA.
-
Focus styles for custom lightboxes.
9 · Performance & Automation Tips
Tool | What It Does |
---|---|
ImageMagick / Sharp | Batch convert to WebP/AVIF |
Squoosh CLI | Quick compress, resize |
gulp-responsive / vite-image-tools | Generate multi-size srcset |
Cloudinary / imgix | On-the-fly resizing, CDN, AVIF fallback |
Lighthouse | Detect large or uncompressed images |
Budget: Aim for < 100 KB hero, < 50 KB inline content images.
10 · Common Mistakes & Fixes
Mistake | Fix |
---|---|
Using 4-MB DSLR photo straight on site | Resize to actual render size, compress to WebP |
All images set to loading="lazy" |
Leave first viewport images eager |
Missing width / height attrs |
Add them or use modern CSS aspect-ratio boxes |
Omitting alt |
Provide succinct description; if purely decorative use alt="" |
Relying only on WebP | Provide JPEG/PNG fallback via <picture> |
11 · Quick Reference Snippet
<picture>
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img
src="hero.jpg"
srcset="hero-480.jpg 480w, hero-800.jpg 800w"
sizes="(min-width: 1024px) 50vw, 100vw"
alt="Mountain lake sunrise"
width="800" height="533"
loading="lazy">
</picture>
Copy-paste, swap filenames, done. π
12 · Wrap-Up
-
Pick the right format (AVIF/WebP/JPEG/SVG).
-
Add
srcset
+sizes
or<picture>
for flexibility. -
Lazy-load what’s below the fold.
-
Compress, compress, compress.
-
Caption and describe for humans and bots alike.
Master these steps and your site will load faster, rank higher, and serve every device perfectly. Happy optimizing! π
π‘ Frequently Asked Questions (FAQ) – Images & Responsive Media
# | Question | Short Answer |
---|---|---|
1 | When should I use <picture> instead of plain <img> ? |
Use <picture> for art-direction (different crops/artwork per breakpoint) or to offer next-gen formats (AVIF/WebP) with JPEG/PNG fallback. |
2 | Is loading="lazy" safe for all images? |
Apply it to below-the-fold images. Keep above-the-fold (hero/logo) “eager” or browsers may flash empty space. |
3 | Do I still need width/height if I have srcset ? |
Yes—explicit dimensions prevent Cumulative Layout Shift (CLS) even while the browser picks the best source. |
4 | What alt text should a purely decorative image get? | Use an empty string: alt="" . That tells screen-readers to skip it. |
5 | Which is smaller: WebP or AVIF? | AVIF usually wins (10-20 % smaller than WebP), but encoding is slower and older browsers lack support—always provide fallback. |
6 | Can I autoplay video on mobile? | Only if it’s muted and has playsinline . Otherwise most mobile browsers block autoplay to save data. |
7 | How many srcset breakpoints are ideal? |
Three to five sizes typically balance quality vs. overhead (e.g., 480 w, 800 w, 1200 w). |
8 | Why are my SVG icons blurry in Chrome? | Probably raster screenshots, not true SVG vectors. Ensure paths/polygons, set shape-rendering="crispEdges" for pixel-perfect icons. |
9 | What’s better for UI icons—SVG sprite or individual files? | SVG sprites reduce requests but complicate caching; for HTTP/2+ many devs use individual SVGs, cached aggressively. |
10 | Does Google index images in <picture> ? |
Yes—Google images parses <picture> and considers the fallback <img> source for indexing. Make sure that <img> has the alt text. |