This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.

Smartphone icons created by Freepik - Flaticon

4.3 HTML Iframes

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

Use Iframe to display another webpage here

HTML Iframe Syntax

The HTML <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document.

Syntax
<iframe src="url" title="description">

Tip: 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.

Iframe - Set Height and Width

Use the height and width attributes to specify the size of the iframe. The height and width are specified in pixels by default:

If you are designing web pages to be viewed by cellphone users, I recommend not setting the width attribute or set it no wider than 325px as it may break the page when displayed on cellphones but will still display well on larger screens. By experimenting with my Google Pixel II, I found that the Iframe set at 325px fit the screen while in portrait and does adjust to a slightly larger display of the Iframe when rotated to landscape. Meanwhile it still looks awesome on larger screens.

Example 1: HTML iframe part 1
<iframe src="demo_iframe.htm" height="200" width="300" title="Iframe Example"></iframe>

On further testing I found not setting the width or setting it to 300px has the same effect across all screen sizes. It is related to setting the <meta name="viewport" content="width=device-width, initial-scale=1"> in the head of the page. This setting makes the webpage mobile friendly. When I disabled viewport on this page the text got larger in Google Chrome but considerably smaller in Firefox mobile on my phone. It also caused the Iframe to display on my phone at the width specified.

In Linux, there is a little known or used native file browser called konqueror that may be used as a web browser. The beauty of this application is that it may be sized down to less than 300px wide. However it does not support a lot of things. It does not support viewport, so I get to see how web pages look on older phones that do not support viewport. The default minimum width appears to be 250px. When I size it to less than 250px, when I go to another webpage it automatically increases to 250px wide. These are my observations.

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

Example 2: HTML iframe part 2
<iframe src="demo_iframe.htm" style="height:200px;width:300px;" title="Iframe Example"></iframe>

Iframe - Remove the Border

By default, an iframe has a border around it. To remove the border, add the style attribute and use the CSS border property:

Example 3: no iframe border
<iframe src="demo_iframe.htm" style="border:none;" title="Iframe Example"></iframe>

With CSS, you can also change the size, style and color of the iframe's border:

Example 4: red iframe border
<iframe src="demo_iframe.htm" style="border:2px solid red;" title="Iframe Example"></iframe>

Iframe - Target for a Link

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:

Example 5: iframe target
<iframe src="demo_iframe.htm" name="iframe_a" title="Iframe Example"></iframe>

<p><a href="https://www.w3schools.com" target="iframe_a">W3Schools.com</a></p> 

This is a very interesting lesson.

  1. The code for the height and width are for styling, not attributes of the iframe
  2. When style is used to set the width % may be used for setting the width
  3. When % is used to set the width, the iframe displays correctly across all screen sizes and modern devices

Chapter Summary

HTML iframe Tag

Tag Description
<iframe> Defines an inline frame

For a complete list of all available HTML tags, visit HTML Tag Reference.