Aral Code

Links & Media Lesson 20 of 42

HTML Iframes — Embedding YouTube Videos and Maps

Use the iframe element to embed YouTube videos, Google Maps and other pages in your site. Learn title, width, height, lazy loading and safety tips.

An iframe (inline frame) is a window inside your page that shows another web page. It's how websites embed YouTube videos, Google Maps, Facebook posts, Canva designs and Google Forms. Instead of making these things yourself, you borrow a small window from the service that makes them.

The basic iframe

<iframe src="https://www.example.com" width="600" height="400" title="Example website"></iframe>
  • src — the address of the page to show inside the frame.
  • width and height — the size of the window.
  • title — a short description of what's inside. Screen readers announce it, so don't skip it.

Embedding a YouTube video

You don't write YouTube iframes by hand. YouTube writes it for you:

  1. Open the video on YouTube.
  2. Click Share, then Embed.
  3. Copy the code and paste it into your HTML.

The code looks like this (the video ID at the end will be different):

Example A YouTube embed
<iframe width="400" height="225"
  src="https://www.youtube-nocookie.com/embed/UB1O30fR-EE"
  title="HTML Crash Course video"
  allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
  loading="lazy"></iframe>

Embedding a Google Map

Same idea. On Google Maps, search for a place, click Share → Embed a map, and copy the HTML. It's perfect for a school's "How to get here" page or a small business's contact page.

<iframe
  src="https://www.google.com/maps/embed?pb=...long code from Google..."
  width="600" height="400"
  style="border:0;"
  allowfullscreen
  loading="lazy"
  referrerpolicy="no-referrer-when-downgrade"
  title="Map to Sample National High School"></iframe>

Embedding a Google Form

Teachers love this one. Open your Google Form, click Send, choose the < > (embed) tab, and copy the iframe. Now students can answer the form right on your class website.

Important attributes

AttributePurpose
titleDescribes the content for screen readers (required for accessibility)
loading="lazy"Waits to load until the reader scrolls near it — saves data
allowfullscreenLets videos go fullscreen
allowLists features the embedded page may use
sandboxPuts strong limits on what the embedded page can do
style="border:0"Removes the default border

Making iframes fit on phones

A fixed width="600" iframe will be too wide for a phone screen. A modern CSS fix is to make it take the full width and keep the 16:9 shape with aspect-ratio:

Example A responsive video frame
<iframe
  src="https://www.youtube-nocookie.com/embed/UB1O30fR-EE"
  title="HTML Crash Course video"
  style="width: 100%; aspect-ratio: 16 / 9; height: auto; border: 0;"
  allowfullscreen loading="lazy"></iframe>

Resize your browser window (or look at this on a phone): the video always fills the width and keeps its shape.

Why some sites won't load in an iframe

Try putting https://www.facebook.com or your bank's website in an iframe — you'll get a blank box or an error. Many websites block being framed, for security. This protects users from "clickjacking," where a bad site hides a real page under fake buttons. Only use the official embed codes that services give you.

Iframes and your own content

A page that's mostly an embedded video with no text of your own isn't very useful to readers or to search engines. If you embed a video, add your own explanation around it: what the video teaches, key points, or questions to think about. That makes the page truly yours.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does an iframe do?
2. Where do you get the iframe code for a YouTube video?
3. Which attribute helps screen reader users know what's inside an iframe?
4. Why does a bank's website show blank in your iframe?

By Sir Reyel · Last updated 2026-12-04