<iframe>

A frame that embeds another HTML document in the page, for example a YouTube video or a map.

Display
inline (replaced element)
Closing tag
Required
Permitted content
None (the content between the tags is ignored)
On this page

Example

#
HTML
<iframe
  title="Ania’s business card"
  width="320"
  height="140"
  srcdoc="<h1 style='font: 700 22px system-ui; margin: 0 0 8px'>Ania Kowalska</h1><p style='font: 15px system-ui; margin: 0'>I’m learning HTML and CSS. This is my first page!</p>"
></iframe>
Preview

Definition and usage

#

The <iframe> element (from inline frame) displays a completely separate document inside the page, with its own HTML, CSS, and JavaScript. This is how YouTube and Vimeo players, maps, payment forms, and chat widgets are embedded. You give the document address in src, and you can also write a short piece of HTML directly in the srcdoc attribute.

The content of the frame is isolated from your page: styles don’t mix, and scripts from another domain have no access to your DOM. Many sites don’t allow their pages to be embedded in frames at all and block it with response headers, so not every page can be displayed in an <iframe>.

Every frame should have a title attribute describing its content, for example “Map showing how to get to the office”. A screen reader reads it before the user enters the frame. Frames below the first screen are worth loading lazily with loading="lazy".

Syntax

#
Syntax
<iframe src="address" title="Description of the content" width="…" height="…"></iframe>

Attributes

#
  • src

    URL

    The address of the document displayed in the frame. Sites like YouTube or Google Maps provide ready-made <iframe> code with the right address under “Share”, then “Embed”.
  • srcdoc

    HTML code

    HTML code that the frame displays instead of fetching a page from an address. It takes precedence over src. Write quotes inside the code as single quotes or the &quot; entity so you don’t close the attribute value.
  • width / height

    number of pixels

    The dimensions of the frame, 300 by 150 pixels by default. Content that doesn’t fit is scrolled inside the frame.
  • sandbox

    empty or a list of permissions, e.g. `allow-scripts allow-forms`

    Runs the frame in a sandbox with restrictions. An empty attribute blocks scripts, forms, and pop-ups, and treats the content as coming from a foreign domain. You restore further permissions with keywords, for example allow-scripts or allow-forms. In the example, the same script runs only in the second frame.
  • allow

    list of permissions, e.g. `fullscreen; autoplay; clipboard-write`

    Specifies which browser features the frame may use: full screen, autoplay, camera, microphone, clipboard. Ready-made YouTube embed codes already contain the right value. The older allowfullscreen attribute still works, but the newer way to write it is allow="fullscreen".
  • loading

    eager | lazy

    lazy loads the frame only when it gets close to the visible part of the page. It is a big saving for embedded videos and maps, which often weigh more than the rest of the page.
  • name

    text

    The name of the frame. A link with target="name" opens its address in this very frame.
  • referrerpolicy

    no-referrer | origin | strict-origin-when-cross-origin | …

    Decides how much information about your page reaches the embedded service in the Referer header.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

srcdoc

HTML code

HTML code that the frame displays instead of fetching a page from an address. It takes precedence over src. Write quotes inside the code as single quotes or the &quot; entity so you don’t close the attribute value.

HTML
<iframe
  title="Button preview"
  width="260"
  height="80"
  srcdoc="<button style='font: 16px system-ui; padding: 8px 16px'>Sign up</button>"
></iframe>
Preview

sandbox

empty or a list of permissions, e.g. `allow-scripts allow-forms`

Runs the frame in a sandbox with restrictions. An empty attribute blocks scripts, forms, and pop-ups, and treats the content as coming from a foreign domain. You restore further permissions with keywords, for example allow-scripts or allow-forms. In the example, the same script runs only in the second frame.

HTML
<iframe
  sandbox
  title="Frame with no permissions"
  width="220"
  height="70"
  srcdoc="<p id='msg' style='font: 15px system-ui'>Scripts blocked</p><script>document.getElementById('msg').textContent = 'Script is running'</script>"
></iframe>
<iframe
  sandbox="allow-scripts"
  title="Frame with the allow-scripts permission"
  width="220"
  height="70"
  srcdoc="<p id='msg' style='font: 15px system-ui'>Scripts blocked</p><script>document.getElementById('msg').textContent = 'Script is running'</script>"
></iframe>
Preview

Default CSS

#

Most browsers display this element with these default styles:

CSS
iframe {
  border: 2px inset;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • A frame from another site runs someone else’s code. Embed only trusted services, and for unknown ones add sandbox with a minimal set of permissions.
  • Don’t combine allow-scripts with allow-same-origin for content from your own domain. The script in the frame could then remove the sandbox restrictions itself.
  • You can get a responsive video frame in CSS: width: 100%; aspect-ratio: 16 / 9; border: 0;.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.