Skip to content

Beginner · HTML basics · topic 8 of 64 · 30 min

Images, video and audio

Explanation

The <img> element shows a picture, and its alt text describes the picture for people who cannot see it. Width and height stop the page from jumping while images load, and lazy loading saves mobile data. Video and audio elements play media with built-in controls.

The img element

<img> is a void element, so it has no closing tag. The src attribute (source) gives the path or URL of the image file. The alt attribute (alternative text) describes the image. Screen readers read it aloud, and the browser shows it if the image fails to load.

html
<img
  src="images/masala-dosa.jpg"
  alt="Crispy masala dosa with coconut chutney and sambar"
  width="600"
  height="400"
  loading="lazy">
A complete image: source, description, size and lazy loading
Think of it like this: Writing alt text is like describing a photo to a friend on a phone call. You would not say "image dot jpg". You would say "Fresh samosas on a steel plate with green chutney". If the picture is only a decoration, like a border, you would not mention it at all.

Writing good alt text

ImageGood alt
A product photoalt="Red cotton kurta, front view"
A logo that links to the home pagealt="Sharma Kirana home"
A chartA short summary: alt="Sales doubled from 2024 to 2026"
A decorative line or patternalt="" (empty, so screen readers skip it)

An empty alt="" says "this image is decoration, skip it". A missing alt attribute is different: some screen readers then read out the file name, like "IMG underscore 2034 dot jpg". So always write the alt attribute, even when it is empty.

Width, height and lazy loading

The width and height attributes are plain numbers in pixels, without "px". They let the browser reserve the right space before the image arrives, so the text below does not jump. That jump is called layout shift, and it makes people tap the wrong thing. CSS such as max-width: 100%; height: auto; can still make the image shrink on phones.

loading="lazy" tells the browser to wait until the image is close to the screen before downloading it. This saves data on long pages. Do not lazy-load the big image at the very top of the page, because the visitor needs it at once.

Figure and figcaption

html
<figure>
  <img src="thali.jpg" alt="South Indian thali on a banana leaf" width="800" height="533">
  <figcaption>Our Sunday special thali, ₹&nbsp;180</figcaption>
</figure>
figure groups an image with its caption

Responsive images and modern formats

srcset lists several sizes of the same image, and sizes tells the browser how wide the image will show. The browser then downloads the smallest file that still looks sharp. The <picture> element lets you offer newer formats first, with an older format as a fallback.

html
<picture>
  <source srcset="kurta.avif" type="image/avif">
  <source srcset="kurta.webp" type="image/webp">
  <img src="kurta.jpg" alt="Red cotton kurta, front view" width="600" height="800">
</picture>

<img src="shop-800.jpg"
  srcset="shop-400.jpg 400w, shop-800.jpg 800w, shop-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 600px"
  alt="Front of the shop with fruit crates" width="800" height="600">
picture offers AVIF and WebP first; srcset offers three sizes

WebP and AVIF are modern image formats. They are often much smaller than JPEG or PNG at the same quality, and all current browsers support them. Smaller images mean faster pages on mobile data.

Video and audio

html
<video controls width="640" height="360" poster="recipe-poster.jpg" preload="metadata">
  <source src="recipe.mp4" type="video/mp4">
  <track kind="captions" src="recipe-en.vtt" srclang="en" label="English">
  <a href="recipe.mp4">Download the recipe video</a>
</video>

<audio controls src="bhajan.mp3"></audio>
controls shows play, pause and volume; the link inside is a fallback
Careful: Browsers block autoplay with sound, and visitors dislike it. autoplay only works together with muted. Big videos also use a lot of mobile data, so use preload="metadata" and a poster image.
Tip: Shrink photos before you upload them. A free tool such as squoosh.app can turn a 4 MB phone photo into a WebP of about 150 KB that looks the same on screen.

Watch the lesson

Images, video and audio · 1/6

<img src="..." alt="...">

Pictures make a page come alive. The img element shows them, and it has no closing tag.

Prefer a video by a teacher? Find free videos on “html images alt text figure picture srcset tutorial for beginners” on YouTube ↗

In real life: A food delivery menu

A food delivery app shows dozens of dish photos on one long page. Each photo has alt text like "Paneer butter masala in a copper bowl", width and height so the list does not jump while scrolling, and loading="lazy" so photos further down load only when needed. The photos are served as WebP or AVIF through <picture>, and a short recipe video uses controls, captions and a poster image instead of autoplay.

Practice (0/3 done)

Task 1 · Code · HTML

Improve this dish photo. Add alt text that describes the photo, width="600" and height="400" (plain numbers), and loading="lazy".

Task 2 · Code · HTML

Put the thali photo and its caption inside a <figure>, and turn the caption paragraph into a <figcaption>. The divider.png image is only decoration: give it an empty alt="".

Task 3 · Code · HTML

This recipe video starts by itself and has no buttons. Remove autoplay, add the controls attribute, keep recipe.mp4 as the source, and put a fallback link "Download the recipe video" inside the <video> element.

Quick quiz

Question 1

1.What is the alt attribute for?

Question 2

2.A small flower pattern is used only as decoration. What alt should it have?

Question 3

3.Why add width and height attributes to an image?

Question 4

4.Which image should NOT use loading="lazy"?

Question 5

5.Which formats are usually smaller than JPEG at the same quality?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Links: connecting pages