HTML iframe Tag: How to Embed Content with Examples

html iframe tag

The iframe tag embeds another HTML page inside the current page. It loads content from a separate source.

Understand the <iframe> Tag in HTML

An <iframe> creates a rectangular region inside a page. This region displays another document from a different URL. The user can scroll or interact with it like any page.

You can use the <iframe> tag to load the following external sources:

  • Maps
  • Ads
  • Videos
  • Full web pages

Here is the basic tag of <iframe>:

<iframe src="URL" width="value" height="value" title="description"></iframe>
  • src: The URL of the page to show.
  • width: Width in pixels or percent.
  • height: Height in pixels or percent.
  • title: Describes the frame for accessibility.

For example:

<iframe src="https://example.com" width="600" height="400" title="Example Site"></iframe>

This loads ā€œexample.comā€ inside the current web page. The frame has a width of 600px and a height of 400px. Screen readers use the title for context.

The <iframe> Tag Attributes in HTML

You can use extra attributes to control security and behavior.

  • sandbox limits access. Blocks scripts and form submissions.
  • allow grants special permissions like camera or fullscreen.
  • referrerpolicy controls what referrer to send.
  • srcdoc replaces src loads of HTML code directly.
  • allowfullscreen let the frame enter full screen.

Here’s a structure with advanced options:

<iframe src="https://example.com" width="500"  height="300"    title="Advanced Example" sandbox allow="fullscreen; microphone"  referrerpolicy="no-referrer" allowfullscreen></iframe>

Main Attributes:

  • src sets the source page.
  • width sets the width.
  • height sets the height.
  • title describes the content.
  • name assigns a name to the frame.
  • loading chooses lazy or eager loading.

Here are the use cases:

  • Use src to load a remote or internal page.
  • Use width and height to fix layout space.
  • Use title for screen reader users.
  • Use name to target the frame in links or scripts.
  • Use loading="lazy" to load after scroll. It improves load time.

Here are two examples that show you these attribute usage:

Basic Frame With Lazy Load:

<iframe src="https://maps.google.com" width="600" height="400"  title="Map" loading="lazy"> </iframe>

This loads a map, and it delays the load until the user scrolls. It improves speed on large pages.

Frame With Target and Name

<iframe 
  src="page.html" 
  name="contentFrame" 
  width="400" 
  height="300" 
  title="Local Page">
</iframe>

<a href="other-page.html" target="contentFrame">Load New Page</a>

This creates a link that loads inside the frame. The frame uses the name to catch the link’s target.

Examples

Embed YouTube Video:

<iframe 
  width="560" 
  height="315" 
  src="https://www.youtube.com/embed/dQw4w9WgXcQ" 
  title="YouTube Video" 
  allowfullscreen>
</iframe>

This shows a YouTube video inside a frame. It fits in a fixed-size box. The allowfullscreen tag lets users open the video full screen.

Use srcdoc Instead of URL:

<iframe 
  width="400" 
  height="200" 
  srcdoc="<h1><iframe> with srcdoc</h1><p>This comes from HTML code.</p>" 
  title="Inline HTML">
</iframe>

This loads custom HTML without a URL. It shows text from the srcdoc. The content is self-contained and does not rely on another page.

Sandbox With Restricted Access:

<iframe 
  src="https://example.com" 
  width="500" 
  height="300" 
  title="Sandboxed Frame" 
  sandbox>
</iframe>

This loads a page but blocks scripts and plugins. It adds security and keeps the frame isolated.

Frame With Microphone and Fullscreen Access:

<iframe 
  src="https://example.com/app" 
  width="800" 
  height="600" 
  title="Advanced Permissions" 
  allow="microphone; fullscreen" 
  allowfullscreen>
</iframe>

This gives the page access to the microphone and full screen. It is useful for apps or video calls.

Wrapping Up

In this article, you learned what an <iframe> is and how to use it. You saw real code and use cases. You learned about both basic and advanced attributes.

Here is a quick recap:

  • <iframe> loads another page inside a frame.
  • src, width, height, and title are essential.
  • sandbox, allow, srcdoc, and loading control behavior.
  • Use name to target the frame from links.
  • Choose srcdoc to embed inline HTML.
  • Use allowfullscreen for video and game content.

FAQs

What does <iframe> mean in HTML?

It stands for inline frame. It loads another page into a part of the current page. It uses the <iframe> element with a src attribute.
<iframe src="page.html"></iframe>

How do I use <iframe> to embed a YouTube video?

Use the video link in the src attribute. Also use allowfullscreen to let users expand it.
<iframe src="https://www.youtube.com/embed/ID" allowfullscreen></iframe>

What is the use of the srcdoc attribute in <iframe>?

It loads custom HTML directly inside the frame. It ignores the src URL.
<iframe srcdoc="<h1>Hello</h1>"></iframe>

What is the difference between <frame> and <iframe>?

<frame> is obsolete. It worked with frameset. <iframe> is part of HTML5. Use <iframe> for all modern layouts.
<iframe src="file.html"></iframe>

Similar Reads

HTML dir Attribute: How to Set Text Direction

The dir attribute tells the browser which way text should go in HTML. You use it when a page or…

HTML Table Tag: How to Build Tables with Its Elements

You use the HTML <table> tag to display structured data in rows and columns, using table elements such as <tr>…

HTML abbr Tag: How Abbreviations Work with Examples

The abbr tag refers to an abbreviation in HTML. It helps you save space and improve accessibility. The screen readers…

HTML main Tag: How to Create a Primary Content Area

The <main> tag in HTML marks the central content of a webpage. It tells browsers and assistive technologies where the…

HTML audio Tag: How to Embed and Control Sound in Pages

The audio tag adds sound to web pages without plugins. It gives you control to play, pause, loop, or mute…

HTML hr Tag: Horizontal Rules

The <hr> tag in HTML gives you a fast way to split content with a clean horizontal line. You may…

Comments in HTML: How They Work with Examples

Comments in HTML help you explain your code. You can add notes without showing anything in the browser. Understand How…

HTML meter Tag: Measurements with Examples

Purpose and use cases: The HTML meter tag shows a scalar value within a known range. You can use it…

HTML Span Tag: Inline Container Explained

The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…

HTML Legend Tag: How it Works in Fieldset Tag with Examples

In this article, you will learn how HTML legend works in the fieldset tag with examples. HTML fieldset and legend…

Previous Article

HTML object Tag: How to Embed External Objects with Examples

Next Article

HTML figure Tag: Responsive Design & Media Elements

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.