HTML <img> Tag

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Images & Media

What You’ll Learn

By the end of this tutorial, you’ll embed images in HTML using the <img> element with proper attributes, responsive techniques, and accessibility.

The <img> tag is a fundamental HTML element for displaying photos, icons, logos, and illustrations. This guide covers syntax, attributes, common patterns, and best practices for beginners and web developers alike.

01

Core Syntax

Use src and alt on every image.

02

Void Element

Understand that img has no closing tag in HTML.

03

Dimensions

Set width and height to prevent layout shift.

04

Image Links

Wrap img in a for clickable thumbnails.

05

Responsive Images

Scale across devices with srcset and CSS.

06

Accessibility

Write meaningful alt text for all users.

What Is the <img> Tag?

The image element (<img>) embeds an image into an HTML document. It is a void (empty) element — it does not wrap content and has no closing tag in HTML syntax.

💡
Not the same as image

Beginners sometimes write <image>, which is not a valid HTML tag. Always use <img>. See the image vs img guide for details.

Attributes such as src, alt, width, and height tell the browser where to fetch the file, how to describe it for accessibility, and how much space to reserve on the page.

📝 Syntax

Include an img element with src pointing to the image file and alt describing its content:

syntax.html
<img src="photo.jpg" alt="Description of the image">

Syntax Rules

  • img is a void element — no </img> closing tag in HTML.
  • src is required (unless using lazy-loading placeholders with data-src patterns).
  • alt is required for accessibility — use alt="" only for purely decorative images.
  • Set intrinsic width and height to reduce cumulative layout shift (CLS).
  • Prefer modern formats (WebP, AVIF) via picture when you need fallbacks.

⚡ Quick Reference

Use CaseCode SnippetNotes
Basic image<img src="photo.jpg" alt="...">Minimum viable markup
With dimensionswidth="300" height="200"Prevents layout jump
Lazy loadingloading="lazy"Defers off-screen images
Responsivesrcset="..." sizes="..."Multiple resolutions
Image link<a><img ...></a>Clickable thumbnail
Decorativealt=""Screen readers skip it

⚖️ <img> vs <image>

Only one of these is valid for HTML page content:

Feature<img><image>
Valid in HTMLYesNo
Renders picturesYesNo
Standard elementCore HTML since early versionsCommon beginner mistake
Learn moreThis tutorialimage misconception guide

⚖️ <img> vs CSS background-image

Choose based on whether the image carries semantic meaning:

ApproachBest forAccessibility
<img>Content images, logos, diagramsalt text
CSS backgroundPure decoration, textures, hero overlaysNo native alt text
<figure> + imgImages with captionsfigcaption adds context
<picture>Art direction, format fallbacksUses img with alt inside

🧰 Attributes

Essential <img> attributes plus global attributes such as class and id:

src Required

URL or path to the image file (JPEG, PNG, WebP, SVG, GIF, etc.).

src="photo.jpg"
alt Required

Alternative text for accessibility and when the image fails to load.

alt="Sunset over the ocean"
width / height Layout

Intrinsic dimensions in pixels to reserve space and reduce layout shift.

width="300" height="200"
srcset / sizes Responsive

Offer multiple image resolutions for different viewports and pixel densities.

srcset="large.jpg 2x"
loading Performance

Defer off-screen images with loading="lazy".

loading="lazy"
decoding Performance

Hint decode timing: async, sync, or auto.

decoding="async"
attributes.html
<img
  src="photo.jpg"
  alt="Mountain landscape at sunrise"
  width="300"
  height="200"
  loading="lazy"
  decoding="async"
>

Do not use the filename as alt text. Describe what the image communicates.

Examples Gallery

Single images, clickable thumbnails, and responsive patterns with copy-ready code and live previews.

Live Preview

Standalone image embedded with the img element:

JavaScript logo

Displaying Single Images

The primary use of <img> is to display standalone images within page content.

displaying-single-images.html
<p>Check out this logo:</p>
<img src="/images/javascript.png" alt="JavaScript logo" width="128" height="128">
Try It Yourself

📚 Common Use Cases

Use <img> for logos, product photos, avatars, diagrams, and icons. Wrap in <a> for clickable thumbnails, or in <figure> when a caption is needed.

Responsive Images

Ensure images scale properly across devices with srcset, sizes, and CSS.

responsive-images.html
<img
  src="small.jpg"
  srcset="large.jpg 1024w, medium.jpg 640w"
  sizes="(max-width: 600px) 100vw, 50vw"
  alt="Responsive image"
  loading="lazy"
>
Try It Yourself

Styling <img> with CSS

Control sizing, cropping, and layout of images with CSS while keeping semantic img markup:

max-width: 100% Fluid responsive images
object-fit: cover Crop to fixed box
border-radius Rounded avatars
aspect-ratio Consistent proportions
img-styles.css
/* Responsive by default */
img {
  max-width: 100%;
  height: auto;
  display: block;
}

img.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
}

img.hero {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: 12px;
}

Rounded img preview

♿ Accessibility

Images must be usable for everyone, including assistive technology users:

  • Meaningful alt text — describe purpose and content, not just “image” or the filename.
  • Decorative images — use alt="" so screen readers skip purely visual decoration.
  • Linked imagesalt should describe the link destination, not just the picture.
  • Text in images — repeat critical text in HTML or alt when possible.
  • Contrast and size — ensure icons and UI images remain visible and tappable on mobile.

🧠 How <img> Works

1

Author sets src and alt

Point src at the image file and describe it with alt.

Markup
2

Browser reserves space

width and height prevent layout from jumping while the file downloads.

Layout
3

Image renders inline

The browser decodes and paints the image in the document flow at its natural or styled size.

Rendering
=

Visible, accessible image

Users see the picture; assistive tech reads the alt text when needed.

Universal Browser Support

The <img> element is fully supported in all browsers, including legacy Internet Explorer.

Baseline · Since HTML 2

The img element works everywhere

From legacy Internet Explorer to the latest mobile browsers — embed images with confidence using img.

100% Core tag support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
<img> tag 100% supported

Bottom line: The <img> element is one of the most universally supported HTML tags in production use today.

Conclusion

Understanding the HTML <img> tag is essential for integrating images into web pages. Whether displaying single images, creating clickable thumbnails, or building responsive layouts, img is the standard, versatile tool for visual content.

Always include meaningful alt text, set dimensions to prevent layout shift, optimize file sizes, and choose the right format for each use case.

💡 Best Practices

✅ Do

  • Include descriptive alt text on every content image
  • Set width and height to reduce layout shift
  • Optimize images (WebP, compressed JPEG/PNG, SVG for icons)
  • Use loading="lazy" for below-the-fold images

❌ Don’t

  • Write <image> instead of <img>
  • Omit alt or use filenames as alt text
  • Upload huge unoptimized photos
  • Use CSS backgrounds for meaningful content images

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <img>

Bookmark these before you ship — they’ll keep your images fast and accessible.

6
Core concepts
📝 02

Void Element

No closing tag required in HTML syntax.

Syntax
03

alt Is Essential

Describe images for accessibility and SEO.

Accessibility
📏 04

Set Dimensions

Width and height prevent layout shift.

Performance
🔗 05

Image Links

Wrap img in a for clickable thumbnails.

Pattern
06

Universal Support

Works in every browser, including legacy IE.

Compatibility

❓ Frequently Asked Questions

It embeds an image in a web page using src for the file URL and alt for alternative text.
img is a void element. In HTML you write it without a closing tag. A trailing slash is optional in XHTML-style syntax.
It provides text for screen readers and when images fail to load. It is required for accessible, SEO-friendly pages.
img is valid HTML. image is not a standard tag for page content. See the image misconception guide.
Use srcset and sizes, CSS max-width: 100%, and optionally the picture element for art direction.

Practice Embedding Images

Try single images, clickable thumbnails, and responsive srcset in the interactive editor.

Try single image example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful