HTML Frames
HTML frames allow you to display multiple HTML documents within a single browser window. In modern web development, this is typically achieved using the <iframe> tag.
Note: Traditional frame technology using <frameset> and <frame> tags has been deprecated in HTML5. They have many usability, accessibility, and SEO issues and should no longer be used. This chapter will focus on the modern <iframe>.
<iframe> - Inline Frame
The <iframe> (Inline Frame) tag is used to embed another independent HTML document within the current HTML document. It creates a rectangular area that can display content from different sources.
This is very useful for embedding third-party content, such as:
- Online videos (like YouTube, Vimeo)
- Interactive maps (like Google Maps)
- Social media posts
- Advertising banners
Example:
The src Attribute
The src attribute specifies the URL of the page to embed. This is the most core attribute of <iframe>.
The width and height Attributes
You can use the width and height attributes to specify the dimensions of the <iframe> (in pixels or percentages).
You can also use CSS to control its dimensions, which is more recommended for responsive design.
The title Attribute
Providing a title attribute for <iframe> is crucial for accessibility. Screen readers will read this title, telling users what the inline frame contains and helping them decide whether to interact with it.
Removing the Border
By default, <iframe> comes with a border. Usually, we want to remove it for seamless embedding. This can be achieved using CSS's border property:
Or define it in a stylesheet:
The <iframe> target Attribute
An <iframe> can be used as a target for links. If you have a link on a page and want the link's content to display in the <iframe> when clicked, you can do this:
- Set a
nameattribute for the<iframe>. - Set the link
<a>tag'stargetattribute to the<iframe>'snamevalue.
Example:
Clicking the link will load the W3C website into the <iframe> named myFrame.