So far, everything that appears on The Blue Cup's pages has either been written by you or lives in the project folders: text, tables, images, the intro video and the café's ambient audio. But some content makes no sense to produce yourself. An interactive map showing where the café is located is provided by specialist services; a video published on YouTube or Vimeo is already hosted there, with its own player and subtitles. To display that external content inside your page, there is the iframe element. In this lesson you will learn to use it, to describe it so that it is accessible, to control what the embedded page can do with allow and sandbox, and to understand the security and privacy risks that come with putting someone else's website inside yours.

Contents

  1. What an iframe is
  2. Basic attributes: src, width, height and title
  3. Loading the iframe only when needed: loading
  4. Permissions: allow and allowfullscreen
  5. Locking the content in: sandbox
  6. What information is sent: referrerpolicy
  7. Security: the risks of embedding and of being embedded
  8. Privacy: third-party cookies and cookie-free modes
  9. Embedding a map
  10. Embedding a YouTube or Vimeo video
  11. srcdoc: content written in the attribute itself
  12. Project: a map on the contact page and a video interview in the news

What an iframe is

An iframe (inline frame) is a rectangular window inside your page that displays another complete HTML document. That document has its own html, its own head, its own styles and scripts, and it can come from your site or from any other.

<iframe src="https://maps.example/the-blue-cup" title="Map showing the location of The Blue Cup"></iframe>
  • The browser downloads the page given in src and displays it inside the rectangle.
  • The embedded page is independent: your CSS doesn't affect it and, in general, your JavaScript can't read it if it comes from another domain.
  • The content between <iframe> and </iframe> is not displayed: current browsers ignore it. Leave it empty.
graph TD
    A["Your page: contact.html (thebluecup.example)"] --> B["Your own content: h1, address, form"]
    A --> C["iframe"]
    C --> D["Another complete document (map service)"]
    D --> E["Its own HTML, CSS, JS and cookies"]

The page containing the iframe is usually called the host page (or parent), and the embedded one the embedded document (or child).

When should you use an iframe?

Situation iframe? Alternative
Interactive map from an external service Yes Static image of the map + link
Video hosted on YouTube or Vimeo Yes video with your own files (Embedding Videos)
A provider's booking, payment or review widget Yes, if the provider offers it that way Link to their page
Showing your own content from another page Hardly ever Write the HTML in the page
Dividing the page layout into areas No Semantic elements + CSS (module 8)

In the 1990s websites were laid out with "frames" (frameset and frame). Those elements are obsolete and are not part of today's HTML. The iframe is something else: a box inside the content for one specific resource.

Basic attributes: src, width, height and title

<iframe
  src="https://maps.example/embed?place=the-blue-cup"
  width="600"
  height="400"
  title="Map: The Blue Cup, 12 Example Street, Fictional Town">
</iframe>
Attribute What it does Value
src Address of the embedded document Absolute or relative URL
width Width of the frame Number of pixels, no unit (default 300)
height Height of the frame Number of pixels, no unit (default 150)
title Describes the content of the frame Short, descriptive text
  • width and height work as they do with images: they reserve the space before the content loads and prevent the page from jumping. In Responsive Images and Videos you will see how to make the frame adapt to the screen width with CSS.
  • title is compulsory in practice. When a screen reader reaches an iframe, it announces something like "frame, Map: The Blue Cup…". Without a title, it only says "frame" or reads out the URL, and the user doesn't know whether it's worth going in. It must describe what it contains, not just "iframe" or "map".

Browsers display a border around the frame by default. The old frameborder="0" attribute is obsolete; the border is removed with CSS (border: none), which you will learn in module 8. If you copy code from a service that includes frameborder, you can delete it.

Loading the iframe only when needed: loading

An iframe is heavy: the browser downloads an entire page, with its scripts and styles. If the map is at the bottom of the page, there's no point in loading it before the user gets there:

<iframe src="…" width="600" height="400" title="…" loading="lazy"></iframe>
  • loading="lazy" (lazy loading) tells the browser to wait until the frame is close to the visible area before downloading it.
  • The default value is eager (load immediately).

You will look at this in detail, together with images, in Performance and Resource Loading. For now the rule is simple: any iframe that isn't visible when the page opens gets loading="lazy".

Permissions: allow and allowfullscreen

The browser does not grant the embedded document certain sensitive capabilities unless the host page explicitly permits them. The allow attribute defines that permissions policy: a list of features separated by semicolons.

<iframe
  src="https://videos.example/embed/interview"
  title="Video: interview with Carmen on Neighbourhood TV"
  allow="autoplay; encrypted-media; picture-in-picture; fullscreen">
</iframe>
allow value What it lets the embedded content do
fullscreen Go full screen
autoplay Play automatically (autoplay policies still apply)
encrypted-media Play protected video (many platforms use it)
picture-in-picture Show the video in a floating window
clipboard-write Write to the clipboard ("copy link" button)
geolocation Ask for the user's location
camera, microphone Ask for access to the camera and microphone
  • Grant only what the content needs. A video needs full screen; a location map doesn't need your microphone.
  • allowfullscreen is an old Boolean attribute equivalent to allow="fullscreen". You will see it in a lot of embed code; it works, but the modern approach is to include fullscreen in allow.
  • Even if you grant geolocation or camera, the browser will still ask the user. allow doesn't bypass that consent: it only makes it possible to ask.

Locking the content in: sandbox

The sandbox attribute applies a set of very strict restrictions to the embedded document. Written without a value, it forbids almost everything:

<iframe src="https://widgets.example/reviews" title="Customer reviews" sandbox></iframe>

With an empty sandbox, the embedded content:

  • Can't run JavaScript.
  • Can't submit forms.
  • Can't open pop-up windows or dialogs (alert).
  • Can't change the URL of your page (of the whole tab).
  • Is treated as if it came from a unique, unknown origin: it can't access its cookies or its storage.

Then you open the doors one by one with values separated by spaces:

Value What it permits again
allow-scripts Running JavaScript
allow-same-origin Treating the content with its real origin (cookies, storage)
allow-forms Submitting forms
allow-popups Opening new windows or tabs (for example, links with target="_blank")
allow-popups-to-escape-sandbox Opening those new windows without the restrictions
allow-modals Showing dialogs such as alert() or confirm()
allow-downloads Starting downloads
allow-presentation Using the Presentation API (sending to an external screen)
allow-top-navigation-by-user-activation Changing the tab's URL, only if the user clicks
<iframe
  src="https://widgets.example/reviews"
  title="Customer reviews of The Blue Cup"
  sandbox="allow-scripts allow-popups allow-popups-to-escape-sandbox">
</iframe>
  • The widget can run its JavaScript to display the reviews.
  • Its links can open new tabs, which will work normally.
  • It can't submit forms, change your page or show alerts.

Watch out for one combination: allow-scripts together with allow-same-origin on a document from your own domain cancels out the protection, because that script could remove the sandbox attribute itself. This doesn't happen with content from another domain, but it's worth knowing.

When should you use sandbox? Mainly when you embed content you don't fully trust or don't control, and you know what it needs. With the big map and video services, sandbox can break features (full screen, links), so it's common not to use it and to limit things with allow instead. If you do use it, test the result thoroughly.

What information is sent: referrerpolicy

When the browser loads the iframe, by default it tells the external server which page the request comes from (the Referer header, which you already met with rel="noreferrer" in Creating Hyperlinks). The referrerpolicy attribute controls how much is disclosed:

Value What is sent
no-referrer Nothing
origin Only the domain: https://thebluecup.example/
strict-origin-when-cross-origin The full address to your own site; only the domain to other sites; nothing when going from HTTPS to HTTP (this is the browsers' default behaviour)
no-referrer-when-downgrade The full address, except from HTTPS to HTTP
<iframe src="…" title="…" referrerpolicy="strict-origin-when-cross-origin"></iframe>

Some services include it in their embed code. If one of your pages had sensitive data in the URL (for example, a parameter with an order number), no-referrer would stop it reaching the external service.

Security: the risks of embedding and of being embedded

The iframe poses risks in both directions.

When you embed others

You are showing something on your website that you don't control. If that service changes, fails or is attacked, the problem appears inside your page. That's why you should:

  • Only embed trusted services, and always over HTTPS.
  • Grant only what is essential with allow, and consider sandbox.
  • Never embed pages that ask for personal data or passwords: the user can't see the frame's address bar and can't check where they are typing.

When others embed you: clickjacking

Imagine someone creates a trap website with an eye-catching button: "Win a free breakfast!". Underneath, in a transparent iframe, they place The Blue Cup's bookings page, so that the "Send booking" button sits right under the fake button. The user thinks they're clicking one thing and clicks another. This technique is called clickjacking.

graph TD
    A["Trap website: visible button “Win a breakfast!”"] --> B["Invisible iframe on top with bookings.html"]
    B --> C["The real click lands on “Send booking”"]

The defence isn't written in HTML: your website's server configures it, using HTTP headers that tell the browser "don't allow this page to be displayed inside an iframe on another site":

Header (on the server) Example Meaning
X-Frame-Options DENY Nobody can embed the page
X-Frame-Options SAMEORIGIN Only pages from the same site
Content-Security-Policy frame-ancestors 'self' Modern version: only the same site

You don't have to configure this now; it's enough to understand the idea. And it explains something that will happen to you: if you try to embed just any website in an iframe (for example, a search engine's home page or a bank's), you will see an empty box or an error. It isn't a bug in your code: that website has prohibited being embedded. You can only embed what the service explicitly offers for that purpose, usually with an /embed/ type URL.

Privacy: third-party cookies and cookie-free modes

When a visitor opens a page with an embedded YouTube video, their browser connects to YouTube's servers even if they don't press "play". That service can receive their IP address, know which page they were visiting and store or read third-party cookies, which allow it to recognise them on other websites.

Practical consequences:

  • Legal: in the European Union, cookies that are not technically necessary require the user's consent (GDPR and cookie regulations, which you already mentioned in Submitting Forms). An embedded video may mean you have to display a cookie notice and mention it in the privacy policy.
  • Technical: more and more browsers block third-party cookies by default, so some widgets may work less well.

Sensible measures:

  1. Use the privacy-enhanced modes that some services offer. YouTube has the youtube-nocookie.com domain: the same player, but it doesn't store tracking cookies until the user plays the video. Vimeo supports the dnt=1 (do not track) parameter.
  2. Lazy loading with loading="lazy", so that the connection only happens if the user reaches that part of the page.
  3. Always offer a link to the external content, for anyone who prefers to view it on the original site.
  4. Mention it in privacy.html: which external services your website embeds.
  5. For maximum privacy, there is the "facade" technique: showing an image with a button and loading the iframe only when the user clicks. It requires JavaScript, and we will mention it again in Performance and Resource Loading.

Embedding a map

Map services (Google Maps, OpenStreetMap and others) offer ready-to-copy code. The process is always similar:

  1. Look up the address in the service.
  2. Click "Share" and choose the "Embed map" option (or "Embed", "HTML").
  3. Copy the <iframe …> code it gives you.
  4. Paste it into your page and review it: add or improve the title, add loading="lazy", remove obsolete attributes.

This is what typical code looks like as delivered by the service (with generic data):

<iframe src="https://www.openstreetmap.org/export/embed.html?bbox=-3.71%2C40.41%2C-3.69%2C40.42&amp;layer=mapnik&amp;marker=40.415%2C-3.70" width="425" height="350" frameborder="0" style="border: 1px solid black"></iframe>

And this is how it looks after reviewing it:

<iframe
  src="https://www.openstreetmap.org/export/embed.html?bbox=-3.71%2C40.41%2C-3.69%2C40.42&amp;layer=mapnik&amp;marker=40.415%2C-3.70"
  width="600"
  height="400"
  title="Map: location of The Blue Cup at 12 Example Street"
  loading="lazy">
</iframe>
  • Note the &amp; in the URL: it's the correct way to write & inside an attribute, as you saw in HTML Entities.
  • %2C is the URL-encoded comma, like the %20 for the space that you used in the mailto: links.
  • frameborder (obsolete) and the style attribute (styles will go in the CSS) have been removed.
  • A specific title and loading="lazy" have been added.
  • The bbox (visible area) and marker (pin) parameters are generated by the service: you don't have to write them by hand.

An embedded map doesn't replace the written address. Some people can't use a map (with a screen reader, on a slow connection or with external content blocked), so the address must also be there as text, and it's a good idea to add a link to open the full map.

Embedding a YouTube or Vimeo video

The process is the same: on the video's page, the "Share" button → "Embed", and copy the code. YouTube code looks like this (with a fictional identifier, VIDEO_ID):

<iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

Reviewed for The Blue Cup:

<iframe
  src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
  width="560"
  height="315"
  title="Video: interview with Carmen on Neighbourhood TV"
  allow="encrypted-media; picture-in-picture; fullscreen"
  referrerpolicy="strict-origin-when-cross-origin"
  loading="lazy">
</iframe>
  • The youtube-nocookie.com domain instead of youtube.com: privacy-enhanced mode.
  • A descriptive title instead of the generic "YouTube video player", which says nothing.
  • A reduced allow: autoplay is removed (we don't want automatic playback, as you decided in module 3), as are accelerometer and gyroscope (phone sensors for 360° videos) and clipboard-write/web-share (not essential). fullscreen is kept, replacing allowfullscreen.
  • frameborder disappears.

With Vimeo, the code is similar:

<iframe
  src="https://player.vimeo.com/video/VIDEO_NUMBER?dnt=1"
  width="640"
  height="360"
  title="Video: interview with Carmen on Neighbourhood TV"
  allow="picture-in-picture; fullscreen"
  loading="lazy">
</iframe>
  • ?dnt=1 asks Vimeo not to track the visitor.

video or iframe? If you have the video files and can host them, the video element (Embedding Videos) gives you full control and doesn't involve third parties. If the video is on a platform (because it belongs to someone else, or because you want to take advantage of its hosting and statistics), use the iframe the platform provides. Never download someone else's video to upload it to your website without permission.

srcdoc: content written in the attribute itself

Instead of a URL, the srcdoc attribute lets you write the HTML of the embedded document directly:

<iframe srcdoc="<p>Hello from inside the frame</p>" title="srcdoc example" sandbox></iframe>

If an iframe has both srcdoc and src, srcdoc wins. It is mainly used in tools that display live code examples, combined with sandbox. We won't need it at The Blue Cup; it's enough for you to recognise the attribute if you see it.

Project: a map on the contact page and a video interview in the news

  1. Location map in contact.html

In contact.html, after the paragraph with the address and before the frequently asked questions, add a section with the map:

      <section id="directions">
        <h2>How to find us</h2>
        <p>We are at 12&nbsp;Example Street, Fictional Town, two minutes from the Market Square bus stop.</p>
        <iframe
          src="https://www.openstreetmap.org/export/embed.html?bbox=-3.71%2C40.41%2C-3.69%2C40.42&amp;layer=mapnik&amp;marker=40.415%2C-3.70"
          width="600"
          height="400"
          title="Map: location of The Blue Cup at 12 Example Street"
          loading="lazy">
        </iframe>
        <p><a href="https://www.openstreetmap.org/?mlat=40.415&amp;mlon=-3.70#map=17/40.415/-3.70" target="_blank" rel="noopener noreferrer">View the full-size map (opens in a new tab)</a></p>
      </section>
  • The section has its h2 and an id that serves as an anchor, like the site's other sections.
  • The address appears as text before the map: the map complements it, it doesn't replace it.
  • The iframe has a specific title, width/height and loading="lazy".
  • The link to the full map follows the convention for external links from module 3.
  • The coordinates are examples. In a real project you would copy the code from the service's "Share" button with the real location.

By the way: this section doesn't use address. The owner's contact details are already in the footer's address; here the address is page content, as you concluded in the exercises of Other Semantic Elements.

  1. The video interview in news.html

A fictional local TV channel, "Neighbourhood TV", has interviewed Carmen and published the video on YouTube. Add a new news item at the end of the list of news, after #cold-brew and before aside#newsletter:

    <article id="tv-interview">
      <header>
        <h2>Carmen on Neighbourhood TV</h2>
        <p>Published on <time datetime="2026-11-17">17 November 2026</time></p>
      </header>
      <p>The local channel Neighbourhood TV visited our bakery and interviewed Carmen, our head baker, about sourdough bread and the workshop on the first Saturday of every month.</p>
      <figure>
        <iframe
          src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
          width="560"
          height="315"
          title="Video: interview with Carmen on Neighbourhood TV"
          allow="encrypted-media; picture-in-picture; fullscreen"
          referrerpolicy="strict-origin-when-cross-origin"
          loading="lazy">
        </iframe>
        <figcaption>Neighbourhood TV interview (<time datetime="PT4M">4 minutes</time>). <a href="https://www.youtube.com/watch?v=VIDEO_ID" target="_blank" rel="noopener noreferrer">Watch the video on YouTube (opens in a new tab)</a>.</figcaption>
      </figure>
    </article>
  • It is an article: a complete news item with its header and its date in time, just like the others.
  • The video goes inside a figure with a figcaption, like the gallery's own video (main and figure). The caption states the duration and offers the link to the original site.
  • VIDEO_ID is a placeholder: in a real project you would replace it with the identifier that appears in the embed code.
  • The video belongs to a third party; subtitles are handled by the platform (YouTube lets you switch them on in its player).

  1. Privacy

Open privacy.html and add a paragraph stating that some pages embed content from external services (an OpenStreetMap map on the Contact page and a YouTube video on the News page, in privacy-enhanced mode), that those services may receive technical data such as the IP address, and that visitors can open that content directly on the original site using the links provided. Remember that the text of this page is awaiting professional review.

  1. Checking

  • Open contact.html and news.html with Live Server: the frames should be visible (with a grey border, for now).
  • In DevTools > Network, reload news.html with a small window: thanks to loading="lazy", the request to youtube-nocookie.com only appears when you scroll down to the news item.
  • Turn on a screen reader or inspect the accessibility tree: the frame should be announced with its title.

Common Mistakes and Tips

  • Forgetting the title or leaving the generic "YouTube video player". Describe the specific content.
  • Using iframe for layout or to show your own pages. That's what semantic elements and CSS are for.
  • Trying to embed any website and seeing an empty frame: the site forbids it with X-Frame-Options or frame-ancestors. Use the embed URL (/embed/) the service provides.
  • Copying embed code without reviewing it: remove frameborder, reduce allow, add loading="lazy".
  • Granting unnecessary permissions (autoplay, camera, geolocation…).
  • A map without a written address: the text is essential for anyone who can't use the map.
  • Forgetting about privacy: third-party content connects to their servers; use cookie-free modes and say so in the privacy policy.
  • Writing an unescaped & in a map's src: use &amp;.
  • Tip: if a sandbox breaks a widget, add the permissions one at a time and check which one it needs, instead of removing the whole attribute.

Exercises

Exercise 1. Review this embed code copied from a video service and rewrite it following The Blue Cup's conventions. Explain each change.

<iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID?autoplay=1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Exercise 2. A provider offers you a review widget at https://widgets.example/reviews/the-blue-cup. According to its documentation, it needs to run JavaScript and its links open the reviews in a new tab; it has no forms. Write the iframe with a sandbox that allows only that, with a suitable title and lazy loading.

Exercise 3. Answer briefly:

  1. Why might an iframe with src="https://www.bank.example" be displayed empty?
  2. What is clickjacking, and who needs to protect themselves against it: the embedding page or the embedded one?
  3. What advantage does youtube-nocookie.com have over youtube.com?

Solutions

Solution 1.

<iframe
  src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
  width="560"
  height="315"
  title="Video: how we make the autumn seasonal coffee"
  allow="encrypted-media; picture-in-picture; fullscreen"
  loading="lazy">
</iframe>
  • Domain changed to youtube-nocookie.com (privacy).
  • ?autoplay=1 and the autoplay permission removed: we don't play videos automatically.
  • A descriptive title instead of the generic one (the specific content is an example).
  • frameborder (obsolete), accelerometer, gyroscope and clipboard-write (unnecessary) removed.
  • allowfullscreen replaced with fullscreen inside allow.
  • loading="lazy" added.

Solution 2.

<iframe
  src="https://widgets.example/reviews/the-blue-cup"
  width="600"
  height="400"
  title="Customer reviews of The Blue Cup"
  sandbox="allow-scripts allow-popups allow-popups-to-escape-sandbox"
  loading="lazy">
</iframe>
  • allow-scripts: the widget needs JavaScript.
  • allow-popups: its links open new tabs.
  • allow-popups-to-escape-sandbox: those tabs open without restrictions, so that the review website works normally.
  • allow-forms (there are no forms) and allow-top-navigation… (it must not change your page) are not included. If the widget needed its own cookies, you would have to add allow-same-origin; as the provider doesn't say so, it is not granted.

Solution 3.

  1. Because that website forbids being embedded by means of a server header (X-Frame-Options or Content-Security-Policy: frame-ancestors). It's a security measure on their side, not a bug in your code.
  2. It's a technique in which a trap website places another page in an invisible iframe so that the user clicks on it without realising. The embedded page (the victim) needs to protect itself, by configuring those headers on its server.
  3. It doesn't store tracking cookies until the user plays the video, which reduces visitor tracking.

Conclusion

You have learnt that an iframe displays a complete document inside your page and that it is used for content from external services, such as maps and videos, never for layout. You know that it always needs width, height and a descriptive title, that loading="lazy" delays its loading, that allow grants only the essential permissions and that sandbox locks the content in a box whose doors you open one by one. You also understand the risks: external content is not under your control, clickjacking is prevented from the server with X-Frame-Options or frame-ancestors, and embedded services can track your visitors, so it's advisable to use cookie-free modes and to mention them in the privacy policy. The Blue Cup now has a map in contact.html and the Neighbourhood TV interview in news.html. In both cases you have added information to the pages that isn't visible but that browsers make use of: a title, some permissions, a loading mode. In the next lesson, Data Attributes, you will go a step further and learn to store your own data inside HTML elements with data-* attributes, so that CSS and JavaScript can use it, for example, to filter the menu and show only the gluten-free products.

HTML Course

Module 1: Introduction to HTML

Module 2: Text Formatting in HTML

Module 3: Links and Media in HTML

Module 4: Tables in HTML

Module 5: Forms in HTML

Module 6: HTML5 Semantic Elements

Module 7: Advanced HTML Techniques

Module 8: Integrating HTML and CSS

Module 9: Responsive Web Design

Module 10: Best Practices and Accessibility

Module 11: Project: Building a Complete Website

© Copyright 2026. All rights reserved