Reading history

HTML Audio, Video, Responsive Images and iframes

6 min read 4 views

Learn HTML video and audio with sources and captions, responsive images with srcset, sizes and picture, lazy loading, iframes and figure captions.

HTML can play sound and video directly, with no plugins, using the <audio> and <video> elements. It can also serve the right image for each screen with srcset and <picture>, and embed content from other sites such as YouTube or Google Maps with <iframe>. This lesson covers all of these, along with captions and the attributes that keep media fast and accessible.

In this lesson you will learn

  • The <video> and <audio> elements and their attributes
  • Offering several formats with <source>
  • Adding captions with <track>
  • Responsive images with srcset, sizes and <picture>
  • Embedding with <iframe>, and <figure> captions

This lesson extends HTML Links and Images.

Video and Audio

<!-- Video with controls, a poster image, several formats and captions -->
<video controls width="640" poster="lesson-poster.jpg" preload="metadata">
  <source src="lesson.webm" type="video/webm">
  <source src="lesson.mp4" type="video/mp4">
  <track src="captions-en.vtt" kind="captions" srclang="en" label="English" default>
  <track src="captions-bn.vtt" kind="captions" srclang="bn" label="বাংলা">
  Your browser does not support HTML video. <a href="lesson.mp4">Download the video</a>.
</video>

<!-- Background-style video: must be muted to autoplay -->
<video autoplay muted loop playsinline width="320">
  <source src="clouds.mp4" type="video/mp4">
</video>

<!-- Audio player -->
<audio controls>
  <source src="podcast.ogg" type="audio/ogg">
  <source src="podcast.mp3" type="audio/mpeg">
  Your browser does not support HTML audio.
</audio>
  • controls shows the browser’s play, pause, volume and full-screen buttons. Without it, the video can only be controlled with JavaScript.
  • Several <source> elements let the browser choose the first format it supports. MP4 (H.264) plays almost everywhere; WebM is smaller in many browsers.
  • poster shows an image before playback starts, and preload="metadata" downloads only the length and size, not the whole video, until the user presses play.
  • <track kind="captions"> adds subtitles from a WebVTT (.vtt) file. Captions help deaf viewers, people watching without sound and learners of a language. You can offer several languages, here English and Bangla.
  • Browsers only allow autoplay when the video is muted. loop repeats it, and playsinline stops iPhones from forcing full screen.
  • Text inside the element is shown only by browsers that do not support it, so a download link is a sensible fallback.
  • <audio> works the same way, with MP3 as the most widely supported format.

Responsive Images and Embeds

<!-- Responsive image: the browser picks the best file for the screen -->
<img src="campus-800.jpg"
     srcset="campus-400.jpg 400w, campus-800.jpg 800w, campus-1600.jpg 1600w"
     sizes="(max-width: 600px) 100vw, 50vw"
     alt="Students walking across the university campus"
     width="800" height="450" loading="lazy">

<!-- <picture>: modern format with a fallback -->
<picture>
  <source srcset="logo.avif" type="image/avif">
  <source srcset="logo.webp" type="image/webp">
  <img src="logo.png" alt="Core Tech ED logo" width="200" height="60">
</picture>

<!-- Embedding a YouTube video or a map with <iframe> -->
<iframe width="560" height="315"
        src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
        title="Introduction to HTML"
        loading="lazy"
        allow="accelerometer; encrypted-media; picture-in-picture"
        allowfullscreen></iframe>

<!-- Figure with a caption -->
<figure>
  <img src="chart.png" alt="Bar chart of enrolments by department" width="600" height="300">
  <figcaption>Figure 1: Enrolments by department, 2026</figcaption>
</figure>
  • srcset lists the same image in several widths, with 400w meaning 400 pixels wide. sizes tells the browser how wide the image will be displayed: the full screen width on phones, half of it on larger screens. The browser then downloads the smallest file that looks sharp, saving data on mobile.
  • Always set width and height. The browser reserves the space before the image arrives, so the page does not jump while loading.
  • loading="lazy" delays images and iframes that are below the visible area until the user scrolls near them.
  • <picture> with type serves modern formats like AVIF or WebP to browsers that support them and falls back to the <img>. The alt text goes on the <img>.
  • <iframe> embeds another page. Give it a title for screen readers; the youtube-nocookie.com domain avoids tracking cookies until the video plays.
  • <figure> and <figcaption> attach a visible caption to an image, chart or code sample.

Media Attributes at a Glance

AttributeElementsEffect
controlsvideo, audioShow playback controls
autoplay + mutedvideo, audioStart automatically (sound off)
loopvideo, audioRepeat
postervideoPreview image
preloadvideo, audionone, metadata or auto
loading="lazy"img, iframeLoad when near the viewport
srcset / sizesimg, sourceChoose the best file for the screen

Common Mistakes

  1. Autoplaying video with sound, which browsers block and users dislike.
  2. No captions on spoken video.
  3. Uploading one huge image for all screens instead of using srcset.
  4. Missing width and height, which causes layout shift.

Practice Exercises

Exercise 1. Write an audio player for lecture.mp3 that does not download anything until the user presses play.

Show answer
<audio controls preload="none" src="lecture.mp3"></audio>

Exercise 2. Embed a Google Map in a responsive box that keeps a 16:9 shape.

Show answer
<iframe src="MAP_EMBED_URL" title="Map of our office" loading="lazy"
        style="width:100%; aspect-ratio:16/9; border:0"></iframe>

The CSS aspect-ratio property keeps the height in proportion to the width.

Frequently Asked Questions

Which video format should I use?

Provide MP4 with H.264 video for the widest support. Adding a WebM or AV1 version as an extra <source> saves bandwidth in browsers that support it.

Should I host videos myself or on YouTube?

Short clips can be self-hosted. Longer videos are usually better on a video platform, which handles streaming at different qualities, and you embed them with an iframe.

Summary

  • <video> and <audio> play media natively; add controls.
  • Offer several <source> formats and <track> captions.
  • srcset, <picture>, dimensions and lazy loading make images fast.
  • <iframe> embeds other sites; give it a title.

Next: Web Accessibility Basics.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *