CHAPTER 19 · HTML

Images: choose a source, write alt text and set dimensions

A picture can explain a page, show an object or simply decorate a section. Learn how to display your own image file and describe its purpose so the page still makes sense when someone cannot see it.

Download example files (ZIP)

The page and image are separate files

Putting an img element in HTML does not copy the picture into the HTML file. The src attribute gives the image address, and the browser requests or reads that resource separately.

Use the original practice images supplied in the example ZIP. Place the images folder beside the HTML files. The desk drawing is images/study-desk.svg. SVG is a vector image format: shapes are described so that the image can scale clearly. You do not need to write SVG for this exercise.

Example fragment — use inside a complete program
html-practice/
  study-image.html
  images/
    study-desk.svg
    practice-mark.svg

The src path follows the same folder rules as href. Keep the filename, letter case and extension exact. An HTML page renamed to .png does not become an image.

Read the img syntax

HTML fragment — part of a document
<img src="images/study-desk.svg" alt="A notebook and pencil on a study desk" width="360" height="220">
PartWhat it supplies
imgThe image element
srcThe image resource address
altA text replacement appropriate to the image's purpose
widthIntended width in CSS pixels, written as an integer
heightIntended height in CSS pixels, written as an integer

Img is a void element. It has no content between opening and closing tags and does not use a closing img tag. Put any visible caption in a separate suitable element, covered in the next chapter.

Complete program: study-image.html

study-image.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Study Desk Image Practice</title>
</head>
<body>
  <h1>Prepare a quiet study place</h1>
  <p>The drawing shows the two items for today's writing task.</p>
  <img src="images/study-desk.svg" alt="A notebook and pencil on a study desk" width="360" height="220">
</body>
</html>
Browser previewDocument title: Study Desk Image Practice

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: the supplied desk drawing appears below the paragraph. The width and height describe its 360 by 220 display size in CSS pixels before any later styling changes it. One CSS pixel does not necessarily equal one physical screen pixel on every device.

Write alt for the picture's job

Alt text is replacement text, not a place to add every keyword or the filename. Ask, “If the picture were unavailable, what would the reader need instead?”

For an informative picture, write a short, useful description. For a chart, the important information may need a nearby explanation or data table as well as a brief alt. A long technical chart cannot always be explained in a few words.

Avoid starting every alt with Image of. Reading tools can already identify an image. Do not depend on alt appearing as a hover tooltip; that is not its purpose. A title attribute is not a replacement for alt or for essential visible instructions.

Image purposeAppropriate approach
Shows items needed for a taskName those items briefly
Pure decoration, with no information to addUse alt=""
Acts as the only content of a linkDescribe the link's purpose or destination
Contains essential wordsProvide the words in equivalent text; prefer real HTML text where possible
Contains complex dataProvide an accessible fuller explanation nearby

The same picture can need different alt text in different contexts. A desk photo in an equipment guide may identify equipment; the same photo beside a heading might be decorative if it adds no useful information.

Choose replacement text by purpose
  1. 1Does the image add information? Describe it
  2. 2Is it only decoration? Use empty alt
  3. 3Is it the only link content? Name the destination
  4. 4Is it complex? Add a fuller text explanation

Do not use a filename as a substitute for thinking about the image purpose.

Empty alt is different from missing alt

An empty value, alt="", deliberately marks a decorative image as having no replacement information. Omitting alt gives no such clear instruction; tools may announce a filename or URL instead. For our authored beginner pages, always make a deliberate choice.

The small practice mark below decorates a sentence whose words already communicate the full message. It therefore uses empty alt.

Complete program: decorative-image.html

decorative-image.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Decorative Practice Mark</title>
</head>
<body>
  <h1>Your practice reminder</h1>
  <p><img src="images/practice-mark.svg" alt="" width="40" height="40"> Keep one checked example in your notebook.</p>
  <p>The words carry the reminder; the small mark only decorates it.</p>
</body>
</html>
Browser previewDocument title: Decorative Practice Mark

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: a small mark appears beside a complete text reminder. Removing that decorative picture would not remove the instruction. Do not use empty alt for a picture that supplies information absent from the surrounding words.

Dimensions, shape and file size

Write width="360", not width="360px" in these HTML attributes. Use the original aspect ratio, the relationship between width and height. A 360 by 220 image forced to 360 by 100 can look flattened.

Supplying both dimensions lets the browser reserve appropriately proportioned space before the image arrives. This helps reduce unexpected layout movement. CSS can later make an image fit a narrow screen while keeping its proportions.

Smaller display dimensions do not necessarily mean a smaller download. A very large photo displayed as a tiny thumbnail may still transfer its full file. Resize or compress the actual resource appropriately. Later lessons cover responsive image selection with srcset and picture.

Loading and decoding

Loading="lazy" allows the browser to defer fetching an image until it is near the visible area. Use it for suitable images farther down a page, not automatically for the main picture readers need immediately. The default is eager loading.

Decoding="async" is a hint about decoding and presentation, not a request for lazy fetching. Fetchpriority="high" is a relative priority hint and should be reserved for a genuinely important image when warranted. It does not replace loading or guarantee an exact request order. These are optional hints; correct src, suitable alt and a sensible resource are the first priorities.

HTML fragment — part of a document
<img src="images/study-desk.svg" alt="A notebook and pencil on a study desk" width="360" height="220" loading="lazy" decoding="async">

Problem and solution: the picture is broken

Problem: the browser shows a broken-image sign or replacement text.

Solution: first open the image file directly to check that it exists and is a real supported image. Then compare the HTML file's location with src. Check case, extension and folder spelling. Reload after saving. Do not remove alt to hide a broken image; fix the resource and keep appropriate replacement text.

Common mistakes and useful tips

  • Using a path from your own Downloads folder in a public page.
  • Writing a filename such as IMG_0003.png as the alt text.
  • Omitting alt for decoration instead of deliberately using an empty value.
  • Making important instructions available only inside a picture.
  • Writing CSS units inside the numeric width and height attributes.
  • Stretching the image by choosing unrelated dimensions.
  • Lazy-loading every image, including the first important image.
  • Copying pictures from websites without permission. Use your own or properly licensed assets.

Practice questions

  1. What does src contain?
  2. Why does img not need a closing tag?
  3. How does alt="" differ from leaving alt out?
  4. Are width="360px" and width="360" both correct HTML attribute spellings here?
  5. Does shrinking the displayed width always reduce the downloaded file size?
  6. Build a page with one informative image and one decorative mark. Explain each alt choice and then temporarily change a source filename to observe a missing image.
Check your practice answers
  1. The image resource address.
  2. Img is a void element and has no child content.
  3. Empty alt deliberately supplies no replacement information for decoration. Missing alt does not clearly convey that intention.
  4. Use the integer spelling width="360". Units belong in appropriate CSS values, not this numeric attribute.
  5. No. The browser may download the same original file regardless of its displayed size.
  6. Use the supplied image files, meaningful replacement text for the informative picture and empty alt for the decoration. Restore the correct src after checking the failure case.

Interview questions

What makes good alt text?

It replaces the image's useful purpose in its current context. It is concise when possible, avoids irrelevant details and does not merely repeat a filename.

Why provide image width and height?

They describe intended dimensions and help the browser reserve space in the correct proportions before loading. They do not themselves compress the source file.

How do loading and decoding differ?

Loading controls whether fetching may be deferred. Decoding is a hint about how image decoding relates to presentation. Neither fixes an incorrect source or unsuitable replacement text.

When should an image use empty alt?

When it is purely decorative or adds no information beyond equivalent nearby content. Do not hide a meaningful image or an otherwise unnamed functional control with empty alt.

Quick revision

  • Img points to a separate source resource and has no closing tag.
  • Choose alt by the picture's purpose.
  • Empty alt means intentional decoration; missing alt is different.
  • Keep dimensions proportional and resource sizes sensible.
  • Use loading hints only when their behaviour suits the page.