The Wayback Machine - https://web.archive.org/web/20240626154835/https://www.geeksforgeeks.org/html-iframes/
Open In App

HTML Iframes

Last Updated : 22 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

HTML iframes offer a powerful way to embed external content, such as videos, maps, or other webpages, directly into your own webpage. This article provides an in-depth exploration of HTML iframes, their syntax, and how they can be used to enhance your web development projects.

What are HTML Iframes?

An iframe is an HTML document embedded inside another HTML document. The <iframe> tag specifies the URL of the embedded content, allowing for seamless integration of external resources. This section will guide you through the syntax and attributes of the <iframe> tag, providing a solid foundation for your iframe usage.

Syntax:

<iframe src="URL" title="description"></iframe>
  • The src attribute specifies the URL of the document you want to embed.
  • Iframes can include videos, maps, or entire web pages from other sources.

Attributes Value:

It contains a single value URL that specifies the URL of the document that is embedded in the iframe. There are two types of URL links which are listed below:

URL

Descriptions

Absolute URL

It points to another webpage.

Relative URL

It points to other files of the same web page.

Supported Attributes:

AttributesDescription
allowSpecifies a set of extra restrictions on the content that can be loaded in an <iframe>.
allowfullscreenIndicates whether the <iframe> can be displayed in fullscreen mode.
allowpaymentrequestEnables payment requests for content inside the <iframe>.
heightSets the height of the <iframe> element.
widthSets the width of the <iframe> element.
loadingSpecifies how the content of the <iframe> should be loaded.
scrollingControls whether or not the <iframe> should have scrollbars.
name Specifies the name of the <iframe> for targeting its content or for referencing it in JavaScript.
referrerpolicySets the referrer policy for the <iframe> content.
sandboxSpecifies an extra set of restrictions for the content in the <iframe>.
srcSpecifies the URL of the document to embed in the <iframe>.
srcdocSpecifies the HTML content of the page to display in the <iframe>.

HTML Iframes Examples

Example 1: This example illustrates the use of an iframe tag which is used to display a webpage inside the current web page.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>HTML iframe Tag</title>
</head>

<body style="text-align: center">
    <h2>HTML iframe Tag</h2>
    <iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
            height="370"
            width="400">
    </iframe>
</body>

</html>

Output:


HTML-Iframe-2

HTML Iframes Example Output


2. Using Height and Width attribute:

The height and width attributes are used to specify the size of the iframe. The attribute values are specified in pixels by default. You can use pixels or percentages (e.g., “80%”).

Example : This example describes the HTML iframe Tag by setting the width & height of the iframe.

HTML
<!DOCTYPE html>
<html>

<body>
    <h2>HTML iframe Tag</h2>

    <p>
        Content goes here
    </p>

    <iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
            height="395" 
            width="400">
    </iframe>
</body>

</html>

Output:


HTML-Iframe-2

HTML Iframes Example Output


3. Removing Borders from Iframe

By default, iframe has a border around it. To remove the border, we must use the style attribute and use the CSS border property.

Example : This example describes the HTML iframe Tag where the border property is set as none.

HTML
<!DOCTYPE html>
<html>

<body>
    <h2>HTML iframe Tag</h2>
    <p>Content goes here</p>

    <iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231227155729/jsonPrac3.html" 
            height="300" 
            width="400" 
            style="border: none"> 
    </iframe>
</body>

</html>

Output:


HTML-Iframe-3

HTML Iframes Example Output


4. Iframe Border Style

Changing the size, style, and color of the Iframe’s border.

Example : This example describes the HTML iframe Tag by specifying the border style.

HTML
<!DOCTYPE html>
<html>

<body>
        
    <p>Content goes here</p>
    <iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html" 
            height="400" 
            width="400" 
            style="border: 4px solid orange"> 
    </iframe>
</body>

</html>

Output:


HTML-Iframe-4

HTML Iframes border style Example Output


5. Iframe Target in 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: This example describes the HTML iframe Tag by using the target frame for a link.

HTML
<!DOCTYPE html>
<html>

<body>
 
    <h2>HTML iframe Tag</h2>

    <p>
        Click the link text
    </p>

    <iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210910170539/gfg-221x300.png"
            height="400"
            width="350" 
            name="iframe_a">
    </iframe>

    <p>
        <a href=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
           target="iframe_a">
            Converter
        </a>
    </p>
</body>

</html>

Output:


HTML-Iframe-5

HTML Iframes Example Output


Supported Browsers:

In Conclusion, HTML iframes serve as a versatile tool in web development, enabling the integration of external content directly into your webpage. With a clear understanding of the <iframe> tag and its attributes, you can leverage iframes to enhance the functionality and user experience of your web projects.



Previous Article
Next Article

Similar Reads

How to create Responsive iFrames using CSS ?
An iframe, or inline frame, is an HTML element used to embed another document or webpage within the current document. Responsive iframes can be achieved by defining the aspect ratio, which refers to the proportional relationship between the width and height of an element. To maintain the aspect ratio we will use padding-top Property. SyntaxTo calcu
4 min read
HTML | DOM HTML Object
The HTML Object property in HTML DOM is used to represent or access the HTML &lt;html&gt; element with in the object. The &lt;html&gt; element is used to return the HTML document as an Element Object. Syntax: It is used to access a &lt;html&gt; element.var x = document.getElementsByTagName("HTML")[0]; It can also be used to access a &lt;html&gt; el
3 min read
HTML | &lt;html&gt; xmlns Attribute
The HTML &lt;html&gt; xmlns Attribute is used to specify the xml namespace for a document. Important Note: This attribute is mainly required in XHTML, not valid in HTML 4.01, and optional in HTML 5. Syntax: &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; Attribute Values: https://www.geeksforgeeks.org/ It defines the namespace to use (for XHTML d
1 min read
Inline HTML Helper - HTML Helpers in ASP.NET MVC
HTML Helpers are methods that returns HTML strings. These are used in the view. In simple terms, these are C# methods that are used to return HTML. Using HTML helpers you can render a text box, an area, image tag, etc. In MVC we have many built-in HTML helpers and we can create custom helpers too. Using HTML helpers a view can show model properties
2 min read
How to return HTML or build HTML using JavaScript ?
JavaScript is very powerful and with it, we can build dynamic web content and add many features to a web application. With HTML, we create the structure of the web page and the same thing can also be done with JavaScript. There are a few JavaScript methods called as createElement(), appendChild() with which we can add nodes to the DOM tree on the f
2 min read
How to convert html.slim to html.erb ?
Introduction to slim and erb: Slim files and tools are used to make the code more light and will help to use it instead of the particular code. erb is also used to do the similar but if you want to make the changes in the code then the user should be familiar with slim/erb respectively. If you have to convert an HTML Document into html.slim file th
1 min read
How to include HTML code snippets in HTML ?
In this article, we will learn how to include HTML snippets in HTML code. We will include the HTML code snippet of "gfg.html" into "index.html". To achieve this task, we are going to write a JavaScript function in "index.html" which traverses the collection of all HTML elements in "gfg.html" and searches for elements with specific attributes. It cr
3 min read
How to display HTML tags as plain text in HTML ?
In this article, we will learn how to display HTML tags as plain text in HTML. We can replace the plain text by using &lt; with   &amp;lt; or &amp;60; and &gt;   with  &amp;gt; or &amp;62; on each HTML tag. HTML Entities:Sign DescriptionEntity nameEntity number&lt;Less than(start of HTML element)&amp;lt;&lt;&gt;Greater than(end of HTML element)
2 min read
What is the difference between &lt;html lang="en'&gt; and &lt;html lang="en-US'&gt; ?
The lang attribute specifies which language is used to write the content of a web page. It is used to set the language for the whole text of the web page. The &lt;html lang="en"&gt; specifies the language of the document as English, while &lt;html lang="en-US"&gt; narrows it down to English as used in the United States. Table of Content &lt;html la
2 min read
HTML Complete Guide – A to Z HTML Concepts
What is HTML ? HTML stands for Hypertext Markup Language. It is a standard markup language used to design the documents displayed in the browsers as a web page. This language is used to annotate (make notes for the computer) text so that a machine can understand it and manipulate text accordingly. Most markup languages (e.g. HTML) are human-readabl
7 min read
three90RightbarBannerImg