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

html
<iframe src="https://www.example.com/page" title="What this frame shows" width="400" height="300" loading="lazy"></iframe>
AttributeJob
srcThe address of the page to show
titleRequired for accessibility. A screen reader reads it to say what the frame contains.
width, heightThe 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

  1. On YouTube, open the video and click Share, then Embed.
  2. Copy the code. It looks like this:
html
<!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>
  1. Paste it into your page, and check three things:
    • title: YouTube writes the video's name here. Make sure it describes the video.
    • width and height: 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 change youtube.com to youtube-nocookie.com in the code.

What you'll see — on a real website or with Live Server: the video's first frame with a red play button.

A browser window at the address http://127.0.0.1:5500/film.html, served by Live Server, showing the heading "A short animated film" and an embedded YouTube player with the film's first frame and a red play button.

Embeds need a real website

We tested this carefully, because it confuses many beginners:

How you open the pageThe YouTube embed
Double-click the file (file:///…)Does not play. YouTube shows "Error 153 — Video player configuration error".
In the Try code playgroundDoes not play. The playground shows a message explaining why.
With Live Server (http://127.0.0.1:5500/…) or on a real websitePlays.

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

  1. Create film.html in your folder with the complete example.
  2. Double-click it: you see Error 153.
  3. Open it with Live Server: the video appears.
  4. Change the title to a poor one, such as "video". Imagine hearing only "video, frame" with a screen reader. Put the good title back.

Common mistakes

  • No title on the iframe. Screen reader users do not know what it is. Fix: always add a clear title.
  • 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

  1. Which attribute must every iframe have for accessibility?
  2. You open your page by double-clicking and the video shows Error 153. Why, and what do you do?
  3. What does loading="lazy" do on an iframe?

Answers

  1. title.
  2. A file opened from disk is not a website, and YouTube refuses to play there. Open the page with Live Server.
  3. The frame loads only when the reader scrolls near it.

Next: an optional lesson on small drawings made of code — inline SVG icons.

Chat with us on WhatsApp