Thuta Learning
HTML
IntermediateWeb DevelopmentBeginner

Images

HTMLLesson 1425 min

What you'll walk away with

  • Write correct alt text for informative images, images inside links and decorative images
  • Explain why width and height attributes prevent layout shift
  • Make images shrink to fit the screen with max-width: 100% and height: auto
  • Add a caption with figure and figcaption and tell it apart from alt text

Explain it simply

Explain it simply

The <img> element puts a picture on the page. src says where the image file is, and alt is the text replacement for people who cannot see it. Think of a radio sports commentator: listeners cannot see the match, so the commentator describes what matters. Alt text is that commentator.

Screen reader users, people on slow connections whose images fail to load, and search engines all read the alt. width and height tell the browser how much room to save for the picture before it has even arrived.

Problem → Why → Solution

The problem: images with no alt and no size

An online shop's page starts with a banner, a product photo and a cart icon link. On a phone, the whole page jumps down the moment the banner arrives, and a user reaching for "Buy now" taps something else. A screen reader user hears only "IMG_2041.jpg" and "link, cart-icon.png".

shop.html
<img src="banner.jpg">
<img src="IMG_2041.jpg" alt="image">
<a href="/cart"><img src="cart-icon.png"></a>
<p><button>Buy now</button></p>

Why does this happen?

Without width and height, the browser learns the image's size only after downloading it, and only then makes room, so everything below jumps; this is called layout shift. Without alt, most screen readers fall back to reading the file name, and alt="image" says nothing useful either. A link whose only content is an image without alt has no name at all.

Core concept: alt describes the image's job

<img> is a void element with no closing tag. src is the path or URL of the image file, and alt must always be present. What goes in the alt depends on the image's job. For an informative image, such as a photo or a chart, write the information the picture gives, briefly. Do not start with "Image of" or "Photo of"; the screen reader already announces that it is an image. For an image inside a link or button, the alt describes the action, not the drawing: a cart icon's alt is "View cart".

For a purely decorative image, write an empty alt: alt="". It tells screen readers to skip the image. That is different from leaving the alt attribute out, which often makes them read the file name aloud. When an image fails to load, the browser shows the alt text where the picture should be.

Write width and height as plain pixel numbers with no unit (width="600" height="400"). From these two numbers the browser computes the aspect ratio and reserves the space before the image arrives, so nothing shifts. You do not remove these attributes for responsive design; you add img { max-width: 100%; height: auto; } in CSS, and on narrow screens the image shrinks without distorting. So the answer is both: HTML provides the ratio and CSS controls the size.

loading="lazy" tells the browser to download images further down the page only when the user scrolls near them, saving data and time. Do not lazy-load the main image at the top of the page, or it will appear late.

Mental model: ask what the image does
  1. Informs? Put the information in alt
  2. In a link? alt names the action
  3. Decoration only? alt=""
  4. Always width/height + height: auto

If you had to describe the picture to a friend over the phone, what would you say? That is the alt.

General pattern

html
<img src="path/to/file.jpg" alt="What the image tells the reader"
     width="W" height="H">
<a href="/destination"><img src="icon.svg" alt="Action name" width="24" height="24"></a>
<img src="decoration.svg" alt="" width="W" height="H">
<img src="far-down.jpg" alt="..." width="W" height="H" loading="lazy">
<style>
  img { max-width: 100%; height: auto; }
</style>

Every img has src, alt, width and height. The alt content depends on the image's job, and one CSS rule makes every image responsive.

Example: three kinds of image on a team page

The team photo sits in a figure with a caption, joined by a decorative leaf icon and a lazy-loaded image further down. Put the image files in the same folder as the HTML file to try it, then rename one file and watch its alt text appear instead.

team.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Our team</title>
  <style>
    img { max-width: 100%; height: auto; }
  </style>
</head>
<body>
  <h1>Our team</h1>
  <figure>
    <img src="team-photo.jpg" alt="Our team working together" width="600" height="400">
    <figcaption>The Golden Leaf team at the Yangon shop.</figcaption>
  </figure>
  <p>
    <img src="leaf.svg" alt="" width="24" height="24">
    We have been serving tea since 2019.
  </p>
  <img src="shop-front.jpg" alt="The shop's green front door under a hanging tea sign"
       width="600" height="400" loading="lazy">
</body>
</html>

Code breakdown

  1. Line 7img { max-width: 100%; height: auto; }

    The image never grows wider than its container, and height: auto shrinks the height in proportion. Without height: auto the height attribute's 400 would remain and the photo would be squashed.

  2. Line 13<img src="team-photo.jpg" alt="Our team working together" width="600" height="400">

    width and height give a 3:2 ratio so the space is reserved before the photo arrives, and alt states what the photo shows.

  3. Line 14<figcaption>The Golden Leaf team at the Yangon shop.</figcaption>

    The caption is visible to everyone and adds context. Do not repeat the alt word for word, or screen reader users hear it twice.

  4. Line 17<img src="leaf.svg" alt="" width="24" height="24">

    The leaf is decoration and carries no information, so alt="". Screen readers skip it and read only the sentence.

  5. Lines 20-21<img src="shop-front.jpg" alt="The shop's green front door under a hanging tea sign" width="600" height="400" loading="lazy">

    Further down the page, so loading="lazy" delays the download until the user scrolls near it. A tag with many attributes can be split over two lines.

You should see
Under the "Our team" heading you see the 600×400 team photo with its caption, then a small icon beside "We have been serving tea since 2019.", and finally the shop-front photo. On a narrow screen such as 320px the photos shrink and keep their proportions. If team-photo.jpg is missing, the alt text "Our team working together" appears in its place.

An alt decision table, and the squashed-image bug

Imagealt
A photo of the shop frontA short sentence on what matters in it
The logo inside a home-page link"Golden Leaf home"
A link containing only a cart icon"View cart"
A decorative icon beside a headingalt=""
An image the nearby text already explainsalt="" (no repetition)

Testing a 600×400 image on a 320px-wide screen in headless Chromium, max-width: 100% alone gave a squashed 304×400 box, while adding height: auto gave a correctly proportioned 304×203. Even before the image loaded, width and height made the browser reserve that 304×203 space.

squash.html
<style>
  /* On a 320px screen: 304 x 400, squashed */
  .wrong { max-width: 100%; }
  /* On a 320px screen: 304 x 203, in proportion */
  .right { max-width: 100%; height: auto; }
</style>
<img class="wrong" src="team-photo.jpg" alt="Our team, squashed"
     width="600" height="400">
<img class="right" src="team-photo.jpg" alt="Our team, in proportion"
     width="600" height="400">

In real projects

Where this shows up in real projects

E-commerce sites often make the alt field for every product photo mandatory in the CMS, blocking publication when it is empty. Tools such as Google Lighthouse report images without alt as an accessibility problem, and layout shift caused by missing width and height as a performance problem (Cumulative Layout Shift). That is why the image components of most frameworks insist on width and height.

News sites place every photo in a figure and put the photographer's name and context in the figcaption. Long articles and galleries lazy-load the images below the first screen so the page opens quickly on phones. Social media platforms let people add alt text when uploading a picture, and that text ends up in the HTML alt attribute.

Common mistakes

Common mistakes

1. Missing or useless alt

Without alt, screen readers tend to read the file name, such as "IMG_2041.jpg", and alt="image" or alt="photo" says nothing. Write the information the image gives, or alt="" if it is decoration.

2. Removing width and height for responsiveness

Removing width and height means the browser no longer knows the ratio, so layout shift comes back. Keep the attributes and add max-width: 100%; height: auto; in CSS.

3. Forgetting height: auto

With max-width: 100% alone, only the width shrinks on a narrow screen while the height stays at the attribute value, squashing the picture. Always pair it with height: auto.

4. Lazy-loading the main image at the top

Giving the hero image loading="lazy" makes the browser fetch it late, slowing the first screen the user sees. Use lazy only for images further down.

Best practices

Best practices

  • After writing alt text, read the page using only the alt text and check that nothing important is lost.
  • Do not ship image files much larger than their display size; a 4000px photo shown at 600px wastes data.
  • Choose SVG for logos and icons, and JPEG or WebP for photos.
  • For a complex image such as a chart, put the key point in the alt and the detailed data in nearby text or a table.

Mini challenge

Mini challenge: a product card

Write a product card for a tea set. Every image needs an alt that fits its job, and every image must be responsive.

  • Product photo with an informative alt, width and height
  • A link containing only a cart icon, with the action in its alt
  • A decorative divider image with alt=""
  • A gallery image in figure/figcaption with loading="lazy", all made responsive by one CSS rule
product.html
<!-- Place inside the <body> of a full page -->
<style>
  /* TODO: one rule that makes every image shrink in proportion */
</style>

<h1>Celadon tea set</h1>
<!-- TODO: product photo (informative alt, width, height) -->

<!-- TODO: a link to /cart whose only content is an icon image -->

<!-- TODO: a decorative divider image -->

<!-- TODO: figure with a lazy-loaded gallery image and a figcaption -->

Expected: All images appear and shrink in proportion at phone width. A screen reader describes the product photo, announces the cart link as "View cart" and skips the divider.

Show hint

For the cart link's alt, ask "what happens when I click this?". Do not give the gallery image the same alt and caption: the alt says what is visible, the caption adds context.

Show solution
product.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Celadon tea set</title>
  <style>
    img { max-width: 100%; height: auto; }
  </style>
</head>
<body>
  <h1>Celadon tea set</h1>
  <img src="tea-set.jpg" alt="A pale green teapot with four matching cups on a tray"
       width="800" height="600">
  <p>
    <a href="/cart"><img src="cart.svg" alt="View cart" width="32" height="32"></a>
  </p>
  <img src="divider.svg" alt="" width="400" height="20">
  <figure>
    <img src="tea-set-detail.jpg" alt="Close-up of a cup's crackled glaze"
         width="800" height="600" loading="lazy">
    <figcaption>Each cup is glazed by hand, so no two are alike.</figcaption>
  </figure>
</body>
</html>

Quick quiz 1 / 3

A link to /cart contains nothing but a cart icon image. What should the image's alt be?

Quick quiz 2 / 3

A small swirl image sits beside a heading purely for decoration. How should you write it?

Quick quiz 3 / 3

A page has <img width="600" height="400"> and the CSS img { max-width: 100%; } only. On a 320px phone the photo looks squashed. Why?

What to remember

  • Every <img> needs src and alt, and the alt describes the image's job.
  • An image in a link gets the action as alt; a decorative image gets alt="".
  • Keep width and height, and make images responsive with max-width: 100%; height: auto;.
  • Lazy-load images further down the page, and write captions with figure and figcaption.

HTML Living Standard — Embedded Content — WHATWG

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Images | Thuta Learning