What is the alt attribute?

The alt attribute on <img> (and <area>, <input type="image">) provides a text alternative for an image. Screen readers read it aloud, browsers show it if the image fails to load, and search engines use it to understand images. Every image needs an alt, set to an empty string (alt="") when the image is purely decorative.

How to write good alt text

  • Informative images: describe the information or purpose briefly: alt="Bar chart: sales grew 40% in Q3".
  • Functional images (inside links or buttons): describe the action or destination, not the picture: alt="Search", alt="DevQnA home".
  • Decorative images: alt="" so assistive technology skips them. Better still, use CSS backgrounds for pure decoration.
  • Images of text: repeat the text.
  • Complex images (charts, diagrams): a short alt plus a longer description nearby or via <figure> and <figcaption>.

Avoid

  • "Image of..." or "Picture of..." (screen readers already announce "image").
  • File names like IMG_2048.jpg.
  • Keyword stuffing for SEO.
  • Omitting alt entirely: screen readers may read the file name instead.
  • Very long alt text; aim for one or two sentences.

Standards

WCAG 1.1.1 (Non-text Content) requires text alternatives for non-text content. Automated tools such as axe and Lighthouse flag missing alt attributes.

Context matters

The same photo may need different alt text on different pages, depending on why it is there.

Code example

html
1<style>body { font-family: system-ui; } img { vertical-align: middle; }</style>
2
3<!-- Informative: conveys the data -->
4<figure>
5 <img src="https://picsum.photos/id/180/320/160" width="320" height="160"
6 alt="Laptop on a desk showing a sales dashboard">
7 <figcaption>Our new analytics dashboard</figcaption>
8</figure>
9
10<!-- Functional: describes the destination, not the picture -->
11<a href="/">
12 <img src="https://picsum.photos/id/1/40/40" width="40" height="40" alt="DevQnA home">
13</a>
14
15<!-- Decorative: empty alt, skipped by screen readers -->
16<img src="https://picsum.photos/id/1060/320/20" width="320" height="20" alt="">
17
18<!-- Broken image: the alt text is displayed instead -->
19<p><img src="/does-not-exist.png" width="200" height="40" alt="Company logo (failed to load)"></p>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app