<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)
Example
#<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>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
#<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”.HTML code
HTML code that the frame displays instead of fetching a page from an address. It takes precedence oversrc. Write quotes inside the code as single quotes or the"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.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 exampleallow-scriptsorallow-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 olderallowfullscreenattribute still works, but the newer way to write it isallow="fullscreen".loading
eager | lazy
lazyloads 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 withtarget="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 theRefererheader.
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#srcdoc
HTML codeHTML 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 " entity so you don’t close the attribute value.
<iframe
title="Button preview"
width="260"
height="80"
srcdoc="<button style='font: 16px system-ui; padding: 8px 16px'>Sign up</button>"
></iframe>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.
<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>Default CSS
#Most browsers display this element with these default styles:
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
sandboxwith a minimal set of permissions. - Don’t combine
allow-scriptswithallow-same-originfor 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
#- <embed>Embeds an external resource in the page, most often a PDF document.
- <object>Embeds an external resource (PDF, image, page) with fallback content in case it fails to load.
- <video>A video player embedded directly in the page.
- <a>A link to another page, a file, an email address, or a place on the same page.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.