Learn • Create • Explore

How to Add Images to HTML

Add an accessible, responsive image with a working source path and the right dimensions.

A useful image does more than occupy space on a page. It can show a reader what to look for, explain a step that is difficult to describe in words, or establish the subject of an article. In this guide, you will add a diagram to a cycling page, give it useful alternative text, and make it scale without distorting. You will also learn how to diagnose a broken path and decide when a second image size is worth preparing.

A browser starts with the image URL, fetches the file, and renders it inside its reserved space

Put the image file somewhere predictable

Start with a folder containing your HTML file and an images subfolder. Save an image as park-map.png inside that subfolder. When the page refers to images/park-map.png, the browser resolves that path from the address of the HTML document. If you move the HTML file into another folder, the same text may resolve to a different address. This explains why a path can work on the home page and fail on a deeper page even though the file itself never moved.

A relative path without an initial slash starts from the current document directory. A path that begins with a slash starts at the root of the current website. A full address includes the scheme and hostname and can refer to a different site. Root-relative paths can be convenient on a deployed website, but an HTML file opened directly from disk has no equivalent site root. Keep the file layout simple while learning, and test the actual page address when you deploy.

Avoid copying a file path from your computer’s address bar into production markup. A path on your drive is meaningful to your machine only. Keep the source image with the rest of the project assets and use the public path at which it will be served. Check capital letters, including the extension. A development computer may overlook a case mismatch that a web server treats as a different name.

Build an image that explains a route

This self-contained example embeds a tiny SVG so the live preview does not depend on a separate file. It draws one road with a blue highlighted segment from the station to the park. In your own project, replace the example source with the relative path to the real diagram. The structure, alternative text, and dimensions work the same way.

HTML example
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Station to park cycling route</title>
  <style>
    body { max-width: 42rem; margin: 2rem auto; padding: 0 1rem; font: 1rem/1.6 system-ui; color: #10244a; }
    figure { margin-inline: 0; }
    img { display: block; max-width: 100%; height: auto; border-radius: 12px; }
    figcaption { margin-top: .6rem; }
  </style>
</head>
<body>
  <main>
    <h1>Ride from the station to the park</h1>
    <figure>
      <img width="600" height="300"
        alt="The blue route runs east from the station, then turns north to the park."
        src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 600 300'%3E%3Crect width='600' height='300' fill='%23e4f4fc'/%3E%3Cpath d='M90 225H465V75' fill='none' stroke='%230078d1' stroke-width='15' stroke-linecap='round' stroke-linejoin='round'/%3E%3Ccircle cx='90' cy='225' r='16' fill='%2310244a'/%3E%3Ccircle cx='465' cy='75' r='16' fill='%2310244a'/%3E%3Ctext x='45' y='270' font-size='24' fill='%2310244a'%3EStation%3C/text%3E%3Ctext x='440' y='45' font-size='24' fill='%2310244a'%3EPark%3C/text%3E%3C/svg%3E">
      <figcaption>Follow the blue route and turn left at the junction.</figcaption>
    </figure>
  </main>
</body>
</html>
Try it in the HTML editor

An image element needs no closing tag. Its source tells the browser what to fetch, and its alternative text tells a reader what information matters when they cannot see it. The figure groups the image and its caption. The caption is available to all readers and can add information such as the source or context. Avoid making the caption and the alternative text identical unless repeating those exact words is useful. In this example the alternative text conveys the orientation of the route, while the caption describes the action to take at the turn.

Write alternative text for the job the image does

Ask what the reader would miss if the image disappeared. A portrait next to a staff biography may need the person’s name if the name is not already adjacent. A screenshot in a troubleshooting guide may need to identify the highlighted panel. A decorative flourish that communicates nothing can use an empty alternative so a screen reader can skip it. Omitting the attribute entirely can leave assistive software trying to announce a filename.

If an image is the only content of a link, describe the destination or action. An arrow leading to the previous article needs an accessible name that identifies that destination. The choice changes if the arrow is beside visible words that already do the job. In that case an empty alternative on the decorative icon avoids repeating the same phrase.

For a complex chart or map, short alternative text can identify its purpose, but the complete explanation usually belongs in adjacent prose. A reader may need the route’s street names, the distance, or a list of turns. That information also helps people looking at a small screen or printing the page. Do not overload one attribute with a paragraph if a structured explanation would be easier to use.

The visible caption and alternative text serve different readers and can contain different information

Reserve the space and let the image shrink

The width and height attributes give the browser an aspect ratio before the image finishes loading. Enter the intrinsic dimensions of the file or values with the same ratio. The CSS limits the displayed width to its container and leaves the height automatic. The browser can then display a smaller version without squashing it. If only one dimension changes and the other is fixed by CSS, the content can stretch or crop unexpectedly.

Sizing an image down in CSS reduces its display size but does not make the downloaded file smaller. If you take a high-resolution photograph and render it in a small card, prepare a smaller file using the image converter or resize it in the image editor. Keep a full-quality master outside the public assets. If you edit and save a lossy file several times, each export can introduce more artefacts.

For photographs with several useful file widths, consider a source set with width descriptors and a sizes attribute that describes the intended layout width. The browser can choose a candidate based on that hint, the screen density, and its own resource selection. Describe the actual layout. A hint saying every card fills the viewport can make the browser select unnecessarily large photos even when several cards sit in one row.

Use the image’s proportions to reserve space, then choose a suitable asset width for each layout

Find the cause of a broken image

Open the browser’s network inspector and reload the page. Look for the request corresponding to the image. A not-found response usually points to the address or filename. A successful response can still be unusable if it contains an HTML error document instead of image bytes. Confirm the response type and open the image address directly when it is safe to do so. Changing the extension does not convert the image format. Re-export it using a converter that encodes the target format.

If the request succeeds and the image still does not show, inspect its computed size and relevant style rules. It may have a zero-sized container or be hidden by a responsive rule. A sandboxed editor can also restrict external resources, which is why this example embeds its diagram. Test a real site asset on the intended host as well, especially when another domain serves the files.

Start with one meaningful diagram and no extra requests. Add a responsive photograph only when it contributes to the page. Check the result with the network throttled, at a narrow viewport, and with the image unavailable so you can judge whether the surrounding prose still makes sense. For full attribute details see MDN’s image element reference.

Choose formats and loading priorities deliberately

For a diagram made of simple shapes, an SVG can stay sharp at different display sizes. A photograph is better suited to a raster format such as JPEG or WebP. PNG is useful when lossless detail or transparency matters, for example in a screenshot with fine text. Compare actual exports before choosing. A tiny line drawing and a large photograph have very different sources of file size, and an efficient format cannot compensate for exporting far more pixels than the design needs.

Let the browser load important above-the-fold imagery early. Lazy loading is useful for illustrations farther down a long article, where visitors may never scroll to them. Avoid marking the primary hero image lazy without measuring the effect, because it can delay the first meaningful content. If you want to assess whether an image is helping or slowing down the page, compare the displayed size, transferred bytes, and visual clarity at the same viewport. Measure with an empty browser cache as well as after a repeat visit.

Related tools

Continue learning