HTML <img> Tag

The <img> element embeds an image into the page. It is a void element: set the file with src (and/or srcset) and describe it with alt.

Prefer <img> for meaningful pictures. For art direction or format switching, wrap it in <picture>. There is no HTML <image> tag for page embeds.

Baseline · Widely available.

Not deprecated. Core support has been stable across browsers for decades. Some attributes (for example loading) are newer enhancements.

Accessibility warning.

Always provide useful alt for content images. Empty alt="" is only for pure decoration so screen readers can skip it.

Deprecated attributes.

align, border, hspace, vspace, name, and longdesc are obsolete — use CSS (and modern ARIA description patterns) instead.

HTML Syntax

syntax.html
<img src=" url" alt="text">

Start tag only — no end tag. At least one of src or srcset is required. Add width and height when you know the intrinsic size.

Attributes

The attributes you will use most often on <img>:

AttributeDescriptionTry it
srcURL of the image. Required unless you rely on srcset alone (still keep src as a fallback)Try it
altText alternative when the image cannot be shown or for assistive technologyTry it
width / heightIntrinsic size in CSS pixels — reserves space and reduces layout shiftTry it
srcset / sizesCandidate URLs (w or x descriptors) and layout width hintsTry it
loadingeager (default) or lazy to defer off-screen imagesTry it
decodingHint: async, sync, or auto for decode timingTry it
crossoriginCORS mode when you need canvas / cross-origin image dataSee crossorigin
referrerpolicyWhich referrer to send when fetching the imageSee referrerpolicy
usemap / ismapClient-side map (#name) or server-side map (inside a link)See map / area

Global Attributes

The <img> tag also supports the Global Attributes in HTML.

Live Preview

Switch a real <img> sample and watch src, alt, and size update.

A real <img>. Pick a file, then Apply (or tap a chip).

Preview
JavaScript logo

<img src="/images/javascript.png" alt="JavaScript logo">

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Image with src and alt

Embed one file and reserve its box with width and height.

HTML
<img
  src="/images/javascript.png"
  alt="JavaScript logo"
  width="128"
  height="128"
>
Try It Yourself

How It Works

  1. Write <img> with no closing tag.
  2. Set src to the image path or URL.
  3. Add meaningful alt that describes the picture’s purpose.
  4. Include width / height so the browser can reserve space early.

Example 3 — Density Candidates with srcset

Offer higher-resolution files for sharper screens (x descriptors).

HTML
<img
  src="/images/javascript.png"
  srcset="/images/javascript.png 1x, /images/javascript.png 2x"
  alt="JavaScript logo"
  width="128"
  height="128"
>
Try It Yourself

How It Works

  1. Keep src as the default / fallback URL.
  2. List candidates in srcset with 1x, 2x, … (or use w + sizes for width-based selection).
  3. Do not mix w and x descriptors in the same srcset.
  4. For art direction (different crops), prefer <picture>.

Example 4 — Lazy Loading

Defer an off-screen image until it is near the viewport.

HTML
<img
  src="/images/nature-pattern.jpg"
  alt="Abstract nature pattern"
  width="200"
  height="140"
  loading="lazy"
  decoding="async"
>
Try It Yourself

How It Works

  1. Set loading="lazy" for below-the-fold images.
  2. Keep width and height so unloaded images still occupy space.
  3. Optional decoding="async" hints that decode can wait for other paint work.
  4. Leave heroes eager so the main above-the-fold image is not delayed.

Edge Cases & Pitfalls

a11y

Missing alt

Screen readers may announce the filename. Describe content images; use alt="" only for decoration.

naming

Writing <image>

Invalid for HTML page embeds. Always use <img> (see image vs img).

layout

No size + lazy load

Lazy images without dimensions can stay 0×0 and never enter the viewport. Always set width/height (or CSS aspect-ratio).

srcset

Mixing w and x

One srcset must use either width descriptors or density descriptors — not both.

legacy

Presentational attributes

Replace align, border, hspace, and vspace with CSS.

css

Background vs img

CSS backgrounds are for decoration. Meaningful pictures belong in <img> so alt and semantics stay available.

Browser Support

The <img> element is core HTML embedded content in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<img>

Supported everywhere for decades. Chrome, Firefox, Safari, Edge, and Opera all support it. Attributes such as loading and decoding are progressive enhancements.

99% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<img> Widely supported

Bottom line: Safe to use everywhere. Prefer modern attributes (srcset, loading) as enhancements, not requirements for basic embeds.

Key Takeaways

  • Role: <img> embeds a picture — void element with src/srcset and alt.
  • Must-haves: useful alt, size hints, and a real image URL.
  • Responsive: srcset/sizes or <picture> when one file is not enough.
  • Practice: lazy-load off-screen images; never replace content images with CSS backgrounds alone.

One line: embed with <img src alt>, size it, and enhance with srcset or loading when needed.

Default CSS Settings

User agents treat <img> as a replaced element. Exact rules vary; a common idea:

default.css
img {
  overflow-clip-margin: content-box;
  overflow: clip;
}

img:is([sizes="auto" i], [sizes^="auto," i]) {
  contain-intrinsic-size: 3000px 1500px;
}

Without HTML size attributes, many engines fall back toward a 300×150 placeholder box until the intrinsic size is known. Prefer explicit width / height.

Frequently Asked Questions

The img element embeds an image into the document. It is a void element: you set src (or srcset) and usually alt, with no closing tag.
Yes in practice. Provide meaningful alternative text for content images. Use alt="" only for pure decoration so assistive technology can skip it.
Always use img. There is no standard HTML image tag for page pictures. See the image tag tutorial for that common mistake.
They let the browser reserve space and compute aspect ratio before the file loads, which reduces layout shift. You can still scale with CSS.
Use picture when you need art direction or different formats (for example WebP with a JPEG fallback). picture still requires an img inside as the final choice.
No. Use loading="lazy" for below-the-fold images. Keep important above-the-fold or LCP images eager so they load immediately.

Next: input

Continue with HTML form controls and input types.

input tutorial →

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.

10 people found this page helpful