Ora

What is Alternative Text in HTML?

Published in HTML Accessibility 4 mins read

Alternative text, often referred to as "alt text" or "alt attributes," is a brief, descriptive textual substitute for an image in HTML. It is specified using the alt attribute within the <img> tag and plays a crucial role in web accessibility, usability, and search engine optimization.

The alt attribute specifies an alternate text for an image if the image cannot be displayed. It provides alternative information for an image if a user for some reason cannot view it—whether because of a slow internet connection, an error in the src (source) attribute, or if the user is using a screen reader.

Why is Alt Text Important?

Alt text serves several vital purposes, ensuring that content remains accessible and informative for all users.

  • Accessibility for Screen Readers: For visually impaired users, screen readers read out the alt text aloud, describing the image content. This allows them to understand the visual information conveyed by the image, making the web more inclusive.
  • Broken Images: If an image fails to load due to a slow connection or a broken link (an error in the src attribute), the alt text is displayed in its place, giving users an idea of what the missing image was about.
  • Search Engine Optimization (SEO): Search engine crawlers cannot "see" images. They rely on alt text to understand an image's content and context. Well-written, descriptive alt text can help images rank in image search results and contribute to the overall SEO of a web page.
  • Bandwidth Conservation: In situations where users have limited bandwidth or have chosen to turn off image loading to save data, alt text provides essential context without downloading the image itself.

How to Implement Alt Text in HTML

Implementing alt text is straightforward and involves adding the alt attribute to your <img> tags.

Basic Syntax Example:

<img src="sunset-beach.jpg" alt="A vibrant orange and purple sunset over a calm ocean beach with a few palm trees">

In this example, if the sunset-beach.jpg image fails to load, the text "A vibrant orange and purple sunset over a calm ocean beach with a few palm trees" will appear instead. A screen reader would also convey this description to a user.

Best Practices for Writing Effective Alt Text

Crafting good alt text goes beyond simply describing an image; it requires thought to ensure it's informative and helpful.

  • Be Descriptive and Specific: Accurately describe the image's content and function. Instead of alt="Dog", use alt="Golden Retriever puppy playing with a red ball in a grassy park".
  • Keep it Concise: While descriptive, aim for brevity. Most screen readers will cut off after about 125 characters, so prioritize the most important information.
  • Avoid "Image of..." or "Picture of...": It's redundant. Screen readers already announce that it's an image.
  • Include Keywords (Naturally): If relevant, incorporate keywords related to your page's content, but do so naturally and without keyword stuffing. The primary goal is user experience.
  • Context is Key: Consider the surrounding content and the image's role on the page. Does it illustrate a point? Is it a link?
  • Decorative Images: For purely decorative images that convey no information (e.g., a background swirl, a separator line), use an empty alt attribute: alt="". This tells screen readers to skip over the image.

Alt Text Scenarios Table:

Image Type Example Image Good Alt Text Example Poor Alt Text Example
Informative Chart showing sales growth over 5 years. alt="Bar chart showing a 20% sales growth from 2019 to 2024" alt="Chart"
Functional Magnifying glass icon linking to a search page. alt="Search" alt="Magnifying glass"
Product Blue running shoes with white laces. alt="Pair of blue Nike running shoes with white laces and sole, viewed from side" alt="Shoes"
Decorative A subtle gradient background image. alt="" alt="Blue gradient background image for header"
Complex (Infographic) An infographic detailing solar panel installation steps. alt="Infographic: Step-by-step guide to installing solar panels" (with full text in a caption or linked resource) alt="Solar panels"

Further Reading and Resources

By diligently applying appropriate alt text, web developers ensure their content is robust, accessible, and performs well across all user agents and platforms.