Chapter 12: Images & Media

Images & Media in HTML — one of the most exciting (and visual!) parts of building websites.

After all, the web is not just text — images, videos, audio, and graphics make pages come alive, tell stories faster, and keep users engaged longer. But if not handled properly, they can also slow down your site, break accessibility, or look bad on mobile.

1. Why Images & Media Matter So Much

  • Images = 50–70% of most web page weight (biggest performance killer if not optimized)
  • Good media → higher engagement, better storytelling
  • Bad media → slow loading, accessibility issues (blind users can’t “see” images), SEO penalty
  • Modern goal: Fast + beautiful + accessible + responsive on every device

2. Core HTML Elements for Images & Media

A. The <img> Tag – The Most Important One

This is how you add almost every static image.

Basic (but incomplete) syntax:

HTML

Mandatory attributes (never skip these!):

  • src = source URL or path (relative or absolute)
  • alt = alternative text (accessibility + SEO + shows if image fails to load)

Good alt examples:

  • Good: alt=”Young developer smiling while coding in VS Code in Hyderabad cafe”
  • Bad: alt=”img1.jpg” or alt=”” (unless purely decorative)

Decorative images (logos, icons, backgrounds that add no meaning):

HTML

B. Modern Responsive Images (2026 Must-Know)

We don’t serve the same huge image to phone and 4K monitor — that wastes data.

Two powerful techniques:

  1. srcset + sizes (most common)
HTML
  • srcset = list of images with width descriptors (w)
  • sizes = tells browser which image to pick based on viewport width
  1. <picture> element (art direction / format switching)
HTML

WebP/AVIF = smaller files, same quality → use them in 2026!

C. <figure> + <figcaption> – Semantic Image + Caption

Wrap images that need explanation (like diagrams, photos in articles).

HTML

This is semantic — screen readers announce “figure” + caption.

D. Audio – The <audio> Element

Simple embedded sound player.

HTML
  • controls = shows play/pause/volume bar
  • Fallback text inside for old browsers

E. Video – The <video> Element

Similar to audio, but with bigger impact.

HTML
  • poster = thumbnail image before play
  • Multiple <source> for format fallback (MP4 + WebM best combo)
  • width/height or CSS control size

3. Complete Example – Images & Media Page

index.html (copy-paste & run with Live Server)

HTML

style.css (basic clean look)

CSS

Save → Open with Live Server → resize browser window → see how images adapt!

Quick Mastery Checklist (2026 Edition)

  • Always include meaningful alt text
  • Use srcset + sizes for responsive images
  • Prefer WebP/AVIF with <picture> fallback
  • Wrap meaningful images in <figure> + <figcaption>
  • Add controls to <audio>/<video>
  • Provide fallback content inside media tags
  • Never stretch images (use height:auto or object-fit in CSS)
  • Compress images before upload (tools: TinyPNG, Squoosh)

How does the page look/feel when you resize or on your phone? Images loading fast? Captions clear?

Next steps you might want (just say):

  • “how to optimize images for speed”
  • “add lazy loading to images”
  • “create image gallery with CSS”
  • “embed YouTube video properly”
  • “background images vs <img> tag”

You’re making pages more alive now — awesome progress! Chai break? Then let’s keep going ☕🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *