🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

HTML Images & SVGs: Performant Visuals

Learn to deploy web media. Master alt text, next-generation WebP formats, SVGs, and the complex art direction of the picture element.

Narrated Video Summary
data-composition-id="html-html-images"1280×720 @ 30fps9 clips3:01 total

Introduction to HTML Media

While text forms the structural backbone of the web, images provide the critical visual context that engages users. Today, we are mastering the technical integration of media. We will explore how to embed images, ensure strict accessibility, prevent catastrophic layout shifts, and utilize advanced elements for responsive art direction.

The Void Element: img

The `<img>` tag is classified as a 'Void Element', meaning it is self-closing and never wraps internal content. The single most critical attribute is `src` (source), which strictly defines the precise URL path to your image file.

Mandatory Accessibility: alt Text

Web accessibility is absolutely non-negotiable. Every `<img>` tag must include an `alt` (Alternative Text) attribute. Screen readers read this description to visually impaired users, and it renders visibly if the image fundamentally fails to download.

Preventing Cumulative Layout Shift

Cumulative Layout Shift (CLS) occurs when a downloading image brutally pushes text down the screen upon rendering. Professional developers actively prevent this by providing precise `width` and `height` attributes directly on the tag, allowing the browser to safely reserve the exact canvas area instantly.

Responsive Art Direction with Picture

Sometimes, scaling down a desktop image makes the subject too small. We utilize 'Art Direction' via the `<picture>` element. It wraps an `<img>` tag and utilizes multiple `<source>` tags. By writing CSS media queries inside the `media` attribute, the browser dynamically swaps the file.

Modern Formats: WebP and AVIF

Modern engineering offers formats like WebP and AVIF. They achieve vastly superior compression algorithms, providing smaller file sizes without noticeable visual degradation. Using `<picture>`, you serve these next-generation formats while automatically maintaining a `.jpg` fallback.

<div style="font-family: sans-serif; padding: 20px; background: #f8fafc; color: #334155; border-radius: 8px; border: 1px solid #e2e8f0; width: 100%; max-width: 600px; margin: 0 auto; overflow: auto;">
<source type="image/webp" srcset="img.webp">
</div>

Introduction to Scalable Vectors (SVG)

While JPGs are raster grids of fixed pixels, SVGs (Scalable Vector Graphics) are XML math. Because they are driven by mathematical formulas, SVGs scale infinitely without pixelation. Crucially, as raw code, you can inject them directly into HTML and modify colors dynamically via CSS.

<div style="font-family: sans-serif; padding: 20px; background: #f8fafc; color: #334155; border-radius: 8px; border: 1px solid #e2e8f0; width: 100%; max-width: 600px; margin: 0 auto; overflow: auto;">
<svg>
  <circle fill="blue" />
</svg>
</div>

Media Integration Mastered

Outstanding work! You mastered the architectural standards for integrating media. You understand strict accessibility with `alt` text, layout preservation with dimensions, art direction with `<picture>`, and next-gen formats like SVG. Next, we build Forms.

0:00 / 3:01
Scene 1 / 9 — Introduction to HTML Media
Total XP: 0|💻 html XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Media Node

Images and Graphics.


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

While text forms the structural backbone of the web, images provide the critical visual context that engages users. However, images are inherently massive assets. We must master technical integration to ensure strict accessibility, eradicate layout shifts, and implement responsive art direction.

1The Mechanics & Layout Shifts

The <img> tag is a 'Void Element', meaning it is self-closing and wraps no internal content. Its core engine relies on the src attribute to declare the precise URL of the asset.

However, a catastrophic performance error is failing to declare the image's dimensions. When an image downloads, it violently pushes text down the screen upon rendering, causing a Cumulative Layout Shift (CLS). Professional developers actively prevent this by providing precise width and height attributes directly on the tag. This allows the browser to instantly reserve the exact layout canvas area before the image file even begins downloading over the network.

+
<!-- Preventing Layout Shift -->
<img
  src="/assets/hero-banner.jpg"
  width="800"
  height="400"
  alt="Team working together">
localhost:3000
Space Reserved Natively

2Semantic Accessibility

Web accessibility is a strict technical requirement. Every single <img> tag must include an alt (Alternative Text) attribute. Screen readers natively parse this description and announce it to visually impaired users, providing them with the visual context they cannot see.

If an image is purely decorative (like a background swirl or a generic dividing line) and conveys absolutely no meaningful information, you must set the attribute to an empty string (alt=""). This explicitly commands the screen reader to silently ignore the node entirely, preventing it from reading out useless filenames like 'divider-line-blue-final.png'.

+
<!-- Meaningful Context -->
<img src="chart.jpg" alt="Sales increased 20%">

<!-- Purely Decorative -->
<img src="swirl.png" alt="">
localhost:3000
🔊 "Sales increased 20%"
(Swirl image is completely ignored)

3Responsive Art Direction

Serving a massive 4K desktop image to a mobile phone destroys bandwidth and often ruins the visual framing. We solve this via 'Art Direction' using the <picture> wrapper.

The <picture> tag houses multiple <source> elements. By attaching a CSS query directly to the media attribute, you command the browser's engine to dynamically swap out the image file based on the user's screen size. You can also use <source type="image/webp"> to serve highly compressed, next-generation formats to modern browsers, while seamlessly failing back to a standard .jpg for older systems.

+
<!-- Art Direction with Next-Gen format -->
<picture>
  <source media="(max-width: 600px)" srcset="mobile.webp">
  <!-- Mandatory Fallback -->
  <img src="desktop.jpg" alt="Hero graphic">
</picture>
localhost:3000
NameSize
mobile.webp24 KB
desktop.jpg1.2 MB

4Step-by-Step Breakdown

Introduction to HTML Media. While text forms the structural backbone of the web, images provide the critical visual context that engages users. Today, we are mastering the technical integration of media. We will explore how to embed images, ensure strict accessibility, prevent catastrophic layout shifts, and utilize advanced elements for responsive art direction.

The Void Element: img. The <img> tag is classified as a 'Void Element', meaning it is self-closing and never wraps internal content. The single most critical attribute is src (source), which strictly defines the precise URL path to your image file.

Source Definition. Which mandatory attribute must be defined on every single <img> tag to tell the browser engine exactly where to download the graphical asset from?

  • href
  • src
  • link
  • file

Mandatory Accessibility: alt Text. Web accessibility is absolutely non-negotiable. Every <img> tag must include an alt (Alternative Text) attribute. Screen readers read this description to visually impaired users, and it renders visibly if the image fundamentally fails to download.

Decorative Images. If an image is purely decorative (like a background swirl) and conveys absolutely no meaningful information, what is the technically correct way to handle its alt attribute for screen readers?

  • Omit it entirely
  • Empty string (alt="")
  • Text "none"

Preventing Cumulative Layout Shift. Cumulative Layout Shift (CLS) occurs when a downloading image brutally pushes text down the screen upon rendering. Professional developers actively prevent this by providing precise width and height attributes directly on the tag, allowing the browser to safely reserve the exact canvas area instantly.

Space Reservation. Which two attributes must be explicitly defined on an image element to reserve its layout space natively, perfectly eliminating Cumulative Layout Shift (CLS) before CSS styling applies?

  • x and y
  • size
  • width and height
  • columns and rows

Responsive Art Direction with Picture. Sometimes, scaling down a desktop image makes the subject too small. We utilize 'Art Direction' via the <picture> element. It wraps an <img> tag and utilizes multiple <source> tags. By writing CSS media queries inside the media attribute, the browser dynamically swaps the file.

Art Direction Query. Inside a <picture> element, which specific attribute on a nested <source> tag allows you to write a CSS query that tells the browser exactly when to load that specific asset variant?

  • query
  • media
  • screen
  • condition

Modern Formats: WebP and AVIF. Modern engineering offers formats like WebP and AVIF. They achieve vastly superior compression algorithms, providing smaller file sizes without noticeable visual degradation. Using <picture>, you serve these next-generation formats while automatically maintaining a .jpg fallback.

Introduction to Scalable Vectors (SVG). While JPGs are raster grids of fixed pixels, SVGs (Scalable Vector Graphics) are XML math. Because they are driven by mathematical formulas, SVGs scale infinitely without pixelation. Crucially, as raw code, you can inject them directly into HTML and modify colors dynamically via CSS.

Infinite Scalability. Which image format is structurally based on pure XML mathematical code rather than a grid of fixed pixels, allowing it to scale infinitely without ever losing crisp visual quality?

  • JPEG
  • PNG
  • SVG
  • GIF

Media Integration Mastered. Outstanding work! You mastered the architectural standards for integrating media. You understand strict accessibility with alt text, layout preservation with dimensions, art direction with <picture>, and next-gen formats like SVG. Next, we build Forms.

Prevent Layout Shift With Image Dimensions. Setting width and height lets the browser reserve space before the image loads.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1`alt` Text Should Describe Purpose, Not Just Appearance

For a photo of a graph, describing the visual ("a bar chart") is less useful than describing the takeaway ("bar chart showing Q3 revenue up 40% year over year") — write alt text for what a sighted user would actually learn from looking at it.

2Purely Decorative Images Need an Empty `alt=""`, Not a Missing Attribute

A missing `alt` attribute causes some screen readers to announce the full image filename or URL, which is worse than nothing. An explicit empty `alt=""` correctly tells assistive technology to skip the image entirely.

<img src="decorative-swirl.png" alt="">

SEO Implications

  • 1

    `alt` Text Is a Direct Image Search Ranking Signal

    Google Images relies heavily on `alt` text (alongside filename and surrounding context) to understand and rank images — missing or keyword-stuffed alt text either loses image search traffic entirely or risks being flagged as manipulative.

  • 2

    Unsized Images Are a Leading Cause of Poor Cumulative Layout Shift

    An `<img>` without explicit `width`/`height` attributes (or CSS aspect-ratio) reserves no space before it loads, causing surrounding content to jump once the image arrives — one of the most common real-world causes of a poor CLS score.

Best Practices

Always Set Explicit `width` and `height` Attributes

Even when the image is styled responsively with CSS, setting the intrinsic `width`/`height` attributes lets the browser reserve the correct aspect-ratio space immediately, preventing layout shift before the CSS or image data even arrives.

Use `loading="lazy"` for Below-the-Fold Images Only

Lazy-loading the hero image or anything visible on initial load actually delays it and can hurt Largest Contentful Paint — reserve `loading="lazy"` for images further down the page that aren't immediately visible.

Frequent Bugs

THE BUG

The page visibly jumps as images finish loading, especially on a slow connection.

THE FIX

The `<img>` tags are missing explicit `width`/`height` attributes, so the browser can't reserve their aspect-ratio space in advance. Add the intrinsic dimensions (even if CSS later overrides the display size) to eliminate the layout shift.

THE BUG

A screen reader announces a long, meaningless filename like 'IMG_4821.jpg' out loud.

THE FIX

The `alt` attribute is missing entirely. Even a purely decorative image needs an explicit empty `alt=""` — an absent attribute causes some assistive tech to fall back to announcing the raw filename instead of silently skipping it.

Real-World Examples

Layout-Stable, Accessible Product Image

An e-commerce product image ships with meaningful alt text, explicit dimensions to prevent layout shift, and lazy loading since it sits below the page's hero section.

<img src="chair.jpg" alt="Mid-century walnut dining chair, side profile" width="400" height="400" loading="lazy">

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Missing the 'alt' attribute

<!-- Wrong --> <img src="dog.jpg"> <!-- Correct --> <img src="dog.jpg" alt="A golden retriever playing in the park">

The Solution //

The 'alt' attribute is required. If the image is purely decorative, use an empty alt attribute (alt=""). Otherwise, describe the image for screen readers.

The Error //

Not specifying width and height

<!-- Wrong --> <img src="logo.png" alt="Company Logo"> <!-- Correct --> <img src="logo.png" alt="Company Logo" width="200" height="100">

The Solution //

Providing width and height attributes reserves space for the image before it loads, preventing Cumulative Layout Shift (CLS) on your page.

Lesson Glossary

[01]alt

Alternative text for screen readers.

Code Preview
alt

[02]WebP

Next-gen highly compressed format.

Code Preview
.webp

[03]SVG

Scalable Vector Graphics (Math XML).

Code Preview
<svg>

[04]picture

Container tag for responsive art direction.

Code Preview
<picture>

Continue Learning