<img> Tag in HTML

Quick facts

CategoryFlow content, phrasing content, embedded content, palpable content
Content modelEmpty — it’s a void element with no children
Tag omissionVoid element — no end tag, ever
Permitted parentsAny element that accepts embedded content
DOM interfaceHTMLImageElement

Syntax

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

Live example

Edit the code below and watch the preview update instantly. This example uses an inline SVG data URI so it works with no external files.

index.html
Preview

Attributes

AttributeDescription
srcRequired. Path or URL to the image file
altAlternative text for accessibility and SEO; use alt="" only for purely decorative images
width / heightIntrinsic dimensions in pixels — set both to prevent layout shift while the image loads
loadinglazy defers loading until the image nears the viewport; eager loads immediately
srcsetA list of image candidates for different screen resolutions or widths

Common examples

Make an image responsive

This one CSS rule stops images from overflowing small screens while keeping their shape.

<img src="photo.jpg" alt="Sunset over the harbour" width="1200" height="800"> <style> img { max-width: 100%; height: auto; } </style>

Serve sharper images on high-resolution screens

With srcset, the browser picks the best file for the screen. For different crops or formats, use <picture>.

<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x" alt="Company logo" width="200" height="60">

Use an image as a link

Wrap the image in an <a>. The alt text then describes where the link goes.

<a href="/"> <img src="logo.png" alt="FreeHTMLEditor.net home" width="200" height="60"> </a>

<img> vs CSS background images

An <img> is part of the page content: it has alt text, it is read by screen readers, and it can appear in Google Images. A CSS background-image is pure decoration: it has no alt text and is invisible to assistive technology and image search.

So the rule of thumb is: if the image would be missed when removed — a product photo, a chart, a portrait — use <img>. If it is only there to look nice, such as a texture or a hero background behind text, use CSS. When you need different crops or modern formats for one content image, wrap it in <picture>.

<img>CSS background-image
Alt text / screen readersYesNo
Google ImagesCan appearNot indexed as content
Best forMeaningful picturesDecoration

Common mistakes to avoid

  • Missing or useless alt text. Leaving out alt, or writing “image” or a file name, fails screen reader users and wastes an SEO opportunity.
  • No width and height. Without them the page jumps as images load (layout shift), which hurts the Core Web Vitals score Google uses for ranking.
  • Huge files. A 5 MB photo shown at 600 pixels wide slows the page badly. Resize and compress images, and prefer WebP or AVIF.
  • Lazy-loading the main image. loading="lazy" is great below the fold, but on the first big image it delays the page’s main content.

Accessibility & SEO tips

  • Write alt text that describes the image’s purpose in context. For purely decorative images, use an empty alt="" so screen readers skip them.
  • Use descriptive file names such as red-running-shoes.jpg rather than IMG_0042.jpg; Google Images reads them.
  • Wrap images that need a visible caption in a <figure> with a <figcaption>.

Browser support

Supported in every browser, including Chrome, Edge, Firefox, Safari and their mobile versions — it has been part of HTML since the very first versions. Newer formats such as AVIF and WebP work in all current browsers.

Related tags

Frequently asked questions

Is alt required?
Browsers won't block rendering without it, but it's essential for accessibility (screen readers) and helps search engines understand the image. Use an empty alt="" only for images that are purely decorative.
What does loading="lazy" do?
It tells the browser to defer loading the image until it's about to scroll into view, which can noticeably improve initial page load performance on image-heavy pages.
Why is my image not showing?
Usually the src path is wrong. Check the folder, the spelling and the capitalisation (many servers treat Photo.jpg and photo.jpg as different files), and open the image URL directly in the browser to test it.
How do I make an image responsive?
Add img { max-width: 100%; height: auto; } to your CSS and keep the width and height attributes in the HTML. The image then scales down on small screens without distortion.
Which image format should I use?
Use WebP or AVIF for photos (smaller files), PNG for screenshots or images needing transparency, SVG for logos and icons, and JPEG as a widely supported fallback.