Skip to main content

HTML Iframes

An HTML iframe is used to display a web page within a web page.

Syntax

The HTML <iframe> tag specifies an inline frame.

An inline frame is used to embed another document within the current HTML document.


<iframe src="url" title="description"></iframe>
note

It is a good practice to always include a title attribute for the <iframe>. This is used by screen readers to read out what the content of the iframe is.

Set Width and Height of iframe

You can set the width and height of iframe by using width and height attributes. By default, the attributes values are specified in pixels but you can also set them in percent. i.e. 50%, 60% etc.


<iframe src="a-page.html" height="200" width="300" title="Iframe Example"></iframe>

Or you can add the style attribute and use the CSS height and width properties:


<iframe src="a-page.html" style="height:200px;width:300px;" title="Iframe Example"></iframe>

Remove the border of iframe

By default, an iframe contains a border around it. You can remove the border by using <style> attribute and CSS border property.

<!DOCTYPE html>
<html>
<body>
<h2>Remove the Iframe Border</h2>
<p>This iframe example doesn't have any border</p>
<iframe src="https://www.wikipedia.org" style="border:none;"></iframe>
</body>
</html>
note

You can also change the size, color, style of the iframe's border.

An iframe can be used as the target frame for a link.

The target attribute of the link must refer to the name attribute of the iframe:

<!DOCTYPE html>
<html>
<body>
<h2>Iframe - Target for a Link</h2>
<iframe height="300px" width="100%" src="https://www.wikipedia.org" name="iframe_a"></iframe>
<p><a href="https://www.google.com" target="iframe_a">Google</a></p>
<p>The name of iframe and link target must have same value else link will not open as a frame. </p>
</body>
</html>

Target a link example

Attributes of <iframe>

Attribute nameValueDescription
allowfullscreenIf true then that frame can be opened in full screen.
heightPixelsIt defines the height of the embedded iframe, and the default height is 150 px.
nametextIt gives the name to the iframe. The name attribute is important if you want to create a link in one frame.
frameborder1 or 0It defines whether iframe should have a border or not. (Not supported in HTML5).
widthPixelsIt defines the width of embedded frame, and default width is 300 px.
srcURLThe src attribute is used to give the path name or file name which content to be loaded into iframe.
sandboxThis attribute is used to apply extra restrictions for the content of the frame
allow-formsIt allows submission of the form if this keyword is not used then form submission is blocked.
allow-popupsIt will enable popups, and if not applied then no popup will open.
allow-scriptsIt will enable the script to run.
allow-same-originIf this keyword is used then the embedded resource will be treated as downloaded from the same source.
srcdocThe srcdoc attribute is used to show the HTML content in the inline iframe. It overrides the src attribute (if a browser supports).
scrollingIt indicates that browser should provide a scroll bar for the iframe or not. (Not supported in HTML5)
autoScrollbar only shows if the content of iframe is larger than its dimensions.
yesAlways shows scroll bar for the iframe.
noNever shows scrollbar for the iframe.