HTML

Getting to Know HTML Iframe: How to Embed YouTube and Google Maps

How to use the HTML <iframe> tag to embed content from other sites, like a YouTube video or a Google Map, inside your own page.

RazakTerra
Reading time
2 min read
Comments
Discussion
Getting to Know HTML Iframe: How to Embed YouTube and Google Maps
On this page
  1. Basic Iframe Syntax
  2. Embedding a YouTube Video
  3. Embedding Google Maps
  4. Useful Attributes
  5. Security and Performance Tips
  6. Quick Tips

Basic Iframe Syntax

HTML
<iframe src="https://example.com" width="600" height="400"></iframe>
  • src → the URL of the content you want to embed
  • width and height → the size of the frame

Embedding a YouTube Video

Go to a YouTube video → click Share → Embed → copy the code. It usually looks like this:

HTML
<iframe
  width="560"
  height="315"
  src="https://www.youtube.com/embed/aqz-KE-bpKQ"
  title="Big Buck Bunny"
  allow="
    accelerometer;
    autoplay;
    clipboard-write;
    encrypted-media;
    gyroscope;
    picture-in-picture;
    web-share;
  "
  referrerpolicy="strict-origin-when-cross-origin"
  allowfullscreen
>
</iframe>

The result:

You can also add loading=“lazy” to improve performance:

HTML
<iframe
  width="560"
  height="315"
  src="https://www.youtube.com/embed/VIDEO_ID"
  title="YouTube video player"
  loading="lazy"
  allowfullscreen
>
</iframe>

Embedding Google Maps

From Google Maps → click Share → Embed a map → copy the iframe code.

Playground
<iframe
  src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d63245.98372065553!2d110.33355936492256!3d-7.803163411126652!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x2e7a5787bd5b6bc5%3A0x21723fd4d3684f71!2sYogyakarta%2C%20Kota%20Yogyakarta%2C%20Daerah%20Istimewa%20Yogyakarta!5e0!3m2!1sid!2sid!4v1791529445162!5m2!1sid!2sid"
  width="600"
  height="450"
  style="border: 0"
  allowfullscreen=""
  loading="lazy"
  referrerpolicy="strict-origin-when-cross-origin"
></iframe>
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

Useful Attributes

Attribute Purpose
src The URL of the embedded content
width Width of the iframe
height Height of the iframe
allowfullscreen Allows the content to go fullscreen
loading=“lazy” Loads the iframe only when it’s needed
title Describes the iframe (good for accessbility)
sandbox Restricts what the iframe can do (security)

Security and Performance Tips

Iframes can affect your page speed and security, so keep these in mind:

  • Always add a title attribute for accessibility.
  • Use loading=“lazy” so the iframe doesn’t slow down the initial page load.
  • Avoid embedding too many iframes on one page.
  • Use the sandbox attribute if you want to limit what the embedded content can do:
HTML
<iframe src="..." sandbox="allow-scripts allow-same-origin"></iframe>
  • Only embed content from trusted sources.

Quick Tips

  • Prefer official embed codes from YouTube and Google Maps.
  • Always set width and height to avoid layout shifts.
  • Use loading=“lazy” for better performance.
  • Don’t overuse iframes, they can make your page heavier.

With the <iframe> tag, you can easily embed videos, maps, and other external content while keeping some control over performance and security.

Written by

Razak

Writes about the web, slowly, and builds small tools on the side.

Discussion

Comments

Loading comments…

Keep reading

1 min
HTML

HTML Form Validation Without JavaScript

HTML5 has built-in form validation: required fields, length and number limits, and format patterns, all without a single line of JavaScript.

Read article