# Image Performance & Inline SVG

> Three HTML attributes control how the browser fetches and decodes images. Inline SVG gives you a shape that CSS can style; an `<img>` pointing at an SVG gives you a picture you cannot touch.

Subject: HTML | Lesson 20 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/image-performance-and-svg
Previous: [Responsive Images](https://codewithanimation.in/html/responsive-images.md) | Next: [video, audio & Captions](https://codewithanimation.in/html/video-audio-and-captions.md)

## What it is

HTML provides three attributes that tune image loading without any JavaScript. `loading="lazy"` defers the fetch until the image is close to the viewport. `decoding="async"` lets the browser decode the image off the main thread. `fetchpriority="high"` tells the browser to promote this image in the fetch queue, useful for the largest image on the page.

SVG is a special case: an SVG file brought in as inline markup becomes part of the DOM, so CSS can color its paths and JavaScript can animate them. The same SVG loaded via `<img src="logo.svg">` is opaque, isolated from the page's styles, and treated just like a JPEG. Same file, two completely different capabilities.

## Why it matters

The single biggest image performance mistake is putting `loading="lazy"` on the hero image. The browser discovers lazy images only when they are about to enter the viewport, which delays the fetch by the time it takes to parse and render everything above the fold. For the above-fold image, that delay hurts Largest Contentful Paint directly. Lazy loading is a win for images below the fold and a penalty for the one the user sees first.

Format choice quietly affects every page load. WebP images are typically 25-35% smaller than JPEG at equivalent quality. AVIF goes further but has older browser gaps. The `<picture>` element lets you offer the modern format to browsers that support it and fall back to JPEG for those that do not, without any JavaScript.

## How it works

For the primary above-fold image, add `fetchpriority="high"` and omit `loading`. For every image below the fold, add `loading="lazy"`. `decoding="async"` is safe to add to any image; it is a hint that lets the browser handle decode scheduling more flexibly. None of these attributes change what the image looks like, only when and how the browser fetches and processes it.

For format switching, use `<picture>` with `<source>` elements. List AVIF first, then WebP, then JPEG as the `<img>` fallback. The browser picks the first source whose type it supports: `<source srcset="photo.avif" type="image/avif">` then `<source srcset="photo.webp" type="image/webp">` then `<img src="photo.jpg" alt="...">`. You still need `width`, `height`, and `alt` on the `<img>` regardless of which format is served.

For inline SVG, paste the SVG markup directly into the HTML. Target shapes with CSS using class names or the SVG element selectors (`circle`, `path`, `rect`). If you only need the SVG as a static image and do not need to style it, `<img src="icon.svg" alt="...">` is simpler and avoids bloating the HTML. The rule: use inline SVG when you need CSS or JS control, and `<img>` when you do not.

## Common mistake: loading="lazy" on the above-fold hero image

This is the most common image performance mistake. `loading="lazy"` delays the browser's fetch until the element is near the viewport. For the image the user sees the moment the page loads, there is no distance to close, so the delay hurts LCP with no benefit. Reserve lazy loading for images that are not visible on first paint. A quick rule: if the image is visible without scrolling on any common screen size, do not lazy load it.

## Tip: Use <picture> to serve modern formats without breaking old browsers

AVIF and WebP are not supported everywhere, but `<picture>` lets you offer them without guessing. The browser tests each `<source>` type in order and falls back to `<img>` if none match. Your build pipeline generates the extra files; the HTML wires them up. The fallback `<img>` ensures the image loads on every browser, every time.

## Try it: Try it

See a lazy image fade in as it enters the viewport, compare inline SVG vs `<img>` CSS control, and watch the `<picture>` source selection.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/image-performance-and-svg

## Check yourself

### Question 1

The hero image at the top of a landing page is the largest element on screen. Which attributes should it have?

- A. fetchpriority="high" and no loading attribute
- B. loading="lazy" to defer it until the page is ready
- C. decoding="sync" to block the page until it loads

Correct answer: A

`fetchpriority="high"` tells the browser to prioritize this fetch in the queue. `loading="lazy"` on an above-fold image delays the fetch and directly hurts LCP. Use lazy loading only below the fold.

- Why not B: Lazy loading the above-fold hero delays its fetch and hurts LCP. The browser needs to fetch it immediately. Use fetchpriority="high" instead.
- Why not C: decoding="sync" blocks the main thread during decode. decoding="async" is the better choice, and it still does not make the image load faster by itself.

### Question 2

You need to change an icon's color on hover using CSS. Should you use inline SVG or <img src="icon.svg">?

- A. Inline SVG, because it becomes part of the DOM and CSS can target its paths
- B. <img src="icon.svg">, because SVG images render the same either way
- C. Either one; both support CSS fill changes

Correct answer: A

Inline SVG joins the DOM, so CSS selectors and `fill` / `stroke` properties reach its internal elements. `<img src="icon.svg">` treats the file as an image blob: visually identical but unreachable by stylesheets.

- Why not B: An SVG loaded via <img> is isolated from the page's CSS. You cannot change its internal path colors with a stylesheet. For CSS control you need inline SVG.
- Why not C: Only inline SVG is part of the DOM. An <img> pointing at an SVG is opaque to CSS, the same as a JPEG.

### Question 3

You have photo.avif, photo.webp, and photo.jpg. How do you offer all three with automatic fallback?

- A. <picture> with <source type="image/avif">, <source type="image/webp">, and <img src="photo.jpg">
- B. Three <img> elements; the browser picks the supported one
- C. CSS background-image with format checks in a media query

Correct answer: A

`<picture>` evaluates `<source>` elements in order and picks the first supported type. The `<img>` at the end is the required fallback and the element that carries `alt`, `width`, and `height`.

- Why not B: Browsers load every <img> they encounter. There is no automatic selection between multiple <img> elements. <picture> with <source> is the correct pattern.
- Why not C: CSS background images have no semantic meaning (no alt text) and cannot use the <picture> fallback chain. Use <picture> for content images that need format switching.

## Remember this

- Never use `loading="lazy"` on the above-fold hero; use `fetchpriority="high"` instead.
- `loading="lazy"` defers below-fold image fetches; `decoding="async"` frees the main thread during decode.
- Inline SVG is part of the DOM and styleable with CSS; `<img src=".svg">` is opaque to stylesheets.
- Use `<picture>` with ordered `<source type="...">` elements to serve AVIF or WebP with a JPEG fallback.
