HTML Images

HTML Living Standard

An img embeds an image in the page. Its attributes can describe the image, reserve space for it, and tell the browser where to find suitable versions.

On this page

How does <img> show a picture?

The img element reserves a place for an image. src gives the browser the address of the image file. The browser reads that file separately and draws it in the page. img is a void element with no closing tag.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<img src="https://javascript-training.cloudninemm.com/cloud-nine-logo.png" width="64" height="64" alt="Cloud Nine Innovations logo">

What to expect With an internet connection, the site logo appears. Change the URL to a missing image and observe the alternative text.

Image source and alternative text

The browser loads the URL in src. Write alt to communicate what the image contributes in its present context. A meaningful image needs a useful text alternative. Give a purely decorative image alt="" so assistive tools can skip it. Include width and height using the image’s proportions so the browser can reserve space before the file arrives.

How to try it Place your photo mountains.jpg beside index.html. For the decoration, add any SVG named flourish.svg or leave out the second img. Put the HTML inside body.

<img src="mountains.jpg" width="640" height="480" alt="Mountain landscape">
<img src="flourish.svg" alt="">

What to expect With mountains.jpg in the same folder, the first image appears. Change its src to a missing file to see how your browser shows a broken image and its alternative text.

Visible captions with <figure>

Use figure around a self-contained illustration. Put figcaption at the beginning or end of it for a visible caption. The caption can explain the photo’s context. The alt text still gives the information a reader would get from the image itself.

How to try it Use the mountains.jpg photo from the previous example and place this figure inside body.

<figure>
  <img src="mountains.jpg" alt="Mountain landscape">
  <figcaption>A view of the mountains at sunrise.</figcaption>
</figure>

What to expect The caption appears under the image, and the alt text is available when readers cannot access the image.

Choose a file size with srcset and sizes

Use srcset to supply the same image at different pixel widths. Each w descriptor is the actual width of that file. sizes tells the browser roughly how wide the image will appear in the layout. The browser uses that information and the screen’s pixel density to select a suitable file. Use CSS to set the displayed size itself.

How to try it Save two versions of the same image beside index.html. Use a 480px-wide mountains-480.jpg and a 960px-wide mountains-960.jpg. Place the img inside body.

<img src="mountains-480.jpg"
     srcset="mountains-480.jpg 480w, mountains-960.jpg 960w"
     sizes="(max-width: 479px) 100vw, 480px"
     style="width: min(480px, 100vw); height: auto"
     alt="Mountain landscape">

What to expect With both files available, inspect the image’s currentSrc in DevTools to see the selected URL. Selection can vary by screen density and browser behavior.

Choose a different crop with <picture>

Use picture for a different crop or composition at a breakpoint. The browser checks source elements in order and uses the first suitable source. The img at the end supplies the fallback, display element, and alternative text.

How to try it Put a wide landscape photo or crop named landscape-wide.jpg and a tighter crop named landscape-small.jpg beside index.html, then place picture inside body. Resize the browser to compare both.

<picture>
  <source media="(min-width: 60rem)" srcset="landscape-wide.jpg">
  <img src="landscape-small.jpg" alt="Mountain landscape near a lake">
</picture>

What to expect The wide crop is selected at 60rem and above. Below that width the img URL supplies the image. Save both image files alongside the HTML to try this example.

Image Fetch Priority (Priority Hints)

Once the browser knows which image file to use, fetchpriority can suggest how urgently to download it. Use high for a key image visible when the page opens, such as a banner that is likely to be the Largest Contentful Paint (LCP) element. Use low for a secondary image you still want the browser to request normally. With auto, the default, the browser chooses the priority. Treat these values as hints and reserve high for the few images that really need it. The loading attribute controls when an image can start loading. Use loading="lazy" for images farther down the page and leave it off the main banner.

How to try it Use Run example to see the layout. To inspect actual network requests, serve index.html from a local web server with mountains.jpg and landscape-small.jpg in the same folder. Place the example inside body.

<h1>Mountain Trail Guide</h1>
<img src="mountains.jpg" alt="Mountains along the hiking trail"
     width="640" height="480" style="max-width: 100%; height: auto"
     fetchpriority="high">

<h2>Photo Gallery</h2>
<img src="landscape-small.jpg" alt="Mountain landscape near a lake"
     width="160" height="160" fetchpriority="low">

What to expect Both images appear. In Chrome DevTools, open Network, enable the Priority column and Disable cache, then reload your saved page to inspect the priority assigned to each image request. Exact priorities and download times can vary.

Official references