Embedding videos and external content
Covered here
- The <video> element and its attributes
- The <audio> element
- Embedding YouTube and Google Maps with <iframe>
- Performance and accessibility tips for embeds
Short answer
Use <video> and <audio> for media files you host yourself. Use <iframe> to embed content from other sites, like a YouTube video or a Google Map. Give every iframe a title, add loading="lazy", and never autoplay sound.
The video element
<video controls width="640" height="360" poster="images/resort-tour.jpg" preload="metadata">
<source src="videos/resort-tour.webm" type="video/webm">
<source src="videos/resort-tour.mp4" type="video/mp4">
<track kind="captions" src="videos/resort-tour-en.vtt" srclang="en" label="English">
Your browser does not support video. <a href="videos/resort-tour.mp4">Download the tour</a>.
</video>
controlsshows play, pause, and volume.postershows a still image before playing.<source>offers formats. The browser picks the first it supports.<track>adds captions, which help deaf viewers and people watching on mute.
The audio element
<audio controls src="audio/radio-ad.mp3"></audio>
Good for podcasts and radio ads. Add a text transcript nearby for accessibility.
Embedding YouTube
<iframe width="560" height="315"
src="https://www.youtube.com/embed/VIDEO_ID"
title="Tour of our resort in Tabaco, Albay"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
On YouTube, click Share, then Embed to copy this code. Hosting videos on YouTube saves your bandwidth. Change the title to describe the video, since screen readers announce it.
Embedding Google Maps
<iframe src="https://www.google.com/maps/embed?pb=..."
width="600" height="450"
title="Map to Mayon Bakeshop, Legazpi City"
loading="lazy"
referrerpolicy="no-referrer-when-downgrade"></iframe>
In Google Maps, search your business, click Share, then Embed a map. A map on your contact page helps customers find you. Always write the address in text too, so it can be copied and read by screen readers.
Try it: embed a video
This embed has no title and loads right away. Add a descriptive title and loading=“lazy”. The preview blocks outside content, so check your code.
Live preview
Quick quiz
Common mistakes
- Autoplaying with sound. It startles visitors. Browsers often block it anyway.
- Too many embeds on one page. Each iframe adds weight. Lazy load them.
- Map with no text address. Always write the address in plain text.
- No captions. Many people watch on mute in public.
Your action
Tick each item when you can do it. Your progress saves on this device.
Key terms
- <video>
- Embeds a video file with built-in controls.
- <audio>
- Embeds an audio file.
- <iframe>
- Embeds another webpage inside your page.
- <source>
- Offers a media file in a specific format.
- <track>
- Adds captions or subtitles in WebVTT format.
- Poster
- An image shown before a video plays.
Questions beginners ask
Can I embed a Facebook video?
Yes. Facebook offers an embed code under the video's menu.
Why does autoplay not work?
Browsers only autoplay muted video. Add muted if autoplay is truly needed.
Is iframe bad for SEO?
No, but content inside an iframe belongs to the other site. Add your own text around it.
Related lessons
Need a website built or fixed while you practice? The team at Remar Digital can help.