Back to HTML Notes
Topic #123

The img Tag

By the end of this lesson, you will be able to write accessible, performant HTML image tags using essential attributes like src, alt, width, height, loading, and srcset.

What it is

The <img> tag embeds an image into an HTML document. It is a void element, meaning it has no closing tag. The browser fetches the resource specified by the source attribute and renders it within the layout flow. Key related terms include "responsive images," "lazy loading," and "accessibility."

Why it matters

  • Accessibility: Screen readers rely on alternative text to describe visual content to users with disabilities.
  • Performance: Proper sizing prevents layout shifts (CLS), while lazy loading reduces initial page weight.
  • Responsiveness: Source sets allow browsers to choose the optimal image resolution for the user's device.
  • SEO: Descriptive alt text helps search engines understand image context.

Syntax or steps

The basic syntax requires at least two attributes: src (source URL) and alt (alternative text). For modern best practices, always include width and height to reserve space before the image loads. Use loading="lazy" for images below the fold. For high-DPI screens, use srcset.

Example

<img 
  src="photo-small.jpg" 
  srcset="photo-small.jpg 400w, photo-large.jpg 800w" 
  sizes="(max-width: 600px) 100vw, 50vw"
  alt="A golden retriever playing in the park" 
  width="800" 
  height="600" 
  loading="lazy">

Breakdown:

  • src: Fallback image if srcset is unsupported.
  • srcset: Lists candidate images with their intrinsic widths (e.g., 400w).
  • sizes: Tells the browser how wide the image will appear on screen relative to the viewport.
  • alt: Text description for accessibility and SEO.
  • width/height: Prevents Cumulative Layout Shift (CLS).
  • loading="lazy": Defers off-screen images until needed.

Common mistakes

  • Missing Alt Text: Leaving alt="" without reason hides the image from screen readers. Use empty alt only for decorative images; otherwise, provide descriptive text.
  • Omitting Dimensions: Not specifying width and height causes layout jumps as images load, hurting Core Web Vitals.
  • Incorrect Srcset Syntax: Using pixel density descriptors (e.g., 2x) incorrectly mixed with width descriptors (w). Stick to one type per attribute.
  • Lazy Loading Above-the-Fold Images: Applying loading="lazy" to hero images delays critical rendering. Use loading="eager" or omit the attribute for immediate visibility.

When to use it

Use <img> for content images that convey information. Compare it with CSS background images:

Feature<img> TagCSS Background Image
AccessibilityHigh (supports alt)Low (ignored by screen readers)
PrintabilityPrints by defaultOften hidden in print
Use CaseContent (photos, diagrams)Decoration (patterns, textures)

Practice

Guided Exercise: Write an <img> tag for a logo named logo.png. It should have dimensions 200x50, alt text "Company Logo", and eager loading.

Challenge: Modify your previous code to support responsive images. Assume you have logo-1x.png and logo-2x.png. Use srcset with pixel density descriptors.

Hint: Syntax looks like srcset="logo-1x.png 1x, logo-2x.png 2x".

Quick check

Q: Why is specifying width and height attributes important even if CSS controls the display size?

A: They define the aspect ratio, allowing the browser to reserve the correct amount of space before the image downloads, preventing layout shifts.

Summary

The <img> tag is fundamental for web content but requires careful attribute management for performance and accessibility. Always provide meaningful alt text, explicit dimensions, and consider lazy loading strategies to optimize user experience.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

The img Tag – FAQs

Quick answers about learning The img Tag in HTML.

This free note from Coding Now Tech Institute explains The img Tag in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including The img Tag, is 100% free with no signup required.
With focused practice, most students grasp The img Tag in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now