We’re moving to a new home! Our website is currently in test mode while we update and transfer our content. Some pages and resources may be temporarily unavailable. We’ll be back with all resources shortly. Thank you for your patience.

HTML & CSS | 01.3 Images, Paths and Alternative Text

Lesson objective

Add images using correct paths and meaningful alternative text. Use captions and size images proportionally so they fit different screens.

Learn

01.3 | IMAGES, PATHS AND ALTERNATIVE TEXT

01 | GIVE AN IMAGE A PURPOSE

An image can explain a circuit, show a finished project or set the mood of a page. Before adding it, decide what it contributes. A useful photograph needs different alternative text from a decorative pattern.

This lesson covers how to load an image, find the correct file path, describe it appropriately and keep it proportional on different screens.

02 | THE IMG ELEMENT

<img src="images/robot.jpg" alt="Delivery robot with a four-wheel chassis">

The img element embeds an image. src gives the image location. alt gives a text alternative. img is a void element, so it does not have a closing tag.

The image file is separate from your HTML document. The browser needs access to that file at the specified location. A filename alone does not copy the picture into your website.

03 | ORGANIZE YOUR FILES

first-website/
  index.html
  images/
    robot.jpg
  pages/
    gallery.html

From index.html, use images/robot.jpg. From pages/gallery.html, use ../images/robot.jpg. The two dots move up one folder before entering images.

Use forward slashes, exact filenames and the correct extension. On many servers Robot.jpg and robot.jpg are different files. A path such as C:/Users/Student/Desktop/robot.jpg refers to your computer and will not work for other visitors.

<img src="images/robot.jpg" alt="Our delivery robot">
<!-- From pages/gallery.html: -->
<img src="../images/robot.jpg" alt="Our delivery robot">

04 | WRITE USEFUL ALTERNATIVE TEXT

Alternative text provides the image’s meaning when a person cannot see it, including when using a screen reader. Describe what matters in the context rather than listing every visible detail.

<img src="images/robot.jpg" alt="Delivery robot carrying a small parcel on its rear platform">

“Image” or “picture of a robot” may add little. In a construction guide, mention the relevant component or arrangement. In a gallery, a concise description of the finished model may be enough.

A genuinely decorative image should have an empty alternative: alt="". Keep the attribute; omitting it is not the same as explicitly marking the image decorative.

<img src="images/decorative-divider.png" alt="">

If an image is a link, its alternative text should communicate the link’s destination or action when no other accessible link text supplies it. A complex diagram needs a longer explanation in nearby text as well as a short alternative.

05 | SIZE AND CENTRE AN IMAGE

<img
  src="images/robot.jpg"
  alt="Delivery robot carrying a parcel"
  width="800"
  height="600"
  style="display: block; width: 100%; max-width: 320px; height: auto; margin: 20px auto;"
>

The width and height attributes above describe an example image’s intrinsic 800 by 600 proportions. Use values matching your own image. They help the browser reserve space before it loads.

The small inline CSS example controls display: max-width limits the visible size, width: 100% lets it fit its container, height: auto preserves proportions, and margin: 20px auto centres this block image with vertical space.

Change 320px after reviewing the page. Do not force unrelated width and height values that stretch the image. We will study CSS properly later; for now, keep this reusable pattern.

06 | CAPTIONS AND IMAGE FORMATS

<figure>
  <img src="images/robot.jpg" alt="Delivery robot with a rear parcel platform">
  <figcaption>Our first delivery-robot prototype.</figcaption>
</figure>

figure groups self-contained content, and figcaption supplies its caption. A caption is visible to everyone; alternative text is not a replacement for a useful caption, nor should both repeat identical wording unnecessarily.

  • JPEG: commonly used for photographs.
  • PNG: useful for graphics and transparency.
  • SVG: vector graphics that can scale cleanly, often suitable for diagrams.
  • WebP: a modern format supporting efficient image encoding and transparency.

Choose a suitable format and reasonable file size. Displaying a huge image at 200px does not automatically reduce the bytes downloaded. Resize or compress the source image appropriately, and use images you have permission to publish.

07 | PRACTISE ON YOUR OWN PAGE

  1. Create an images folder beside index.html.
  2. Add a suitable photograph named robot.jpg, or update the code to match your own filename.
  3. Paste the example below into your document body.
  4. Save, open the page and check its appearance.
  5. Make the browser window narrower. Check the image fits and remains proportional.
<h1>Our robot</h1>
<figure>
  <img src="images/robot.jpg" alt="Delivery robot carrying a small parcel" style="display: block; width: 100%; max-width: 320px; height: auto; margin: 20px auto;">
  <figcaption>Our first prototype.</figcaption>
</figure>

The code preview on previous lessons blocks external image files. Use your local browser and your actual image for this task.

08 | DEBUG A MISSING IMAGE

If the image does not load, compare src with the folder layout. Check capitalization, extension, spelling and which folder contains the HTML file. Confirm the image really exists and opens independently.

If the image loads but looks stretched, check the sizing rules. If the picture is meaningful but its alternative text says nothing useful, improve the description.

CHOOSE THE RIGHT MARKUP

Select an element or attribute for each purpose.

09 | CHECK MEANING, THEN TEST

Review your page in two ways. First, read the source: are the headings in a sensible hierarchy, are items inside li elements, and are the tags correctly nested? Then use the browser: can you scan the content, follow the steps and understand where each link leads?

A link can look correct yet point to a missing file. Test every destination. A browser can also recover from malformed markup, so a page appearing readable is not proof of correct structure.

Further reading: MDN: headings and paragraphs and MDN: creating links.

Terminology

Terminology

img

The void element embedding an image.

src

The attribute giving the image location.

alt

The attribute providing a text alternative.

Relative path

A file location resolved relative to the document or base URL.

Parent folder

The enclosing directory, reached using ../ in a relative path.

Decorative image

An image adding no information needed to understand the content; normally use alt="".

Intrinsic dimensions

The image’s own width and height.

Aspect ratio

The relationship between an image’s width and height.

figure

An element grouping self-contained content.

figcaption

The visible caption for a figure.

Responsive image sizing

Sizing that adapts to the available space.

Image optimization

Reducing unnecessary image dimensions or file size while retaining appropriate quality.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which attributes give an image its location and alternative?
2. Which is correct for a decorative image?
3. From pages/gallery.html, which reaches images/robot.jpg in the parent folder?
4. Which statements are correct?
5. Which preserves image proportions when changing width?
6. Which supplies a visible caption?
7. Which are useful image practices?
8. Which is true about img?

WRITE AND EXPLAIN

Answer in your book or code editor before revealing the sample.

1. Write an img element for images/robot.png with meaningful alternative text.

2. Explain why an image works on index.html but not pages/gallery.html when both use images/robot.jpg.

3. Explain the difference between a caption and alternative text.

4. A 4000-pixel-wide image is displayed at 200px. Does that automatically make the download small?

5. How do you avoid stretching a picture?

6. What alternative text should a purely decorative divider have?

BUILD | A PROJECT GALLERY

Create a local gallery with two meaningful project images and one decorative image. Use correct paths, suitable alternative text and a visible caption for each project image. Limit displayed sizes, preserve proportions and test at a narrow browser width.

Ask a partner to explain what the pictures contribute. Review whether the alternative text conveys that same useful information. Keep your HTML and images together in the project folder.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 01.3 Images, Paths and Alternative Text is coming soon. Complete the practical gallery task and keep the HTML file with its images.