What if your beautiful image disappears--will your message vanish too?
Why Image source and alt attribute in HTML? - Purpose & Use Cases
Imagine you want to show a picture on your webpage. You type the image's file path directly into your code without any description.
If the image file is missing or slow to load, visitors see a broken icon or nothing at all. Also, people using screen readers or with slow internet miss out because there is no text to explain the image.
Using the src attribute tells the browser where to find the image, and the alt attribute provides a text description. This helps everyone understand what the image is, even if it doesn't load or if they use assistive tools.
<img src="photo.jpg"><img src="photo.jpg" alt="A smiling child playing in the park">
This makes your website more reliable, accessible, and friendly to all users, no matter how they browse.
Think about a news site showing a photo of a protest. If the image doesn't load, the alt text can say "People holding signs at a peaceful protest," so readers still get the message.
Always use src to link your image file.
Use alt to describe the image for accessibility and fallback.
This helps everyone understand your content better.
Practice
alt attribute in an <img> tag do?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]
- Confusing alt with src attribute
- Thinking alt controls image size
- Using alt to add styling
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]
- Using <image> instead of <img>
- Using 'source' instead of 'src'
- Using 'description' instead of 'alt'
<img src="logo.png" alt="Company Logo">
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]
- Expecting no output or blank space
- Thinking browser shows error popups
- Assuming browser crashes
<img src="picture.jpg" alt>
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]
- Leaving alt attribute empty
- Adding closing tag
- Changing alt attribute name incorrectly
<img> tag for accessibility and SEO?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]
- Using vague alt text like 'image' or 'cute'
- Omitting alt attribute entirely
- Using unrelated or too short alt text
Ready to prove it?
Pass the test and this pattern moves unknown
