Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › HTML Snippets › Reference › <img>

HTML Reference

<img> — The Image Element

Embeds an image into a page. A void element with one required attribute and one ethical rule: alt text.

Last updated: October 2026

Definition: The <img> element embeds an image into the document. It is a void element — it has no closing tag. The src attribute is required and points to the image file. The alt attribute provides fallback text for accessibility.

🎬 Live Example

Every img needs three things: src, alt, and width/height.

MAK UI Studio social card showing the site name and tagline

💡 The alt text describes the image for screen readers and search engines. The width and height prevent layout shift.

📝

Basic Syntax

<img src="photo.jpg" alt="A description">

Two attributes are essential: src (the image URL) and alt (the fallback description).

⚙️ Attributes Reference

Attribute Value Purpose
srcURLImage source. Required.
alttextDescription for accessibility. Required.
widthpixelsReserves horizontal space. Prevents layout shift.
heightpixelsReserves vertical space. Prevents layout shift.
loadinglazy / eagerDefer loading until near viewport.
srcsetcomma-separated listMultiple image sizes for responsive display.
sizesmedia conditionsTells the browser which srcset size to pick.
decodingasync / sync / autoHint for the browser's decoding strategy.
fetchpriorityhigh / low / autoPriority hint for loading. Use high for hero images.
ismap / usemap—Legacy image map attributes. Rarely used.
1

Basic Image

The minimum viable img element — src, alt, and dimensions.

<img
    src="/images/team.jpg"
    alt="Our engineering team at the 2026 conference"
    width="800"
    height="600"
>

Setting width and height as attributes (not CSS) tells the browser the aspect ratio before the image loads, preventing layout shift.

2

Lazy Loading

Load images only when they're about to enter the viewport.

<!-- Hero image: load immediately -->
<img src="hero.jpg" alt="Hero" loading="eager" fetchpriority="high">

<!-- Everything below the fold: lazy load -->
<img src="product.jpg" alt="Product" loading="lazy">

💡 Rule: The first image on the page (or the hero) should use loading="eager". Everything below the fold should use loading="lazy". Lazy loading hero images hurts LCP.

3

Responsive Images with srcset

Serve different image sizes based on the viewport width.

<img
    src="photo-800.jpg"
    srcset="
        photo-400.jpg 400w,
        photo-800.jpg 800w,
        photo-1200.jpg 1200w,
        photo-1600.jpg 1600w
    "
    sizes="(max-width: 600px) 100vw,
           (max-width: 1200px) 50vw,
           800px"
    alt="Mountain landscape at sunset"
    width="800"
    height="600"
    loading="lazy"
>

The browser picks the best size for the current viewport. Mobile phones download the 400px version; desktops pick 800px or larger.

4

Modern Image Formats

Serve WebP or AVIF with a fallback for older browsers using <picture>.

<picture>
    <source srcset="photo.avif" type="image/avif">
    <source srcset="photo.webp" type="image/webp">
    <img src="photo.jpg" alt="Description" width="800" height="600">
</picture>

AVIF is 50% smaller than JPEG. WebP is 25% smaller. Modern browsers pick the best format they support; older browsers fall back to JPG.

5

Clickable Image

Wrap an image in an anchor to make it a link.

<a href="/products/widget">
    <img
        src="/images/widget.jpg"
        alt="The Widget — click to view product details"
        width="300"
        height="300"
    >
</a>

Important: The alt text should describe the destination, not just the image. "Click to view product details" is better than "Widget image."

🎯 Writing Good Alt Text

Image type Alt text strategy
Informative photoDescribe what's happening: "Two developers pair-programming at a whiteboard."
Product imageName the product: "Blue cotton t-shirt, front view."
Chart or graphSummarize the key insight: "Bar chart showing revenue growth from 2020 to 2026."
Logo in a linkName the destination: "MAK UI Studio homepage."
Decorative imageUse empty alt: alt="" — tells screen readers to skip.
Purely functional iconDescribe the action: "Search", "Close menu", "Next page."

💡 Test: Read the alt text out loud. If it doesn't make sense as a standalone description of the image's role, rewrite it.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the HTML <img> tag?

The <img> tag embeds an image into a web page. It is a void element — it has no closing tag. The src attribute points to the image file, and the alt attribute provides text for screen readers and when the image fails to load.

Why is the alt attribute important?

The alt attribute describes the image for screen readers, search engines, and users when images fail to load. It is required for accessibility. Empty alt (alt="") is correct for purely decorative images.

What is the difference between img and picture?

<img> displays a single image. <picture> lets you provide multiple sources for responsive images — different images at different viewport sizes or formats (like WebP or AVIF). Use <img> inside <picture> for the fallback.

Should I set width and height on images?

Yes. Setting width and height attributes prevents layout shift (CLS) because the browser reserves space before the image loads. It's one of the highest-impact performance optimizations for pages with images.

What does loading="lazy" do?

loading="lazy" tells the browser to defer loading the image until it's about to enter the viewport. This improves initial page load speed. Use it for images below the fold. Do not use it for hero images.

🎯 What's Next?

← All HTML Reference