Lesson 3
Adding Alt Text for Better Accessibility
Importance of Alt Text

Welcome to another crucial topic in our journey to create engaging and accessible webpages! In this lesson, we'll dive into the importance of alt text (alternative text) for images in HTML. Adding alt text to images is not just a good practice; it is essential for making your webpages accessible to all users, including those who rely on screen readers due to visual impairments.

What You'll Learn

In this lesson, we will cover:

  • Understanding the role of alt text in improving accessibility.
  • How to add alt text to an image using the alt attribute in the <img> tag.
  • The importance of writing descriptive alt text.

Here's a code snippet to get you started:

HTML, XML
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <title>My Enhanced Webpage</title> 6</head> 7<body> 8 <h1>Welcome to My Enhanced Webpage</h1> 9 <p>Here is an image:</p> 10 <img src="https://codesignal-staging-assets.s3.amazonaws.com/uploads/1718971012550/a0506561-f951-4885-a4d4-faa6bd6ab16f.jpg" alt="An image of floating Space Corgis" width="300"> 11 <img src="doesnt-exist.jpg" alt="This image doesn't exist. This is an alternative text"> 12</body> 13</html>

The alt attribute in the <img> tag provides alternative text for the image if it cannot be displayed. It also helps screen readers describe the image to visually impaired users.

Note that some browsers may not display the alt text if the image is not found. This might be the case with Safari.

Why It Matters

Alt text is a small but significant addition to your HTML that makes a big difference. Here's why it's important:

  • Accessibility: Alt text ensures that visually impaired users who use screen readers can understand the content of your images. The screen reader will read the alt text aloud, allowing all users to comprehend the visual elements of your webpage.
  • SEO Benefits: Search engines cannot 'see' images but rely on alt text to understand them. Properly used alt text can improve your webpage's search engine ranking.
  • Error Handling: If an image fails to load, the alt text will be displayed in its place, providing context to users instead of a broken image icon.

Making your webpage more accessible not only benefits a wider audience but also enhances the overall user experience and search engine performance. Ready to make your images more informative and accessible? Let’s head into the practice section and apply these concepts!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.