Images make websites look nice and interesting. The src attribute tells the browser where to find the image. The alt attribute gives a description of the image for people who cannot see it.
Image source and alt attribute in HTML
Introduction
Syntax
HTML
<img src="image-url.jpg" alt="Description of the image">
The src attribute must have the path or URL of the image.
The alt attribute should describe what the image shows in simple words.
Examples
HTML
<img src="cat.jpg" alt="A cute orange cat sitting on a chair">
HTML
<img src="https://example.com/logo.png" alt="Company logo">
HTML
<img src="flower.png" alt="">
Sample Program
This webpage shows a heading, an image of a cat with a clear description, and a short paragraph below it.
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Example</title> </head> <body> <h1>My Favorite Animal</h1> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/320px-Cat03.jpg" alt="A fluffy gray cat lying on a wooden floor" width="320" height="240"> <p>This is a picture of a cat.</p> </body> </html>
Important Notes
Always include the alt attribute for accessibility.
Use meaningful descriptions in alt to help users who cannot see images.
If the image is purely decorative, use an empty alt="" to avoid confusion.
Summary
The src attribute tells the browser where to find the image.
The alt attribute describes the image for people who cannot see it.
Using both helps make websites better for everyone.
Practice
1. What does the
alt attribute in an <img> tag do?easy
Solution
Step 1: Understand the purpose of the
Thealtattributealtattribute provides alternative text describing the image content.Step 2: Recognize its use for accessibility and fallback
This text helps screen readers and shows if the image fails to load.Final Answer:
Provides a text description of the image for screen readers and when the image can't load -> Option AQuick Check:
altdescribes image content [OK]
Hint: Alt text describes images for accessibility and fallback [OK]
Common Mistakes:
- Confusing alt with src attribute
- Thinking alt controls image size
- Using alt to add styling
2. Which of the following is the correct syntax to add an image with source "photo.jpg" and alt text "A sunset"?
easy
Solution
Step 1: Identify correct tag and attributes
The correct tag is<img>with attributessrcandalt.Step 2: Check attribute names and order
<img src="photo.jpg" alt="A sunset">uses correct attribute names and syntax:src="photo.jpg"andalt="A sunset".Final Answer:
<img src="photo.jpg" alt="A sunset"> -> Option AQuick Check:
Correct tag and attributes =<img src="photo.jpg" alt="A sunset">[OK]
Hint: Use <img> with src and alt attributes exactly named [OK]
Common Mistakes:
- Using <image> instead of <img>
- Using 'source' instead of 'src'
- Using 'description' instead of 'alt'
3. What will the browser display if this code is used and the image file "logo.png" is missing?
<img src="logo.png" alt="Company Logo">
medium
Solution
Step 1: Understand what happens when image source is missing
If the image file can't be found, the browser shows the alt text as a fallback.Step 2: Identify the alt text in the code
The alt text is "Company Logo", so this text will display in place of the missing image.Final Answer:
The text "Company Logo" will appear instead of the image -> Option DQuick Check:
Missing image shows alt text [OK]
Hint: Missing images show alt text in browser [OK]
Common Mistakes:
- Expecting no output or blank space
- Thinking browser shows error popups
- Assuming browser crashes
4. Find the error in this image tag:
<img src="picture.jpg" alt>
medium
Solution
Step 1: Check the alt attribute syntax
The alt attribute is present but has no value assigned, which is invalid.Step 2: Confirm correct attribute names and tag usage
The src attribute is correct, and<img>is self-closing, so no closing tag needed.Final Answer:
The alt attribute is missing a value -> Option CQuick Check:
Alt attribute must have a value [OK]
Hint: Alt attribute always needs a descriptive value [OK]
Common Mistakes:
- Leaving alt attribute empty
- Adding closing tag
- Changing alt attribute name incorrectly
5. You want to add an image that shows a cat playing with a ball. Which is the best way to write the
<img> tag for accessibility and SEO?hard
Solution
Step 1: Understand the purpose of alt text for accessibility and SEO
Alt text should clearly describe the image content to help screen readers and improve search engine understanding.Step 2: Evaluate the options for descriptive quality
<img src="cat-ball.jpg" alt="Cat playing with a ball">describes the image specifically as "Cat playing with a ball", which is clear and meaningful.Final Answer:
<img src="cat-ball.jpg" alt="Cat playing with a ball"> -> Option BQuick Check:
Descriptive alt text improves accessibility and SEO [OK]
Hint: Use clear, specific alt text describing the image content [OK]
Common Mistakes:
- Using vague alt text like 'image' or 'cute'
- Omitting alt attribute entirely
- Using unrelated or too short alt text
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
