HTML Iframe (With Examples)

HTML Iframe (With Examples)

4 mins read133 Views Comment
Esha
Esha Gupta
Associate Senior Executive
Updated on Mar 22, 2024 16:12 IST

Have you ever wondered how to display a webpage within another webpage? HTML iframes make this possible, offering a way to embed external content, videos, maps, or other documents directly into your site, creating a window to the world within your own digital space. Let's understand more!

An HTML iframe is an HTML tag that allows you to embed another HTML document within a current HTML document.

The < iframe > element can be used to insert content such as web pages, videos, maps, etc., within an existing web page, creating a window or frame in the web page where the embedded content can be displayed.

Explore Top HTML Interview Questions and Answers

Importance of Using HTML iframes

  • Allows for content integration from various sources. This means you can easily embed multimedia elements such as videos, maps, or interactive content from other platforms into your webpage.
  • Updating content within an iframe can be more straightforward compared to updating content on a regular webpage.
  • Iframes support modular web design by allowing developers to create reusable web components. This feature can be a significant time-saver, as developers can create a component once and reuse it across different webpages or even different websites, maintaining consistency and reducing the time and effort needed for coding.

Must Check: How to Create Table in HTML

Recommended online courses

Best-suited Web Development courses for you

Learn Web Development with these high-rated online courses

75 K
6 months
1.5 L
13 months
8.5 K
3 months
1 L
6 months
3.18 L
3 months
14.35 K
1 week
70.5 K
12 months
60 K
90 hours
90 K
6 months

Here is a basic syntax of an iframe element:

<iframe src="URL" width="width_in_px" height="height_in_px"></iframe>

In this syntax:

  • <iframe> is the tag that initiates the iframe.
  • The src attribute specifies the URL of the page to display in the iframe.
  • The width and height attributes define the dimensions of the iframe.

Let’s see some main attributes that can be used within the <iframe> tag

1.src

This is the most important attribute, which specifies the URL of the document to embed in the iframe.

For example


 
<iframe src="https://www.example.com"></iframe>
Copy code

Output

2023_09_1.gif

2. ‘width’ & ‘height’

These attributes are used to specify the width and height of the iframe, respectively. You can specify values in pixels or as a percentage.

For example


 
<iframe src="https://www.example.com" width="500" height="300"></iframe>
Copy code

Output

2023_09_Screenshot-2023-09-12-154424.jpg
 

3. ‘frameborder’

This attribute can be used to specify whether or not to display a border around the iframe. It accepts values 0 (no border) or 1 (border).

Note: This attribute is not supported in HTML5. It is better to control borders using CSS.

For example


 
<iframe src="https://www.example.com" frameborder="1"></iframe>
Copy code

Output

2023_09_2.gif

4. ‘scrolling’

This attribute controls the appearance of scrollbars in the iframe. It can take three values: auto (browser decides), yes (always show scrollbars), and no (never show scrollbars).

Note: This attribute is not supported in HTML5. It is better to control borders using CSS.

For example


 
<iframe src="https://www.example.com" scrolling="no"></iframe>
Copy code

Output

2023_09_Screenshot-2023-09-12-160713.jpg

5. ‘name’

This attribute can be used to specify a name for the iframe. This name can be used as the target for links to load content into the iframe.

For example


 
<iframe src="https://www.example.com" name="myIframe"></iframe>
Copy code

Output

2023_09_3.gif
Understanding HTML Doctype
Understanding HTML Doctype
Have you ever wondered about the importance of the HTML doctype declaration? The HTML doctype declaration is a crucial element at the beginning of an HTML document that defines its...read more
Concept of HTML Colors
Concept of HTML Colors
HTML Colors play a pivotal role in creating the visual identity of a webpage, influencing its aesthetics, readability, user experience, and even its brand messaging. HTML colors are a way...read more
HTML Tags: Essential and Basic Tags for Creating an HTML Document
HTML Tags: Essential and Basic Tags for Creating an HTML Document
HTML tags form the basic structure of any webpage or HTML document. In this blog, we will discuss some essential and basic HTML tags with examples. HTML tags form the...read more

6. ‘sandbox’

This attribute enables an extra set of security restrictions on the content within the iframe. It can take several values like allow-same-origin, allow-scripts, allow-popups, etc., to control the behavior of the content.

For example


 
<iframe src="https://www.example.com" ></iframe>
Copy code

Output

2023_09_4.gif

7. ‘allowfullscreen’

This boolean attribute can be used to specify whether or not to allow the iframe to be displayed in fullscreen mode.

For example


 
<iframe src="https://www.example.com" allowfullscreen></iframe>
Copy code

Output

2023_09_5.gif

8. ‘loading’

This attribute is used to defer the loading of offscreen iframes to improve page loading performance. It can have values eager (load immediately) or lazy (defer loading until the iframe is in view).

For example


 
<iframe src="https://www.example.com" loading="eager"></iframe>
Copy code

Output

2023_09_6.gif

Remember to replace “https://www.example.com” with the actual URL of the content you wish to embed in your iframe.

How to Create HTML Forms – Tutorial with Examples
How to Create HTML Forms – Tutorial with Examples
HTML forms or web forms are a powerful medium for interacting with users. They have interactive controls for submitting information. Web forms enable us to collect data or personal information...read more
HTML Attributes Explained with Examples
HTML Attributes Explained with Examples
HTML attributes provide additional information about HTML elements. They are used to modify the behavior or appearance of an element. Attributes are specified within the opening tag of an element...read more
Creating HTML Tables (Tutorial With Examples)
Creating HTML Tables (Tutorial With Examples)
This guide offers an introduction to HTML tables. You will learn how to create tables in HTML to present tabular data. Tables are a great way to present data visually....read more

Let’s see an advanced example


 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Iframe</title>
<style>
/* Adding some style to position the iframe better within the document */
.iframe-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>
</head>
<body>
<div class="iframe-container">
<iframe
src="https://www.example.com"
width="600"
height="400"
name="myIframe"
sandbox="allow-scripts allow-same-origin"
allowfullscreen
loading="lazy"
title="My Example Iframe"
style="border: 2px solid #000;">
<!-- You can add content here which will be displayed if the browser does not support iframes -->
<p>Your browser does not support iframes.</p>
</iframe>
<!-- A link that targets the iframe by its name attribute -->
<a href="https://www.example.com" target="myIframe">Load example.com in the iframe</a>
</div>
</body>
</html>
Copy code

Output

2023_09_8.gif

Thus, HTML iframes are a versatile and powerful tool in web development, allowing developers to embed documents, videos, and interactive content within a parent HTML document.

FAQs

What is an HTML iframe?

An HTML iframe (Inline Frame) is an element used to embed one HTML document within another HTML document. It allows external content such as web pages, videos, maps, or advertisements to be displayed seamlessly within a webpage.

How do you create an iframe in HTML?

To create an iframe, you use the <iframe> tag with attributes specifying the source (src) of the content and optionally set its dimensions and other properties. 

Can an iframe display content from different domains?

Yes, an iframe can display content from different domains, allowing you to embed external content seamlessly into your webpage. However, there are security considerations, such as the same-origin policy, which restricts scripts and interactions between documents from different origins for security reasons.

How do you control the appearance and behavior of an iframe?

You can control the appearance and behavior of an iframe using various attributes such as width, height, scrolling, frameborder, allowfullscreen, and more. These attributes help customize the iframe's size, scrolling behavior, border display, and other properties.

Are there any accessibility concerns when using iframes?

Yes, there are accessibility concerns when using iframes, as screen readers may not always interpret iframe content correctly. It's essential to provide alternative text or descriptions for iframe content to ensure accessibility for users with disabilities. Additionally, avoid using iframes for essential content or navigation to maintain accessibility standards.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio