MyInternships.in

HTML Attributes

HTML alt Attribute

The alt attribute provides alternative text that describes an image when it cannot be displayed or seen. It is essential for accessibility and search engines.


The alt Attribute

The alt attribute is used on the <img> element (and also on <area> and <input type="image">) to supply a text description of the image. This text is shown if the image fails to load, is read aloud by screen readers, and is used by search engines to understand the image.

Good alt text describes the meaning or purpose of the image in a short, clear phrase.

Syntax

ℹ️

Syntax: <img src="file.jpg" alt="description of the image">. The value is a short, meaningful text description of what the image conveys.

The basic pattern
Example
<img src="team.jpg" alt="Four interns collaborating around a laptop">

Example

Run the example. The first image loads normally; the second points to a missing file so you can see its alt text render in its place.

A working image and a broken one showing its alt text
Example
<!DOCTYPE html>
<html>
  <body>
    <p>
      <img src="https://picsum.photos/id/1043/220/140"
           alt="Sunlight streaming through a green forest" width="220">
    </p>
    <p>
      <img src="does-not-exist.jpg"
           alt="This image failed to load, so you are reading its alt text"
           width="220">
    </p>
  </body>
</html>

More Examples

Decorative image with empty alt

Empty alt tells screen readers to skip a decorative image
Example
<!DOCTYPE html>
<html>
  <body>
    <h2>Newsletter</h2>
    <img src="https://picsum.photos/id/1062/300/40" alt="" width="300">
    <p>Purely decorative dividers use alt="" so screen readers ignore them.</p>
  </body>
</html>

Attribute Values

ValueDescription
Descriptive textA concise description of the image's meaning or content
alt=""Empty alt marks a purely decorative image so screen readers skip it
(omitted)Discouraged; screen readers may read the file name instead
💡

For accessibility, describe the purpose of the image, not its appearance. Avoid phrases like "image of" or "picture of" since screen readers already announce that it is an image. Use empty alt="" only for decorative images that add no information.

Key Takeaways

  • alt gives images a text alternative for accessibility and SEO.
  • It is shown when an image fails to load.
  • Describe the image's purpose, not just its looks.
  • Use alt="" for purely decorative images.
  • Never omit alt on meaningful images.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships