To add an alt tag to an image in HTML, use the alt attribute inside the <img> element. The alt attribute provides a text alternative that describes the image or explains its purpose when the image cannot be seen.
The basic syntax is:
<img src="image.jpg" alt="Description of the image">
For example:
<img src="laptop.jpg" alt="Black laptop on a wooden desk">
Here, src specifies the image file, while alt provides a meaningful text description.
What Is an Alt Tag in HTML?
The term “alt tag” commonly refers to the HTML alt attribute. Technically, it is not a separate HTML tag. It is an attribute of the <img> element.
The alt attribute is important for accessibility because screen readers can read the alternative text to users who cannot see the image. It also provides useful context if an image fails to load.
A correctly written image element looks like this:
<img src="team.jpg" alt="Web development team discussing a project">
The alternative text should describe what is important about the image in the context of the page.
How to Write Effective Alt Text
Good alt text should be concise, specific, and relevant. Avoid describing every visual detail unless those details are necessary to understand the image.
For example, instead of:
<img src="dog.jpg" alt="Dog">
you could use:
<img src="dog.jpg" alt="Golden retriever sitting in a garden">
The second example gives users more useful information without becoming unnecessarily long.
Do not begin alt text with phrases such as “image of” or “picture of” in normal situations. A screen reader already identifies the element as an image, so this wording usually adds unnecessary repetition.
Add Alt Text to Decorative Images
Not every image needs a description. If an image is purely decorative and does not provide meaningful information, use an empty alt attribute:
<img src="decorative-line.png" alt="">
The empty value tells assistive technology that the image can be ignored.
This is different from leaving the alt attribute out completely. For example:
<img src="decorative-line.png">
A missing alt attribute can cause accessibility problems because assistive technologies may handle the image differently depending on the environment.
Alt Text for Functional Images
When an image acts as a link or button, the alt text should describe its function rather than simply describe its appearance.
For example:
<a href="/search">
<img src="search-icon.png" alt="Search">
</a>
The purpose of the image is to take the user to the search function, so “Search” is more appropriate than “Magnifying glass icon.”
Another example:
<button type="button">
<img src="close.png" alt="Close">
</button>
Here, the alt text communicates the button’s action.
Alt Text for Images Containing Text
When important text appears inside an image, the alt text should communicate that text when it is necessary for understanding the page.
For example:
<img src="sale-banner.jpg" alt="50% off all summer products">
However, important information should generally be provided as real HTML text instead of being embedded only in an image. HTML text is easier to access, search, resize, and maintain.
Do Not Use Keyword Stuffing in Alt Text
Alt text should describe the image naturally. Do not use it as a place to insert a list of SEO keywords.
For example, this is poor practice:
<img src="shoes.jpg" alt="shoes buy shoes cheap shoes online best shoes running shoes">
A better version is:
<img src="shoes.jpg" alt="Black running shoes on a white background">
Search engines can use image context, but alt text should primarily serve as a useful text alternative for the image.
Alt Attribute vs. Title Attribute
The alt attribute and title attribute serve different purposes.
Use alt to provide an alternative for the image:
<img src="office.jpg" alt="Employees working in a modern office">
A title attribute may provide additional information, but it should not be used as a replacement for alt text:
<img src="office.jpg" alt="Employees working in a modern office" title="Company headquarters">
An image should still have appropriate alt text even when a title attribute is present.
Complete HTML Example
Here is a practical example showing several common cases:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Images</title>
</head>
<body>
<h1>Our Services</h1>
<img
src="web-design.jpg"
alt="Designer creating a website layout on a desktop computer"
>
<img
src="decorative-border.png"
alt=""
>
<a href="/contact">
<img src="contact-icon.png" alt="Contact us">
</a>
</body>
</html>
The first image contains meaningful information, so it has descriptive alt text. The second is decorative, so its alt value is empty. The third image functions as a link, so its alt text describes the link’s purpose.
How to Add Alt Tags to Existing HTML Images
If your HTML already contains images, find each <img> element and add the alt attribute.
Before:
<img src="product.jpg">
After:
<img src="product.jpg" alt="Blue ceramic coffee mug">
For decorative images:
<img src="background-shape.png" alt="">
Check important images individually and choose alt text based on their purpose and surrounding content.
Key Rule for HTML Image Alt Text
The correct approach is simple: use the alt attribute on every meaningful <img> element, describe the image’s relevant content or function concisely, and use alt="" for purely decorative images.
The goal is not to write the longest description. The goal is to provide the same essential information or function that a user would receive from seeing the image.
Explore More Blogs
How To Insert A Horizontal Line In Html
What Is The Correct Html For Adding A Background Colour
How To Convert Html To Powerpoint
How To Convert Ipynb To Html
How To Link An Email In Html
How To Open A Link In A New Tab Html
How To Upload Html File To WordPress
How To Add Schema Markup To Html
How To Call A Function In Javascript From Html
How To Center An Iframe In Html


