How to Write Good Alt Text for Images
Writing useful alt text for screen readers, search engines and everyone else.
When adding images to a page it's easy to skip the alt attribute or fill it
with the file name. Someone using a screen reader gets the alt text in place of
the image. If it's missing, they hear the file name read out loud, which isn't
very helpful.
Alt text describes what an image is there to do, for anyone who can't see it.
The Basics
The alt attribute goes right on the <img> tag:
<img src="/images/team.jpg" alt="Our team of six standing outside the office" />
Screen readers read this text in place of the image. It also shows up when an image fails to load, and search engines use it to understand what the image is about.
Describe What the Image Is For
Ask yourself what someone would miss if the image wasn't there. The same photo can need different alt text depending on why it's on the page:
<!-- On a recipe page --><img src="/images/cake.jpg" alt="A slice of chocolate cake with raspberries" /><!-- On a bakery's contact page --><img src="/images/cake.jpg" alt="Our storefront on Main Street" />
Keep it short, usually a sentence or less. If an image needs a long explanation, like a chart, put that explanation in the text on the page.
Skip "Image Of"
Screen readers already announce that it's an image, so starting with "image of" or "picture of" just repeats it:
<!-- Not great --><img src="/images/dog.jpg" alt="Image of a dog" /><!-- Better --><img src="/images/dog.jpg" alt="A golden retriever catching a frisbee" />
The exception is when the type of image matters, like "Screenshot of the settings page" or "Illustration of a rocket".
Use an Empty alt for Decorative Images
Some images are only there for looks, like a background pattern or an icon next
to text that already says the same thing. Give these an empty alt and
aria-hidden="true":
<img src="/images/divider.svg" alt="" aria-hidden="true" />
An empty alt tells screen readers to skip the image, and aria-hidden="true"
hides it from assistive technology completely, so it doesn't show up in the
accessibility tree at all. Leaving alt off entirely is different. Many screen
readers will read the file name instead.
The same goes for decorative inline SVG icons, which don't have an alt
attribute:
<svg aria-hidden="true" focusable="false">...</svg>
focusable="false" stops Internet Explorer from adding the SVG to the tab
order.
Images Inside Links and Buttons
When an image is the only thing inside a link or button, the alt text becomes the label. Describe where it goes or what it does, not what it looks like:
<a href="/"><img src="/images/logo.svg" alt="Jools.dev home" /></a><button><img src="/images/search.svg" alt="Search" /></button>
"Magnifying glass" tells someone what the icon looks like. "Search" tells them what it does.
Don't Stuff Keywords
It can be tempting to load alt text with keywords for SEO. Search engines can tell, and it makes the page worse for screen reader users:
<!-- Don't do this --><img src="/images/shoes.jpg" alt="shoes running shoes best shoes cheap shoes" /><!-- Do this --><img src="/images/shoes.jpg" alt="Blue running shoes with white soles" />
Alt Text in React
JSX uses the same alt attribute. It's also a good habit to make it a required
part of any image data you pass around:
const ProductImage = ({ product }) => (<img src={product.imageUrl} alt={product.imageAlt} />)
If you use create-react-app, the built-in eslint-plugin-jsx-a11y rules will
warn you when an <img> is missing its alt.
A good test is to read the page out loud with the alt text in place of each image. If it still makes sense, you're in good shape.
Have some feedback on this post? .