HTML alt Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Accessibility & Media

Introduction

The alt attribute is a fundamental feature in HTML that is primarily used with the <img> (image) element. The purpose of the alt attribute is to provide alternative text for browsers to display when the image cannot be loaded. This text also serves as a description for users who may be using screen readers or have images disabled.

What You’ll Learn

01

Alt Text Basics

Describe images.

02

Accessibility

Screen reader support.

03

Fallback Text

When images fail.

04

Decorative Images

Use alt=""

05

JavaScript

Set alt dynamically.

06

SEO

Search-friendly text.

Purpose of alt

The primary purpose of the alt attribute is to enhance web accessibility by ensuring that users with disabilities or those facing slow internet connections can still understand the content conveyed by images on a webpage. Additionally, it serves as a placeholder when the image itself cannot be rendered.

💡
Required on img

Every meaningful <img> should have an alt attribute. It is not optional for accessible, standards-compliant HTML.

📝 Syntax

Add alt to an <img> element with a descriptive string (or empty for decorative images):

alt.html
<img src="photo.jpg" alt="A scenic view of a mountain landscape">

Syntax Rules

  • Required on <img> elements (use alt="" if decorative).
  • Value is a plain text string describing the image content or function.
  • Also valid on <area> elements in image maps.
  • Do not include "image of" or "picture of"—screen readers already announce it as an image.

💎 Values

The alt attribute accepts a string value that represents the alternative text for the image. This text should be descriptive enough to convey the content and purpose of the image. Proper use of the alt attribute is crucial for making websites more accessible and user-friendly.

  • Descriptive textalt="A scenic view of a mountain landscape" explains what the image shows.
  • Functional textalt="Search" on an icon button describes what the control does.
  • Empty stringalt="" marks a purely decorative image that assistive tech should ignore.
alt-values.html
<!-- Informative image -->
<img src="chart.png" alt="Sales grew 24% in Q2 2026">

<!-- Decorative image -->
<img src="divider.svg" alt="">

<!-- Image map area -->
<area shape="rect" coords="0,0,100,100" href="/about" alt="About us">

⚡ Quick Reference

Image Typealt ValueNotes
Informative photoDescribe the contentWhat would you say if you couldn’t see it?
Linked imageDescribe the link destinationSame as link text purpose
Icon buttonAction namee.g. alt="Close"
Decorativealt=""No meaningful content
Complex chartSummary + long descriptionLink to detailed text nearby
Applicable elementsimg, areaPrimary use on images

Applicable Elements

ElementSupported?Notes
<img>Yes (required)Primary and most common use
<area>YesClickable regions in image maps
<input type="image">YesSubmit button styled as image
<video>, <audio>NoUse captions or transcripts instead

Examples Gallery

Descriptive alt text, dynamic JavaScript updates, and decorative images with empty alt.

👀 Live Preview

An image with descriptive alt text (inspect or disable images to see the fallback):

alt="A scenic view of a mountain landscape"

Example

Let’s consider a simple example of using the alt attribute with an <img> element:

alt.html
<img src="example.jpg" alt="A scenic view of a mountain landscape">
Try It Yourself

How It Works

In this example, the alt attribute is set to “A scenic view of a mountain landscape,” providing a textual description of the image content. If the image fails to load, this text appears in its place.

Dynamic Values with JavaScript

You can dynamically set the alt attribute using JavaScript. This can be useful when dealing with changing content or user interactions. Here’s a brief example:

dynamic-alt.html
<img id="dynamicImage" src="photo.jpg" alt="">

<script>
  document.getElementById("dynamicImage").alt = "Sweet red cherry fruit";
</script>
Try It Yourself

How It Works

In this script, the alt attribute for an image with the id dynamicImage is set to “Sweet red cherry fruit.” This dynamic approach can be helpful when the content of the image changes based on user actions.

Example — Decorative Image

If an image is purely decorative and doesn’t convey meaningful content, use an empty alt attribute:

decorative-alt.html
<img src="divider.svg" alt="" role="presentation">
Try It Yourself

How It Works

An empty alt="" tells assistive technologies to skip the image. Use this only when the image adds no information beyond what surrounding text already provides.

♿ Accessibility

  • Every img needs alt — Use descriptive text or alt="" for decorative images; never omit the attribute.
  • Be concise but meaningful — Convey the image’s purpose, not every visual detail.
  • Don’t repeat nearby text — If a caption already describes the image, keep alt brief or use alt="".
  • Test with a screen reader — Verify alt text reads naturally and adds value.
  • Complex images need more — Provide a longer description in visible text or via aria-describedby.

🧠 How alt Works

1

Author adds alt to img

Write descriptive text or leave empty for decorative images.

Markup
2

Browser loads image

If the image loads, alt is used by assistive technology.

Render
3

Fallback if image fails

Broken or blocked images show alt text in the page.

Fallback
=

Inclusive content for all users

Everyone understands your images—visually, technically, or via assistive tech.

Browser Support

The alt attribute is supported in all browsers and has been part of HTML since the earliest image support.

HTML2+ · Fully supported

Universal image accessibility

All major browsers honor alt on img and area elements.

100% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer All versions
Full support
Opera Fully supported
Full support
alt attribute 100% supported

Bottom line: Use alt on every img—it works everywhere and is essential for accessibility.

💡 Best Practices

✅ Do

  • Always include alt on every <img> element
  • Make alt text descriptive and concise
  • Use alt="" for purely decorative images
  • Describe the image’s function if it is a link or button
  • Test with images disabled and with screen readers

❌ Don’t

  • Omit the alt attribute entirely
  • Start with “Image of…” or “Picture of…”
  • Keyword-stuff alt text for SEO
  • Duplicate long captions word-for-word in alt
  • Use filename as alt (e.g. alt="IMG_4521.jpg")

Conclusion

The alt attribute is a crucial element for creating accessible and inclusive web content. By using it appropriately, you contribute to a better user experience for all visitors, regardless of their abilities or technical constraints.

Good alt text helps screen reader users, improves fallback when images fail to load, and gives search engines meaningful context about your visual content.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about alt

Bookmark these before adding your next image.

5
Core concepts
02

Accessibility

Screen readers.

A11y
🔍 03

SEO

Search context.

SEO
⚙️ 04

JavaScript

image.alt

Dynamic
🚫 05

alt=""

Decorative only.

Empty

❓ Frequently Asked Questions

It provides alternative text for images. Browsers show it when images fail to load; screen readers announce it to users.
Yes. HTML requires an alt attribute on img elements. Use descriptive text or alt="" for decorative images.
Only for purely decorative images that add no information. Assistive technologies will skip them.
Yes. Descriptive alt text helps search engines understand image content. Write for users first, not keywords.
Yes. Assign imageElement.alt = "description" when image content changes dynamically.
Yes. Universal support on img and area elements across all browsers.

Write better alt text today

Practice the alt attribute with descriptive, dynamic, and decorative examples in the Try It editor.

Try descriptive alt example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful