Skip to main content

HTML Image src and alt Attributes

Two small attributes decide whether an image works at all: src tells the browser where the picture is, and alt tells everyone what it is when they cannot see it.

One is about the machine, the other is about people. Both are easy to get wrong, and both are worth getting right. A wrong src gives your visitors a broken icon. A missing or lazy alt shuts out anyone who uses a screen reader, and it quietly hurts your page in search results too. This lesson makes both attributes simple.


What You Will Learn​

  • How src works, with relative, root-relative, and absolute paths
  • How to name and organize image files
  • What alt text is, and who relies on it
  • How to write great alt text for photos, icons, charts, logos, and linked images
  • When to use an empty alt
  • How alt works in different languages
  • Common mistakes, and how to test your images

Part 1: The src Attribute​

src stands for source. Its value is the address of the image file. It follows the same rules as any other URL, so everything you learned in Relative URLs and Absolute URLs applies here.

index.html
<img src="images/harbor.jpg" alt="Fishing boats at sunrise" width="240" height="160" />

The demo uses a small drawing built into the page. In your project, src points to a real file.

Four Ways to Write a Path​

Imagine this project.

my-website/
├── index.html
├── images/
│ ├── logo.svg
│ └── team/
│ └── photo.jpg
└── blog/
└── first-post.html
KindExampleWhere it is measured from
Relative to the pageimages/logo.svgThe folder of the current page
Going up a folder../images/logo.svgOne folder above the current page
Root-relative/images/logo.svgThe root of the website
Absolutehttps://example.com/images/logo.svgNowhere. It is complete

In index.html, you write images/team/photo.jpg. In blog/first-post.html, the same file is ../images/team/photo.jpg.

Pick one habit and keep it

Relative paths work everywhere, including on your own computer. Root-relative paths are tidy but need the site to live at the top of a domain. Absolute paths are for images hosted somewhere else, such as a content delivery network (CDN) you control.

Name Your Files Well​

A good file name helps you, your teammates, and search engines.

WeakBetterWhy
IMG_4521.JPGstudents-coding-workshop.jpgSays what the picture shows
Final Logo (2).pngcodeharborhub-logo.pngNo spaces, brackets, or "final"
Photo.JPGphoto.jpgLowercase avoids case problems on servers
café-münchen.jpgcafe-munchen.jpgPlain letters work everywhere

Rules to follow:

  1. Use lowercase letters, numbers, and hyphens.
  2. Describe the content, with a few words.
  3. Avoid spaces and special characters. If a space is unavoidable, it must be written as %20 in the URL.
  4. Keep the extension honest. A .jpg should be a real JPEG.

Images From Another Site​

You can point src to a picture on another website.

<img src="https://images.example.com/photos/sunrise.jpg" alt="Sunrise over a harbor" width="600" height="400" />

Do this only with permission, or for files that a service clearly offers for embedding. Otherwise you use their bandwidth, the file may change or vanish, and you may break copyright rules. Always prefer a copy that you control. Use https so browsers do not block the image on a secure page.

Data URIs: Images Inside the Code​

A data: URI places the whole image inside the src text. It needs no separate file, but it makes the HTML long. It is useful only for very small images, such as the tiny drawings in this tutorial.

<img
src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'><circle cx='5' cy='5' r='4' fill='%232563eb'/></svg>"
alt=""
width="24"
height="24"
/>

For anything bigger, use a normal file, so the browser can cache it.

Part 2: The alt Attribute​

alt stands for alternative text. It is a short description that stands in for the image when the picture cannot be seen or shown.

Who Uses alt Text?​

WhoHow they meet alt
Screen reader usersTheir software reads the alt aloud
People on slow or failed connectionsThe browser shows alt while the image loads, or if it fails
People who turned images offTo save data, or for focus
Search enginesThey read alt to understand and index the image
Text-only and reader-mode toolsThey show alt instead of the picture
Voice assistants and translation toolsThey use alt to describe a page

Roughly speaking, alt is not an extra. It is the version of the image for everyone who cannot see it.

The Golden Rule​

Write the alt text that would give the visitor the same information or the same function as the image.

Ask: what is this image doing here?

  • Is it giving information? Say that information.
  • Is it a link or button? Say where it goes or what it does.
  • Is it decoration? Leave alt empty.

How to Write Good alt Text​

1. Be Specific and Concise​

Describe what matters, in a short phrase or sentence. Aim for around 125 characters or fewer as a guide, not a strict rule.

<img src="boats.jpg" alt="boats" />
<img src="boats.jpg" alt="image" />
<img src="boats.jpg" alt="boats.jpg" />
<img src="boats.jpg" alt="harbor sunrise boats water sea fishing photo best cheap" />

2. Do Not Start With "Image of" or "Picture of"​

Screen readers already announce "image" or "graphic." Starting with those words repeats it.

Instead ofWrite
"Image of a girl reading a book""A girl reading a book under a tree"
"Photo of our office""Our office, with a long table and large windows"

3. Match the Image to the Context​

The same picture can need different alt text on different pages.

PagePicture of a lighthouseGood alt
Travel guideA lighthouse at dusk"A white lighthouse glowing at dusk on a rocky coast"
Article on architectureThe same photo"The lighthouse's spiral stone tower, built in 1850"
Company logo areaA lighthouse logo"Beacon Books home"

4. Do Not Stuff Keywords​

Search engines recognize it, visitors find it annoying, and screen reader users have to listen to it. Write for people.

5. Use Normal Sentences and Punctuation​

A final period helps screen readers pause naturally, and it is optional. What matters is that the text reads smoothly aloud.

Writing alt for Different Kinds of Images​

Type of imageWhat to writeExample
Photo that supports an articleWhat is shown, and why it mattersalt="Students on a video call, sharing their code on a screen"
LogoThe organization's name, and where it goes if linkedalt="CodeHarborHub" or alt="CodeHarborHub home"
Icon with a label next to itEmpty, since the label already says italt=""
Icon alone, in a button or linkThe action or destinationalt="Search", alt="Download the PDF"
Decorative divider or background shapeEmptyalt=""
Photo of a personWho, and what they are doing, if it mattersalt="Amara Okafor, course instructor, smiling at a whiteboard"
Chart or graphThe main conclusion, and give the data elsewherealt="Bar chart: course sign-ups doubled between March and June"
ScreenshotWhat it shows, and the key detailsalt="Browser window showing the page with a blue Sign Up button"
Image of textThe same wordsalt="Sale ends Sunday"
MapWhere, and what the reader needs from italt="Map: the school is two blocks north of the train station"

Linked Images​

When an image is the only thing inside a link, the alt text is the link text. Describe the destination.

<a href="/">
<img src="images/logo.svg" alt="CodeHarborHub home" width="140" height="40" />
</a>

Charts and Complex Images​

A short alt cannot hold all the data in a chart. Give a brief conclusion in alt, and put the full information next to the image as text, in a table, or in a longer description.

<figure>
<img
src="images/signups.png"
alt="Bar chart: course sign-ups doubled between March and June"
width="600"
height="360"
/>
<figcaption>
Sign-ups grew from 120 in March to 250 in June.
<a href="#signups-table">See the full data table.</a>
</figcaption>
</figure>

You will see how <figure> and <figcaption> work in Figure and Figcaption, and go deeper on these cases in Accessible Images.

Part 3: The Empty alt​

Some images do not add anything for a person who cannot see them. Examples are decorative lines, background swirls, and icons that repeat the text next to them. For these, write an empty alt.

<img src="images/divider.svg" alt="" width="300" height="12" />

A screen reader skips an image with alt="". That is exactly what you want.

Empty vs Missing: A Big Difference​

What you writeWhat a screen reader may do
alt=""Skips the image completely. Correct for decoration
alt left out entirelyMay read the file name, such as "IMG underscore 4521 dot jpg," or announce "image" with no explanation
alt="decorative image"Reads those words aloud, which is just noise
Never leave alt out

An <img> without alt is invalid HTML, and it is a real barrier. If the image is decorative, write alt="". If it is meaningful, write a description.

Is It Decoration? Test It​

Ask: if I remove this image, does the page lose any information or function?

  • If no, it is decoration. Use alt="", or put it in CSS as a background.
  • If yes, describe what would be lost.

alt, Captions, and title​

These three are different tools.

altCaption (<figcaption>)title
Seen byMostly people who cannot see the imageEveryoneMouse users on hover
PurposeA replacement for the imageExtra information about the imageA tooltip
Required?AlwaysOptionalOptional, use rarely

Do not copy the same text into both alt and the caption. If a caption already describes the image well, the alt can be shorter or focus on what the caption leaves out.

alt for a Global Audience​

  • Write alt in the language of the page. A Hindi page needs Hindi alt text, so Hindi screen readers pronounce it properly.
  • Mark text in another language with lang on the image, for example <img ... alt="Bonjour" lang="fr" />.
  • Avoid slang and idioms, which translate badly in automatic tools.
  • Use plain, clear words. Many visitors read in their second or third language.
  • Spell out names with their usual spelling, and avoid abbreviations people may not know.
  • Do not hide key information only in the picture. Translation tools cannot translate text inside an image.
<img src="images/library.jpg" alt="Students reading quietly in the school library" />

Testing Your Images​

Do not guess. Check.

  1. Turn off images in your browser settings, or break a src on purpose. Does the page still make sense with only the alt text?
  2. Use a screen reader for a minute: VoiceOver on Apple devices, TalkBack on Android, or a free one such as NVDA on Windows.
  3. Run an accessibility checker. Browser developer tools and extensions flag missing alt.
  4. Read all your alt text in a row. Do the descriptions sound useful, or repetitive?
  5. Ask someone else what they understand from the alt alone.

Common Mistakes​

Mistake 1: A wrong path​

A typo, a wrong folder, or a capital letter breaks the image. Check against your folder map.

Mistake 2: Missing alt​

Always include it, even if empty.

Mistake 3: Useless alt such as "image" or the file name​

It adds noise and no information.

Mistake 4: Keyword stuffing​

It harms accessibility and trust.

Mistake 5: Describing too much​

A paragraph in alt is hard to listen to. Put long details in the page text.

Mistake 6: Describing decoration​

If the image adds nothing, use alt="", and do not make visitors listen to it.

Mistake 7: Describing the look instead of the function​

A search icon inside a button should say "Search," not "magnifying glass."

Mistake 8: Important words inside the image only​

Repeat the text in the alt, or better, put it in HTML.

Try It Yourself​

Here is a short project.

  1. Create a folder images with at least five pictures of different kinds: a photo, a logo, an icon, a decorative shape, and a chart or screenshot.
  2. Rename every file with a clear, lowercase, hyphenated name.
  3. Create alt-practice.html and add each image with a correct src.
  4. Write a thoughtful alt for each one, using the table in this lesson.
  5. Make the logo a link to the home page, with alt that describes the destination.
  6. Mark the decorative shape with alt="".
  7. Break one src on purpose and check that the page still makes sense.
  8. Turn on a screen reader and listen to the page. Rewrite any alt that sounds odd.
  9. Bonus: write the same alt texts in a second language.

Quick Recap​

Click a question to check your answer.

1. What does the src attribute contain?

The address of the image file, as a relative, root-relative, or absolute URL.

2. Why are lowercase, hyphenated file names recommended?

Many servers are case-sensitive, and spaces or special characters can break URLs. Clear names also help people and search engines.

3. What is the main purpose of alt text?

To give the same information or function as the image to anyone who cannot see it.

4. When should alt be empty?

When the image is decorative or its meaning is already given by nearby text, so that a screen reader skips it.

5. What should alt say for an image that is the only content of a link?

Where the link goes, such as "CodeHarborHub home," because the alt text becomes the link's name.

Key Takeaways​

  • src points to the file. Use correct relative paths, simple lowercase file names, and https for outside images.
  • alt is the image's text version. Write it for the visitor who cannot see the picture.
  • Match the alt to the image's job: information, function, or decoration.
  • Use alt="" for decoration, never leave alt out, and never write "image of."
  • Write alt in the page's language, keep it concise, and test with images turned off and a screen reader.

You can now point to your images and describe them well. Next, learn how to stop your page from jumping while images load with Width and Height.