Skip to main content

HTML Iframes

An HTML iframe shows a web page inside another web page.


HTML Iframe Syntax

The <iframe> tag makes an inline frame, which embeds another document inside the current page.

Tip: Always include a title attribute on an <iframe>. Screen readers use it to describe the iframe's content.

Iframe syntax

The shape of an iframe.

index.html
<iframe src="url" title="description"></iframe>

Iframe - Set Height and Width

Use the height and width attributes to set the iframe's size (in pixels by default), or set them with the CSS height and width properties in the style attribute.

With attributes

An iframe sized with the height and width attributes.

Prompt
A page with an iframe embedding demo_iframe.html, sized 300 wide by 200 tall using the height and width attributes, with a title for accessibility.
Output: complete HTML page

With CSS

An iframe sized with CSS in the style attribute.

Prompt
A page with an iframe embedding demo_iframe.html, sized 300px by 200px using the style attribute with CSS height and width, with a title.
Output: complete HTML page

Iframe - Remove the Border

By default an iframe has a border. Remove it with the CSS border property set to none, or use CSS to change the border's size, style, and color.

No border

An iframe with its border removed.

Prompt
A page with an iframe embedding demo_iframe.html whose border is removed using the style attribute with border:none, with a title.
Output: complete HTML page

Colored border

An iframe with a red border.

Prompt
A page with an iframe embedding demo_iframe.html with a 2px solid red border set via the style attribute, with a title.
Output: complete HTML page

Iframe - Target for a Link

An iframe can be the target frame for a link. Set the link's target attribute to the iframe's name attribute, and the linked page loads inside the iframe.

Example

A link that loads its page inside a named iframe.

Prompt
A page with an iframe named iframe_a embedding demo_iframe.html, and a link to PromptCookbook whose target is iframe_a so it loads inside the iframe.
Output: complete HTML page

Chapter Summary

To recap: the <iframe> tag makes an inline frame, the src attribute sets the page to embed, and you should always include a title for screen readers. The height and width attributes set its size, and border:none; removes the border.