Sub topic 11
Images and the alt attribute
This chapter assumes you can write a basic HTML document, know the parts of an element, and understand attributes and void elements. It covers the img element, the attributes that matter most, and the errors that appear most often.
The img element
The img element embeds an image in a page. It is a void element, so it has no content and no end tag. Everything it needs goes in attributes.
<img src="cat.jpg" alt="A grey cat asleep on a sofa" width="400" height="300">The browser requests the file named in src, then draws it inline, in the flow of the surrounding text.
The essential attributes
srcis required. It holds the URL of the image file, relative or absolute, using the same rules as link URLs.altis the text alternative. It is required for valid HTML, apart from a few special cases.widthandheightgive the image's intrinsic size in pixels, as plain numbers without units.
Setting width and height lets the browser reserve space before the file arrives. Without them, the page content jumps when the image loads. This jump is called layout shift. The values should match the file's real proportions. CSS can resize the image later.
Writing good alt text
The alt text stands in for the image when it cannot be seen. Screen readers read it aloud. It is also shown if the file fails to load, and search engines use it.
Write alt text that gives the same information the image gives, in context.
| Image | Weak alt | Better alt |
|---|---|---|
| Photo of a chart showing sales rising | `chart` | `Bar chart: sales rose from 40 to 90 units in 2025` |
| Logo that links to the home page | `logo` | `Garden Notes home` |
| Photo that only decorates | `beautiful picture` | `` (empty) |
Some guidelines:
- Describe purpose, not appearance. If an image is a link, the alt text should say where the link goes.
- Do not begin with "image of" or "picture of". Screen readers already announce an image.
- Keep it short. If an image needs a long explanation, put the explanation in the page text.
- For purely decorative images, use an empty value:
alt="". Screen readers then skip the image.
<img src="divider.png" alt="" width="600" height="20">Common errors
- Missing
alt. The validator reports this as an error, and it harms accessibility. - A wrong path in
src. The browser shows a broken-image icon and the alt text. Check the file name, its capitalization and the folder. Open the developer tools Network panel to see a failed request, usually a 404. - Pixel units in attributes. Write
width="400", notwidth="400px". - Stretched images. If
widthandheightdo not match the file's proportions, the picture looks distorted. - Text inside an image. Words in a picture cannot be selected or read by assistive technology unless the alt text repeats them.
How the pieces fit
Putting it together
An image inside a paragraph stays inline. An image used as a link goes inside the a element, and its alt text names the destination.
<a href="/">
<img src="logo.png" alt="Garden Notes home" width="160" height="48">
</a>Exercise
A page shows a small flourish between two sections. It adds no information. What alt value should its img element have, and why not leave the attribute out?
Show answer
Use alt="". An empty value tells screen readers to skip the image. If the attribute is missing, some screen readers announce the file name instead, which adds noise.
Exercise
Fix this element and say what was wrong: <img src="Team.JPG" width="300px" alt="image of our team">
Show answer
Write <img src="Team.JPG" width="300" height="200" alt="The five members of the design team outside the office">. The problems were the px unit in width, the missing height, and alt text that began with "image of" and said nothing specific. Also check that the file name's capitalization matches the real file.