Lesson 46 of 51 · Module 7, lesson 6 of 11 · 4 min
Embedding pages and YouTube videos
Show a video or map from another site in your page, and test it the right way.
What you'll learn
- How to show another page inside yours with
<iframe>- How to embed a YouTube video
- Why embeds only work on a real website — and how to test them on your computer
Why this matters
Sometimes the best content is on another website: a video, a map, a calendar. An iframe (inline frame) is a window in your page that shows another page. Video sites and map services give you ready-made iframe code — you only need to understand it and make it accessible.
The <iframe> element
<iframe src="https://www.example.com/page" title="What this frame shows" width="400" height="300" loading="lazy"></iframe>
| Attribute | Job |
|---|---|
src | The address of the page to show |
title | Required for accessibility. A screen reader reads it to say what the frame contains. |
width, height | The frame's size |
loading="lazy" | Load it only when the reader scrolls near it |
Many websites do not allow their pages to be shown inside other sites. Their frame then stays empty. Services that are made for embedding — video and map sites — do allow it, and give you the code.
Embedding a YouTube video
- On YouTube, open the video and click Share, then Embed.
- Copy the code. It looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>A short film</title>
</head>
<body>
<h1>A short animated film</h1>
<iframe
width="320" height="180"
src="https://www.youtube-nocookie.com/embed/aqz-KE-bpKQ"
title="Big Buck Bunny, an animated short film by the Blender Foundation"
allow="fullscreen; picture-in-picture"
loading="lazy">
</iframe>
<p>Big Buck Bunny © Blender Foundation, shared under a Creative Commons licence.</p>
</body>
</html>
- Paste it into your page, and check three things:
title: YouTube writes the video's name here. Make sure it describes the video.widthandheight: change them to fit your page, keeping the same shape (16 wide to 9 high).youtube-nocookie.com: this privacy-friendly address (YouTube's own) sets fewer tracking cookies until the visitor presses play. You can changeyoutube.comtoyoutube-nocookie.comin the code.
What you'll see — on a real website or with Live Server: the video's first frame with a red play button.
Embeds need a real website
We tested this carefully, because it confuses many beginners:
| How you open the page | The YouTube embed |
|---|---|
Double-click the file (file:///…) | Does not play. YouTube shows "Error 153 — Video player configuration error". |
| In the Try code playground | Does not play. The playground shows a message explaining why. |
With Live Server (http://127.0.0.1:5500/…) or on a real website | Plays. |
YouTube needs to know which website the video is shown on, and a file opened from your disk is not a website. So test embeds with Live Server (see Video with captions).
Maps and other services
Map services work the same way: find Share → Embed a map, copy the iframe code, and check its title, size and loading="lazy".
Try it yourself
- Create
film.htmlin your folder with the complete example. - Double-click it: you see Error 153.
- Open it with Live Server: the video appears.
- Change the
titleto a poor one, such as "video". Imagine hearing only "video, frame" with a screen reader. Put the good title back.
Common mistakes
- No
titleon the iframe. Screen reader users do not know what it is. Fix: always add a cleartitle. - Testing a YouTube embed by double-clicking the file. You get Error 153. Fix: test with Live Server.
- Embedding a site that does not allow it. The frame stays empty. Fix: use the service's own embed code, or link to the page instead.
- Too many embeds on one page. Each one loads a whole other page, which is slow on phones. Fix: few embeds, all with
loading="lazy".
Check yourself
- Which attribute must every iframe have for accessibility?
- You open your page by double-clicking and the video shows Error 153. Why, and what do you do?
- What does
loading="lazy"do on an iframe?
Answers
title.- A file opened from disk is not a website, and YouTube refuses to play there. Open the page with Live Server.
- The frame loads only when the reader scrolls near it.
Next: an optional lesson on small drawings made of code — inline SVG icons.

