# The img Element & Alt Text

> `<img>` is a void element that needs two attributes to work correctly: `src` for the image and `alt` for everything else. Getting `alt` right is what separates an accessible image from an invisible one.

Subject: HTML | Lesson 17 of 42 | 5 min read
Interactive lesson: https://codewithanimation.in/html/img-and-alt
Previous: [New Tabs, rel & mailto/tel](https://codewithanimation.in/html/target-rel-and-protocols.md) | Next: [width, height & Layout Shift](https://codewithanimation.in/html/width-height-and-cls.md)

## What it is

`<img>` is a void element: it has no closing tag and embeds an image into the document at the point it appears. The `src` attribute points to the image file; the `alt` attribute provides a text alternative. Both are required for a meaningful image, though the rules for `alt` depend on what the image communicates.

When an image fails to load, the browser typically shows a broken-image icon alongside the `alt` text. When a screen reader encounters an `<img>`, it announces the `alt` value in place of the image. Both of these make `alt` a functional part of the content, not an optional annotation.

## Why it matters

A screen reader user cannot see the image. The `alt` text is the entire experience of that image for them. A vague `alt="image"` or a missing `alt` forces them to skip content that sighted users receive naturally. Well-written alt text delivers the same information the image would deliver to someone who can see it.

Alt text also appears in place of images when they fail to load: slow connections, corporate firewalls that block image domains, or a typo in the `src` path all produce broken images. A page whose images all carry meaningful alt text still communicates when its images do not render.

## How it works

For meaningful images, write alt text that describes what the image communicates in context. Skip the redundant prefix: do not write `alt="photo of a puppy"`, write `alt="a golden retriever puppy running on a beach"`. The screen reader already announces it as an image; you do not need to say so again. Focus on what information a sighted reader gets from the image, and write that.

For decorative images, set `alt=""` (an empty string). This tells assistive technology to skip the element entirely. Do not omit the `alt` attribute: without it, some screen readers fall back to reading the `src` filename aloud, which is almost always meaningless noise. The distinction between `alt=""` and no `alt` attribute is subtle in markup but significant in experience.

For images inside links, the `alt` text must describe the link destination, not the image. If a logo links to the home page, `alt="Home"` or `alt="Example Corp home page"` is correct. The logo's visual appearance is irrelevant; what matters is where the link goes.

Add `loading="lazy"` to any `<img>` below the fold. The browser will defer fetching the image until the user scrolls near it, saving bandwidth on initial load. Never put `loading="lazy"` on the largest image visible on page load (the LCP image); that image should load as early as possible.

## Common mistake: Omitting alt entirely instead of using alt=""

A missing `alt` attribute and `alt=""` look similar in source but behave differently. An `<img>` with no `alt` is, by the HTML spec, an image whose purpose is unknown. Screen readers handle this by reading the filename from `src`, which produces output like "puppy underscore park underscore 2026 dot jpg", uninformative at best, disruptive at worst. An `<img alt="">` explicitly marks the image as decorative and skips it cleanly. For decorative images, always include the attribute, just leave it empty.

## Tip: Describe what the image communicates, not what it shows

A bar chart and a photo require different alt strategies. The photo might need `alt="Volunteers assembling food boxes at the distribution center"`. The chart needs the data it encodes: `alt="Bar chart showing revenue grew from 1.2M in 2024 to 1.8M in 2026"`. In both cases the question is: what would a sighted reader take away from this? Write that. If the image is pure decoration, no information is lost by leaving `alt=""`.

## Try it: Try it

See what happens when an image loads successfully versus when the `src` fails, and how the `alt` attribute behaves differently for meaningful and decorative images.

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

## Check yourself

### Question 1

You have a purely decorative divider image. How should you mark it up?

- A. <img src="divider.svg" alt="">
- B. <img src="divider.svg"> (no alt attribute)
- C. <img src="divider.svg" alt="decorative divider">

Correct answer: A

Decorative images should have `alt=""` (empty string), not a missing `alt` attribute and not a description. Empty `alt` is the explicit signal that the image carries no information and should be skipped by assistive technology.

- Why not B: Without an alt attribute, some screen readers read the filename aloud. An empty alt="" explicitly marks the image as decorative and tells AT to skip it.
- Why not C: Describing a decorative image adds noise for screen reader users. If the image carries no information, alt="" is correct: it tells AT to skip the element entirely.

### Question 2

A company logo is wrapped in an <a> that links to the home page. What is the correct alt text?

- A. alt="Acme Corp home page"
- B. alt="Acme Corp logo"
- C. alt="" (the link text will describe it)

Correct answer: A

When `<img>` is the only content of an `<a>`, the image's `alt` becomes the link's accessible name. Describe the link destination (`alt="Acme Corp home page"`), not the image's visual appearance.

- Why not B: When an image is inside a link, the alt text is the accessible name of the link. It should describe the destination, not the image's visual appearance.
- Why not C: If the image is the only content of the link, an empty alt leaves the link with no accessible name. Screen readers would announce it as "link" with no destination information.

### Question 3

Which alt text is best for a photo showing staff working in an open office?

- A. alt="Staff collaborating at standing desks in the London office"
- B. alt="photo of office"
- C. alt="image"

Correct answer: A

Good alt text describes what the image communicates in context. Skip the "photo of" prefix (redundant) and focus on the specific information: who, where, and what is happening.

- Why not B: "Photo of" is redundant (screen readers already announce images as images) and the description gives almost no useful information.
- Why not C: This tells the user nothing about what the image shows. The alt should convey the same information a sighted user gets from the photo.

## Remember this

- `<img>` is a void element; it needs `src` (file) and `alt` (text alternative) for meaningful images.
- Decorative images take `alt=""` (empty string); omitting `alt` entirely causes screen readers to read the filename.
- Alt text describes what the image *communicates*, not what it *shows*; skip the "photo of" prefix.
- For images inside links, `alt` must describe the link destination, not the image's appearance.
