IntermediateLesson 18 of 306 min read

HTML iframes: embedding videos, maps and other content

Show YouTube videos, maps and other external content on your page with the iframe element, safely and responsively.

In this lesson you will

  • Embed YouTube videos and maps
  • Use title, loading and allow attributes
  • Make embeds responsive
  • Know the security basics

About 12 minutes

Windows to other websites

Priya now wants a map of her shop and her YouTube video about sourdough on the same page. She does not want to upload the video or draw a map. She wants to show content that already lives somewhere else. HTML provides a window for exactly that: the <iframe> element, short for inline frame.

How an iframe works

<iframe src="https://example.com/page"
        width="600" height="400"
        title="Description of the content">
</iframe>

The src is the address of the page to show. Width and height set the size of the window. The title is not optional in practice: screen readers read it aloud to describe what is inside the frame, so write something meaningful such as “Map of Priya’s Bakery”.

Embedding a YouTube video

Every YouTube video has a Share button, and under it an Embed option which gives you ready-made code. It looks like this:

<iframe width="560" height="315"
        src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
        title="How to feed a sourdough starter"
        loading="lazy"
        allow="accelerometer; encrypted-media; picture-in-picture"
        allowfullscreen>
</iframe>
  • Replace VIDEO_ID with the part of the video address after v=.
  • The domain youtube-nocookie.com is the privacy-friendlier version that sets fewer tracking cookies until the video plays.
  • loading="lazy" delays loading the frame until the visitor scrolls near it, which speeds up the first view of the page.
  • allowfullscreen lets people enlarge the video.

Embedding a map

Map services give you embed code in the same way. In Google Maps, search for your address, click Share, choose Embed a map and copy the code. You end up with an iframe whose source points at the map.

<iframe src="https://www.google.com/maps/embed?pb=..."
        width="600" height="450" style="border:0"
        loading="lazy" referrerpolicy="no-referrer-when-downgrade"
        title="Map showing the location of Priya's Bakery"></iframe>

Give this iframe a proper title and make it lazy-loaded. It shows a small example page.

<iframe src="https://example.com" width="400" height="200"></iframe>

Add title=”Example website” and loading=”lazy” to the opening tag.

<iframe src="https://example.com" width="400" height="200" title="Example website" loading="lazy"></iframe>

Making embeds responsive

An iframe with a fixed width of 560 pixels will overflow a narrow phone screen. The modern fix is two lines of CSS.

.video-wrap { max-width: 800px; margin: 0 auto; }
.video-wrap iframe { width: 100%; height: auto; aspect-ratio: 16 / 9; border: 0; }
<div class="video-wrap">
  <iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
          title="Sourdough starter guide" allowfullscreen></iframe>
</div>

The aspect-ratio property keeps the frame in the correct shape however wide it becomes, so the video is never squashed.

Security: only embed what you trust

An iframe loads someone else’s page inside yours. That is powerful, and it deserves care.

  • Embed content only from sources you trust. A hijacked page inside your frame could show your visitors something harmful.
  • The sandbox attribute restricts what the framed page may do, such as running scripts or opening popups. You can relax it selectively, for example sandbox="allow-scripts allow-same-origin".
  • Some sites forbid being framed at all and will show an empty box or an error. That is their choice and cannot be fixed from your side.
  • Every embed adds weight and tracking. Add only what really helps the reader.
Common mistake

Do not frame whole other websites to make your page look bigger. It frustrates visitors, it may break the other site’s rules, and search engines treat the content as belonging to the other page.

Other kinds of embedded content

  • <embed> and <object> can display PDFs and other documents, but most of the time a simple link to the file is friendlier on phones.
  • Social posts, podcast players, forms and calendars usually give you an embed snippet to paste. The structure is nearly always an iframe or a small script.
  • The <picture> and <svg> elements are not embeds of other pages, but they bring responsive images and vector graphics into your document. You will meet them in the CSS course.
Click to load

Pages with many videos can feel slow. A good trick is to show a thumbnail with a play button and load the iframe only when someone clicks. JavaScript courses show how.

Write an iframe that embeds a YouTube video with the ID dQw4w9WgXcQ using the nocookie domain, a title, lazy loading and allowfullscreen.

<!-- your iframe here -->

The address is https://www.youtube-nocookie.com/embed/ followed by the ID.

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" title="Example video" loading="lazy" allowfullscreen></iframe>

Debugging an embed that will not show

  1. Open the browser’s developer tools and look at the Console tab. A message mentioning “X-Frame-Options” or “frame-ancestors” means the other site refuses to be embedded.
  2. Check the address. Embed addresses often differ from the normal page address. YouTube uses /embed/ instead of /watch?v=.
  3. Use https addresses. A secure page cannot load an insecure frame, and browsers will block it.
  4. Turn off ad blockers and privacy extensions for a moment to see if one of them is the cause.
  5. Make sure the width and height are not zero, and that the frame is not hidden by CSS.

Most embed problems are one of those five. Working through them in order solves nearly every case in a few minutes.

Privacy and consent

An embed from a large platform can load scripts and cookies from that platform as soon as your page opens. In some regions the law asks you to get consent before that happens. A simple, kind approach is to use the privacy-enhanced embed addresses where they exist, and to load heavy embeds only when the visitor clicks. Your cookie policy should list the services you embed, and your visitors will thank you for the honesty.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does an iframe do?

  1. Draws a border
  2. Shows another page inside your page
  3. Plays audio
  4. Creates a table

An iframe is a window onto another document.

Why should every iframe have a title?

  1. For search ranking only
  2. Screen readers announce it to describe the content
  3. It sets the colour
  4. It is required for the video to play

The title tells assistive technology what the frame contains.

What does loading="lazy" do?

  1. Slows the video
  2. Loads the frame only when it is near the visible area
  3. Mutes the sound
  4. Hides the frame

Lazy loading saves bandwidth and speeds up the first view.

Which CSS property keeps an embed in 16:9 shape on any width?

  1. aspect-ratio
  2. opacity
  3. float
  4. z-index

aspect-ratio plus width:100% makes a fluid embed.

What is the sandbox attribute for?

  1. Adding sand textures
  2. Restricting what the framed page may do
  3. Resizing the frame
  4. Playing music

Sandbox limits scripts, popups and other powers of the embedded page.

Summary

  • An iframe shows another page inside yours.
  • Always give it a meaningful title.
  • Lazy loading and aspect-ratio keep embeds fast and fluid.
  • Embed only trusted content and consider sandbox.

Next lesson

Great pages work for everyone. Next: accessibility, the skill that separates good developers from great ones.

Questions

Why is my iframe empty?

The other site may not allow framing, or a browser extension may be blocking it. Check the browser console for a message about frame options.

Do iframes hurt SEO?

Their content counts for the other page, not yours. Use them for helpful extras, not for the main content you want to rank.

Is it better to embed or link?

Embed when watching the content in place helps the reader. Link when you only want to point them to more, since links add no weight to your page.

Progress is saved in this browser.