Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the void <img> element, with a src attribute pointing to the image and an alt attribute providing its useful text alternative: <img src="images/photo.jpg" alt="A description of the image">. The image is a separate resource from the HTML file, so the path must resolve from the page’s URL and the image must be available to the browser.

The basic HTML image element

For one image, start with this markup:

<img src="images/photo.jpg" alt="A penguin standing on a beach">

<img> embeds the image. Its src attribute identifies the image file, while alt supplies text that can stand in for the image’s meaning or function. The WHATWG HTML Standard says to use img with src when embedding a single image resource: WHATWG HTML Standard. MDN’s image element reference also documents the element and its attributes.

img is a void element: it does not wrap content and has no closing tag. In HTML, write <img ...>, not <img>...</img>. At least one of src or srcset is required for the element to identify an image resource. For a straightforward image, src is usually the clearest starting point.

Choose the right image path

The browser requests the image separately from the HTML document. Choose a path that matches where the file is stored and how the site is served:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
Path form Example How it resolves
Relative images/photo.jpg From the current HTML document’s URL, using an images folder beneath it.
Root-relative /images/photo.jpg From the root of the current website.
Absolute URL https://example.com/photo.jpg At the complete address given in the URL.

For example, if a page is served at https://example.com/articles/guide, the relative path images/photo.jpg is resolved in relation to that page’s URL; it is not a universal path to the site’s image folder. A root-relative path starts at the site root instead. If the page is nested in a directory, check the actual image request address in the browser rather than assuming a relative path starts at the project folder on your computer.

Write useful alt text

Think of alt as a text replacement, not a label announcing that an image exists. Describe the information or action the image contributes in the surrounding context. For an informative photograph, a concise description might be alt="A penguin standing on a beach". Avoid a filename or generic value such as alt="image"; those tell a reader little about what the image conveys.

Informative, decorative, and linked images

  • Informative image: Write a concise description of the content or meaning that matters in context.
  • Decorative image: When the image contributes no information and has no textual equivalent to communicate, use an empty value: alt="". That is different from omitting the attribute, because the empty value deliberately marks the image as decorative.
  • Image that is a link: Describe the destination or action the link provides. For example, if clicking the image opens a gallery, use wording such as “Open the photo gallery,” rather than describing only its visual appearance.
<a href="/gallery">
  <img src="gallery-thumb.jpg" alt="Open the photo gallery">
</a>

The same alt text can appear when the image cannot load, so a meaningful alternative helps in both normal use and failure cases. MDN explains the alt attribute and gives guidance for writing image alternatives.

Set dimensions and make an image fit its container

If you know the image’s intrinsic pixel dimensions, include them as integer values in the HTML. They give the browser its aspect ratio before the image finishes downloading, allowing it to reserve space and reducing layout shift:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<img src="photo.jpg" alt="A mountain at sunrise" width="1200" height="800">

Write the actual width and height in pixels as plain integers; do not add px to these HTML attributes. They describe the source image’s proportions, not necessarily the final size at which the image will appear.

For a fluid image that should fit within its container, use CSS:

img {
  max-width: 100%;
  height: auto;
}

max-width: 100% prevents the image from overflowing a narrower container, and height: auto preserves its proportions as it scales. Keep accurate HTML width and height values as intrinsic ratio hints even when CSS controls the rendered size. The MDN width and height guidance explains how reserving the image’s aspect ratio can limit layout shifts.

Serve different image sizes or crops

One src is enough when every visitor should get the same image resource. If you have multiple resolutions of the same image, use srcset to list width candidates and sizes to describe the image’s expected display width. The browser can then choose an appropriate candidate:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 800px"
  width="1600"
  height="1067"
  alt="A mountain at sunrise">

The values such as 400w and 800w are width descriptors: they should match the actual intrinsic widths of those image files. sizes is a description of the image’s likely rendered width at different viewport conditions. It is not a list of image files, and it should reflect your page layout. If those descriptors or the sizing rule do not match reality, the browser may choose an unsuitable candidate.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Use picture for art direction or format alternatives

Use <picture> when the browser should have a choice of different crops or formats, such as a portrait crop at a narrow viewport and a different source elsewhere. Keep an <img> inside it: that element supplies the fallback image and the alternative text.

<picture>
  <source media="(max-width: 600px)" srcset="portrait-crop.jpg">
  <source type="image/avif" srcset="photo.avif">
  <img src="photo.jpg" alt="A mountain at sunrise" width="1600" height="1067">
</picture>
Need Markup to consider
One image resource at all sizes img with src.
The same image offered at different resolutions img with width-descriptor srcset and an appropriate sizes rule.
Different crops or format alternatives picture with one or more source elements and an img fallback.

See MDN’s guidance on responsive images for how candidate selection works and why the fallback img remains part of a picture.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why an image does not appear

When an image is missing, diagnose its request rather than changing markup at random. Open the page in a browser, use the developer tools’ Network panel, reload, and inspect the image request. A 404 indicates the requested address was not found; a blocked request or failed response points to a different problem than an incorrect alt description.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Check the path against the page URL. Confirm whether the image should be referenced relative to the current document or from the site root. Look at the actual requested address in Network tools.
  2. Check the filename and folder. Match spelling, capitalization, extension, and directory location exactly. A path that works on a case-insensitive local computer may fail on a case-sensitive server.
  3. Check URL characters. If a filename contains spaces or other special characters, make sure the URL is encoded correctly; a simpler filename can avoid mistakes.
  4. Check whether the file can be served. Confirm the image exists at the requested address, the server is available, and the file is accessible rather than blocked by server permissions or another request restriction.
  5. Check srcset and sizes if only the wrong version appears. Make sure each width descriptor matches its image’s actual intrinsic width and that sizes represents the layout at the relevant viewport.
  6. Check the dimensions and CSS if the layout jumps or the image seems distorted. Use the correct intrinsic dimensions and preserve the ratio with height: auto.

A broken-image icon is often a path, filename, encoding, permission, or server-availability issue. If the image appears but assistive technology gives an unhelpful result, review the purpose of the image and rewrite its alt value to communicate that purpose. More background on image requests and URLs is available in MDN’s HTML images guide.

Or skip the browser setup

Once you have put the image on a page and made that page reachable by URL, you can use a screenshot API to capture the rendered result without setting up a browser automation stack. ScreenshotNeo is a website screenshot API and MCP server. Its one-call endpoint can return an image or PDF; the API documentation is at ScreenshotNeo docs.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Replace the sample target URL with the URL of your own page after publishing it, and replace YOUR_API_KEY with your key. For a Python request, use:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

For Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
  • Before capture, it accepts cookie or consent banners as a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. Response headers identify the page verdict and billing status.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents, including Claude, Cursor, and other MCP clients.
  • The free plan includes 1,000 shots per month with no card. Paid plans start at $5 for 3,000 shots; yearly billing gives two months free, and every feature is on every plan.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.