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

HTML Iframes

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

HTML iframes are used to embed external content such as videos, maps, or entire web pages into their web projects. This article will cover the syntax, attributes, and use cases of iframes, providing a solid foundation for leveraging them in web development.

What are HTML Iframes?

An iframe (short for inline frame) is an HTML element used to embed another HTML document inside the current document. The <iframe> tag specifies the URL of the external content, enabling seamless integration of videos, maps, forms, or entire web pages into your site.

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.

URL Types for <iframe>

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 of the <iframe> Tag

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: Basic Iframe Embedding

In this example, an iframe is used to display another webpage within the current webpage.

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 2: Setting Width and Height

The width and height attributes allow you to control the iframe’s dimensions. The default unit is pixels, but percentages can also be used to create responsive iframes.

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 3: Removing Iframe Borders

By default, iframes have borders. You can remove or style these borders using CSS.

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 4: Customizing Iframe Border Style

You can change the size, style, and color of the iframe border using CSS.

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: Using an Iframe as a Target for a Link

You can target an iframe with links by using the name attribute of the iframe and the target attribute of the 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:

output-fig-1

HTML iframe Tag by using the target frame for a link.

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.

HTML Iframes – FAQs

What is an iframe?

An <iframe> embeds another HTML document within the current page, allowing you to display external content like videos or webpages.

How do you create an iframe?

Use <iframe src=”URL”></iframe> to embed content from the specified URL. Optionally, add attributes for width, height, and other settings.

What attributes can you use with iframes?

Common attributes include src (URL), width, height, title, frameborder, allowfullscreen, and sandbox.

How do you set the size of an iframe?

Use the width and height attributes to specify the iframe’s dimensions, e.g., <iframe width=”600″ height=”400″></iframe>.

What is the sandbox attribute?

The sandbox attribute restricts iframe content with security features like disabling scripts or forms. Use values like “allow-scripts” to lift specific restrictions.

Can you control iframe content with JavaScript?

Yes, you can interact with iframe content via JavaScript using the contentWindow and contentDocument properties if same-origin policies allow.



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
5 min read
HTML | DOM HTML Object
The HTML Object property in HTML DOM is used to represent or access the HTML <html> element with in the object. The <html> element is used to return the HTML document as an Element Object. Syntax: It is used to access a <html> element.var x = document.getElementsByTagName("HTML")[0]; It can also be used to access a <html> el
3 min read
HTML | <html> xmlns Attribute
The HTML <html> 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: <html xmlns="http://www.w3.org/1999/xhtml"> 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 < with   &lt; or &60; and >   with  &gt; or &62; on each HTML tag. HTML Entities:Sign DescriptionEntity nameEntity number<Less than(start of HTML element)&lt;<>Greater than(end of HTML element)
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
HTML Course Basics of HTML
In this article, we will go through all the basic stuff required to write HTML. There are various tags that we must consider and know about while starting to code in HTML. These tags help in the organization and basic formatting of elements in our script or web pages. These step-by-step procedures will guide you through the process of writing HTML.
5 min read
HTML Course Structure of an HTML Document
HTML(Hypertext Markup Language), structures web pages. Unlike programming languages, it isn't compiled or interpreted. Browsers render HTML directly, displaying content without typical errors. It's a markup language, not a programming one, making execution smooth without encountering compilation or interpretation issues. Table of Content HTML Docum
3 min read
What is the difference between <html lang="en'> and <html lang="en-US'> ?
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 <html lang="en"> specifies the language of the document as English, while <html lang="en-US"> narrows it down to English as used in the United States. Table of Content The < ht
2 min read
HTML Code to JSON Converter App Using HTML, CSS, and JavaScript
HTML tables are a common way to represent structured data on web pages, but transforming this data into a JSON format can be troublesome. JSON is a lightweight data-interchange format that is easy for humans to read and write and for machines to parse and generate. We will be creating the HTML Code for the JSON Converter tool. The converter is desi
3 min read
HTML <html> Tag
HTML is a language full of diverse elements, and one such element is the <html> tag. This tag, standing for ‘HyperText Markup Language’, is used to define the root of an HTML or XHTML document. It serves as the main container for all other HTML elements, excluding the <!DOCTYPE> declaration. Understanding the <html> TagThe <htm
3 min read
Web technologies | HTML and XML | Question 1
Which of the following is an advantage of putting presentation information in a separate CSS file rather than in HTML itself? (A) The content becomes easy to manage (B) Becomes easy to make site for different devices like mobile by making separate CSS files (C) CSS Files are generally cached and therefore decrease server load and network traffic. (
1 min read
HTML | scoped Attribute
The HTML scoped attribute is a boolean attribute that is used to specify that the styles only apply to this element's parent element and that element's child elements (not the entire document).Note : It is a deprecated attribute and it is not supported by HTML5. Supported tag: style Example: C/C++ Code <!DOCTYPE html> <html> <head
1 min read
HTML DOM lang Property
In HTML document, the lang property is used to set or return the value of the lang attribute of an element. This attribute specifies the language code of the element's content. The element attribute takes "en" for English, "ja" for Japanese, "es" for Spanish, and so on. The default value of this attribute is unknown. Syntax: Get the value of lang:
2 min read
HTML DOM Style backgroundClip Property
The DOM style backgroundClip Property is used to set or return the painting area of the background. Syntax: It is used to return the backgroundClip property.object.style.backgroundClip It is used to set the backgroundClip property. object.style.backgroundClip = "border-box|padding-box|content-box|initial|inherit" Property Values: border-box propert
1 min read
HTML DOM Style borderTop Property
The DOM style borderTop property is used to set or return the three different border-top property such as border-top-width, border-top-style, and border-top-color of an element. Syntax: It returns the borderTop property. object.style.borderTopIt is used to set the borderTop property. object.style.borderTop = "width style color|initial|inherit" Prop
2 min read
Web technologies Questions | HTML Course | Practice Quiz 2 | Question 1
What is the full form of CSS? (A) Color style sheet (B) Cascading style sheet (C) Creative style sheet (D) Concatenated style sheet Answer: (B) Explanation: Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Web technologies Questions | HTML Course | Practice Quiz 2 | Question 7
Which HTML tag is used for internal style sheet? (A) Internal (B) Style (C) CSS (D) Internal Style Answer: (B) Explanation: Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Web technologies Questions | HTML Course | Practice Quiz 2 | Question 8
Which of the following is the correct CSS syntax? (A) p:color=green, font-size=15px (B) {p:color=green, font-size=15px} (C) p {color:green, font-size:15px;} (D) p {color:green; font-size:15px;} Answer: (D) Explanation: Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Web technologies Questions | HTML Course | Practice Quiz 2 | Question 9
Select the correct among the following for linking an external style sheet. (A) <link rel=“stylesheet” type=“text/css” href=/html-course-practice-quiz-2/>(B) <style rel=“stylesheet” type=“text/css” href=/html-course-practice-quiz-2/>(C) <link>stylesheet (D) <link href=/html-course-practice-quiz-2/> Answer: (A) Explanation: Quiz of th
1 min read
HTML | <th> valign Attribute
The HTML <th> valign Attribute is used to specify the vertical alignment of text content in a header cell. It is not supported by HTML 5. Syntax: <th valign="top | middle | bottom | baseline"> Attribute Value: top: It sets the table header content to top-align. middle: It sets the table header content to middle-align. bottom: It sets th
1 min read
HTML | <col> align Attribute
The HTML <col> align Attribute is used to set the horizontal alignment of text content inside the col element. It is not supported by HTML 5. Syntax: <col align="left | right | center | justify | char"> Attribute Values: left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align. justif
1 min read
HTML | DOM Meter max Property
The DOM Meter max Property is used to set or return the value of the max attribute of a gauge. The max attribute is used to specify the upper bound of the gauge and the value of the max attribute must be greater than the value of the min attribute. It has a default value which is 1. Syntax: It return the max property.meterObject.maxIt is used to se
2 min read
HTML | poster Attribute
The HTML poster Attribute is used to display the image while video downloading or when the user clicks the play button. If this image not set then it will take the first frame of video as a poster image.Applicable: It is mainly used in <video> element. Syntax: <video poster="URL"> Attribute Values: It contains a single value URL which s
1 min read
HTML DOM fullscreenerror Event
The HTML DOM fullscreenerror event occurs when an element can not be viewed in full-screen mode, even if it has been requested. Use the element.requestFullscreen() method to view an element in fullscreen mode and the element.exitFullscreen() method to cancel fullscreen mode. Supported Tags It supports all HTML elements. Syntax: In HTML: <element
1 min read
HTML | DOM onvolumechange Event
The onvolumechange event in HTML DOM occurs when the media volume is changed. Use volume property to set/return the volume of the media.This event is invoked by: Volume increasing/decreasingMuting/unmuting the media player Supported Tags <audio><video> Syntax: In HTML: <element onvolumechange="myScript">In JavaScript: object.onvol
1 min read
HTML | DOM Input Hidden value Property
The Input Hidden value property in HTML DOM is used to set or return the value of the value attribute of the hidden input field. The value attribute defines the default value of the input hidden field. Syntax: It returns the value property.hiddenObject.valueIt is used to set the value property.hiddenObject.value = text Property Values: This propert
2 min read