DevAcademy
LearnHTMLIFrames
AdvancedHTML

IFrames

Learn how to embed another web page inside your page using the iframe element, and understand its security considerations.

Reading Time

12 min

Lesson

Lesson 22 of 27

The iframe Element

An <iframe> embeds another HTML document inside the current page, rendered in its own independent browsing context. It’s commonly used for embedding maps, videos, and third-party widgets.

Basic IFrame

<iframe
  src="https://www.example.com"
  width="600"
  height="400"
  title="Example website"
></iframe>
Output

Common Attributes

AttributePurpose
srcURL of the page to embed
titleAccessible name describing the embedded content
width / heightDimensions of the iframe
loading"lazy" defers loading until the iframe is near the viewport
allowGrants permissions like camera, microphone, or fullscreen
sandboxRestricts what the embedded page is allowed to do

Embedding a Video

Iframes are the standard way to embed content from platforms like YouTube, since it lets the video run in its own isolated context without exposing your page’s code to it.

YouTube Embed Example

<iframe
  src="https://www.youtube.com/embed/VIDEO_ID"
  title="YouTube video player"
  loading="lazy"
  allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
></iframe>
Output

The sandbox Attribute

sandbox applies a set of restrictions to the embedded content — for example, disabling scripts or form submission — as a security measure when embedding untrusted content.

Sandboxed IFrame

<iframe
  src="untrusted-widget.html"
  sandbox="allow-scripts"
  title="Untrusted widget"
></iframe>
Output

Security Considerations

Never embed untrusted content without a sandbox attribute, since a malicious iframe could otherwise attempt to manipulate your page. Only embed sources you trust or have explicitly restricted.

Always Add a title

The title attribute is required for accessibility — it tells screen reader users what the embedded content is before they enter it.

Best Practice

Always set a descriptive title, use loading="lazy" for below-the-fold embeds, and apply the narrowest sandbox permissions necessary when embedding third-party content.

Interview Questions

Quick Quiz

1. What does the src attribute of an <iframe> specify?

2. Which attribute restricts what an embedded page is allowed to do?

3. Why should every iframe have a title attribute?