Images make websites more interesting and help explain ideas visually.
Image tag usage in HTML
<img src="image-url" alt="description">
The src attribute tells the browser where to find the image.
The alt attribute describes the image for people who can't see it.
<img src="cat.jpg" alt="A cute cat">
<img src="https://example.com/logo.png" alt="Company logo">
<img src="icon.svg" alt="Search icon" width="50" height="50">
This example shows a simple webpage with an image and a caption. The image scales to fit the screen and has a description for accessibility.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Tag Example</title> <style> body { font-family: Arial, sans-serif; padding: 1rem; max-width: 600px; margin: auto; } img { max-width: 100%; height: auto; border-radius: 0.5rem; display: block; margin-bottom: 1rem; } figcaption { font-style: italic; color: #555; margin-bottom: 2rem; } </style> </head> <body> <main> <figure> <img src="https://via.placeholder.com/400x200.png?text=Sample+Image" alt="Sample placeholder image"> <figcaption>This is a sample image with a description.</figcaption> </figure> <p>Images help make content clearer and more engaging.</p> </main> </body> </html>
Always include the alt attribute to help users with screen readers.
Use relative or absolute URLs in src depending on where your image is stored.
Control image size with CSS or width and height attributes for better layout.
The <img> tag adds pictures to your webpage.
Use src to set the image source and alt to describe it.
Images improve user experience by making content visual and accessible.
Practice
<img> tag in HTML?Solution
Step 1: Understand the role of the <img> tag
The <img> tag is specifically designed to add pictures to a webpage.Step 2: Compare with other options
Other tags like <a> create links, <p> add text, and CSS styles the page, so they are not correct here.Final Answer:
To display images on a webpage -> Option DQuick Check:
<img> tag = display images [OK]
- Confusing <img> with <a> for links
- Thinking <img> adds text
- Assuming <img> styles content
Solution
Step 1: Check the tag and attributes
The correct tag is <img> with attributes src for source and alt for alternative text.Step 2: Identify incorrect attributes or tags
<image> is not a valid tag, href is for links, and title is not the alt text.Final Answer:
<img src="photo.jpg" alt="A photo"> -> Option AQuick Check:
Use src and alt in <img> [OK]
- Using <image> instead of <img>
- Using href instead of src
- Confusing alt with title attribute
<img src="flower.png" alt="A red flower">
Solution
Step 1: Understand how the browser uses src and alt
The browser tries to load the image from the src path. If it succeeds, it shows the image.Step 2: Role of alt attribute
If the image cannot load, the alt text is shown as a fallback. No error message appears on the page.Final Answer:
The image file named flower.png if it exists, otherwise a broken image icon -> Option CQuick Check:
Image shows if src works, alt shows if not [OK]
- Thinking alt text always shows instead of image
- Expecting error messages on page
- Assuming missing alt means no display
<img src='cat.jpg' alt=Cat Picture>
Solution
Step 1: Check attribute syntax
HTML attributes with spaces in values must be quoted. alt=Cat Picture lacks quotes around 'Cat Picture'.Step 2: Verify tag and attributes
The tag <img> is correct, src is correct, and alt is needed for accessibility.Final Answer:
Missing quotes around the alt attribute value -> Option BQuick Check:
Attribute values with spaces need quotes [OK]
- Leaving out quotes for multi-word alt text
- Using <image> instead of <img>
- Removing alt attribute entirely
<img> tag?Solution
Step 1: Understand accessibility for decorative images
Decorative images should have an empty alt attribute (alt="") so screen readers skip them.Step 2: Analyze other options
Omitting alt means screen readers may read the file name; descriptive alt text is for meaningful images; aria-hidden="false" means not hidden.Final Answer:
<img src="decorative.png" alt=""> -> Option AQuick Check:
Empty alt hides decorative images from screen readers [OK]
- Omitting alt attribute entirely
- Adding descriptive alt for decorative images
- Using aria-hidden="false" instead of true
Ready to prove it?
Pass the test and this pattern moves unknown
