Definition
The <iframe> tag represents a nested browsing context and is used to embed an HTML document in your current HTML document.
Example
<iframe
src="https://www.thewebmaster.com"
title="The Webmaster"
width="500"
height="350"
></iframe>Usage
- Both the opening and closing tags are required.
- If the
srcattribute and thesrcdocattribute are both specified, thesrcdocattribute takes priority.
Attributes
The <iframe> element supports the Global Attributes, as well as the following:
allowThe
allowattribute specifies a feature policy to define what features are available to the<iframe>. For example:<iframe src="[insert source]" allow="camera 'none'; microphone 'none'"> </iframe>allowfullscreenThe
allowfullscreenattribute specifies whether the<iframe>can activate fullscreen. This is a legacy attribute. You should use theallowattribute instead:<iframe src="[insert source]" allow="fullscreen"> </iframe>allowpaymentrequestThe
allowpaymentrequestattribute specifies whether the<iframe>can activate the Payment Request API. This is a legacy attribute. You should use theallowattribute instead:<iframe src="[insert source]" allow="payment"> </iframe>heightThe
heightattribute specifies the height of the<iframe>in pixels. The default value is150.nameThe
nameattribute specifies the name of the<iframe>.referrerpolicyThe
referrerpolicyattribute specifies the referrer information that you wish to send with the iframe.Values can include:
no-referrerno-referrer-when-downgradeoriginorigin-when-cross-originsame-originno-referrer-when-downgradestrict-origin-when-cross-originunsafe-url
sandboxWhen the
sandboxattribute is empty it imposes wide range of restrictions for the<iframe>content. Alternatively, it can take a pre-defined value that will remove those particular restrictions. Multiple values can be provided in a space-separated list.Values can include:
allow-downloadsallow-formsallow-modalsallow-orientation-lockallow-pointer-lockallow-popupsallow-popups-to-escape-sandboxallow-presentationallow-same-originallow-scriptsallow-top-navigationallow-top-navigation-by-user-activation
srcThe
srcattribute specifies the source URL of the document embedded in the<iframe>.srcdocThe
srcdocattribute specifies the HTML code or content to be embedded in the<iframe>. When usingsrcdocyou should escape"and&characters. For example:<iframe sandbox srcdoc="<p>hey that’s earl’s table. <p>you should get earl&amp;me on the next cover."></iframe> <iframe sandbox srcdoc="<p>Yeah, you can see it <a href="/gallery?mode=cover&amp;page=1">in my gallery</a>."> </iframe>widthThe
widthattribute specifies the height of the<iframe>in pixels. The default value is300.
Best Practices
- To help people with assistive technology such as screen readers, you should use the
titleattribute on an<iframe>element. This should describe the<iframe>contents, so users do not need to navigate it to find out what it is about.
Specification
Browser Support
Desktop
| Chrome | Edge | Firefox | IE | Opera | Safari |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Mobile
| Android Webview | Chrome Android | Firefox Android | Opera Android | iOS Safari | Samsung Internet |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
