🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEhtml

html Documentation

LOADING ENGINE...

<img>

AI & DATA SCIENCE // img-tag

The <img> element embeds an image in the page — a void element whose src points to the file and whose alt provides essential text for accessibility and SEO.

Syntax

<img src="/photo.jpg" alt="A red bicycle leaning against a brick wall">

Deep Dive Course

**`<img>`** requires **`src`** (the image URL) and should always include **`alt`** — descriptive text that screen readers announce in place of the image, that displays if the image fails to load, and that search engines use to understand the image's content (a real ranking factor for image search). Modern responsive images can also use **`srcset`** and **`sizes`** to let the browser pick the best-resolution file for the user's screen and connection, and **`loading="lazy"`** to defer loading offscreen images until they're about to scroll into view.

1Understanding <img>

`<img>` requires `src` (the image URL) and should always include `alt` — descriptive text that screen readers announce in place of the image, that displays if the image fails to load, and that search engines use to understand the image's content (a real ranking factor for image search). Modern responsive images can also use `srcset` and `sizes` to let the browser pick the best-resolution file for the user's screen and connection, and `loading="lazy"` to defer loading offscreen images until they're about to scroll into view.

💡

For a purely decorative image with no informational content (like a background flourish), use alt="" (empty, but present) — this tells screen readers to skip it silently, rather than leaving it out entirely, which some tools may then announce using the filename instead.

editor.html
<img src="/team-photo.jpg" alt="Our team of five developers at the 2026 offsite">
localhost:3000

2Practical Example

Here is a real-world application of <img> showing how it is used in production HTML.

editor.html
<!-- Responsive image serving different resolutions -->
<img src="/photo-800.jpg"
     srcset="/photo-400.jpg 400w, /photo-800.jpg 800w, /photo-1600.jpg 1600w"
     sizes="(max-width: 600px) 100vw, 800px"
     alt="Mountain landscape at sunset"
     loading="lazy">
localhost:3000

3Best Practices

Follow these guidelines when working with <img>:

1. Always include a descriptive alt attribute — never omit it

2. Use alt="" specifically for purely decorative images so screen readers skip them

3. Use loading="lazy" on images below the fold to improve initial page load performance

⚠️

Tip: For a purely decorative image with no informational content (like a background flourish), use alt="" (empty, but present) — this tells screen readers to skip it silently, rather than leaving it out entirely, which some tools may then announce using the filename instead.

editor.html
<img src="/team-photo.jpg" alt="Our team of five developers at the 2026 offsite">
localhost:3000

Examples

Example 01Basic Usage
<img src="/team-photo.jpg" alt="Our team of five developers at the 2026 offsite">
Example 02Advanced Example
<!-- Responsive image serving different resolutions -->
<img src="/photo-800.jpg"
     srcset="/photo-400.jpg 400w, /photo-800.jpg 800w, /photo-1600.jpg 1600w"
     sizes="(max-width: 600px) 100vw, 800px"
     alt="Mountain landscape at sunset"
     loading="lazy">

Best Practices

  • Always include a descriptive alt attribute — never omit it
  • Use alt="" specifically for purely decorative images so screen readers skip them
  • Use loading="lazy" on images below the fold to improve initial page load performance

Interview Question

Why should alt="" (empty) be used for decorative images instead of omitting the alt attribute entirely?

Hint: Think about what a screen reader does when alt is completely missing versus explicitly empty.

When alt is completely absent, some screen readers fall back to announcing the image's filename or full URL, which is often meaningless noise ('IMG dash two four one three dot JPG'). Setting alt="" explicitly tells the screen reader 'this image is decorative and carries no information' — it skips over it silently, which is the correct, intentional behavior for something like a background flourish that adds nothing to the content.

Exercises

EasyPractice using <img> in a real scenario.
View Solution
<img src="/team-photo.jpg" alt="Our team of five developers at the 2026 offsite">

Frequently Asked Questions

Why should alt="" (empty) be used for decorative images instead of omitting the alt attribute entirely?

When alt is completely absent, some screen readers fall back to announcing the image's filename or full URL, which is often meaningless noise ('IMG dash two four one three dot JPG'). Setting alt="" explicitly tells the screen reader 'this image is decorative and carries no information' — it skips over it silently, which is the correct, intentional behavior for something like a background flourish that adds nothing to the content.

Related Functions

video-tagmap-tag