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,sizesand<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>
controlsshows 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. postershows an image before playback starts, andpreload="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
autoplaywhen the video ismuted.looprepeats it, andplaysinlinestops 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>
srcsetlists the same image in several widths, with400wmeaning 400 pixels wide.sizestells 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
widthandheight. 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>withtypeserves modern formats like AVIF or WebP to browsers that support them and falls back to the<img>. Thealttext goes on the<img>.<iframe>embeds another page. Give it atitlefor screen readers; theyoutube-nocookie.comdomain 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
| Attribute | Elements | Effect |
|---|---|---|
controls | video, audio | Show playback controls |
autoplay + muted | video, audio | Start automatically (sound off) |
loop | video, audio | Repeat |
poster | video | Preview image |
preload | video, audio | none, metadata or auto |
loading="lazy" | img, iframe | Load when near the viewport |
srcset / sizes | img, source | Choose the best file for the screen |
Common Mistakes
- Autoplaying video with sound, which browsers block and users dislike.
- No captions on spoken video.
- Uploading one huge image for all screens instead of using
srcset. - 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; addcontrols.- 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.
