The Head, CSS/JS & StandardsPro· 25 min read

Iframes In Depth

A closer look at embedding other pages — attributes, sizing and security.

What you will learn

  • Control iframe size and behaviour
  • Understand sandboxing and security
  • Use iframes responsibly

Iframe attributes

AttributePurpose
srcThe page to embed
width / heightSize of the frame
titleDescribes the content (accessibility)
allowfullscreenAllow full-screen (videos)
loading="lazy"Load only when scrolled into view (faster pages)
An embedded map
<iframe src="https://www.openstreetmap.org/export/embed.html?bbox=77.5,12.9,77.7,13.0"
  width="100%" height="200" title="Map of Bengaluru" loading="lazy"></iframe>
Live preview

This single <iframe> embeds a live OpenStreetMap page: src is the map address (the numbers pick the area), width="100%" makes it fill the available width, height="200" sets its height, title describes it for screen readers, and loading="lazy" delays loading it until the user scrolls near it — keeping the rest of the page fast.

Note: Output: A full-width, 200px-tall interactive map of Bengaluru embedded in the page; it only loads once it is about to come into view.

Watch out: An iframe runs another site inside yours. Only embed trusted sources, and never put sensitive content where a malicious embedded page could read it.

Note: You can target an iframe from a link with the name attribute — older sites used this for navigation. Today, prefer normal page links and components.

Q. Which attribute delays loading an iframe until it is scrolled into view?

Answer: loading="lazy" tells the browser to load the iframe only when it is about to be seen, speeding up the page.

✍️ Practice

  1. Embed a map with loading="lazy" and a descriptive title.
  2. Try embedding a site that blocks framing and observe what happens.

🏠 Homework

  1. Build a “resources” page that embeds two trusted external widgets.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →