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 Footer Tag for Document Footers

The <footer> tag defines a footer section in HTML. It creates a clear endpoint for a page or section. Browsers…

HTML sup Tag: How to Add Superscript in Web Pages

The HTML sup tag puts text above the normal line. Writers use it for math power and marks. So, readers…

HTML Global Attributes Overview

HTML global attributes work on most elements. They do not depend on the tag name. You can use them with…

HTML Link Attributes Guide for Beginners

HTML link attributes tell the browser how a link works. They set where the link goes, how it opens, and…

HTML figure Tag: Responsive Design & Media Elements

The HTML figure tag links media with a caption. Use it to group an image, diagram, code block, or video…

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…

The Button Tag in HTML: How it Works with Examples

The HTML button tag creates interactive controls on a web page. You can use this element to trigger actions or…

HTML code Tag: How to Mark Up Code Snippets

The <code> tag in HTML shows short code, command names, or output. Use it to display text that the browser…

HTML bdi Tag: Control Text Direction in HTML

The HTML bdi tag marks text that has an unknown or mixed direction. It makes sure that this part does…

HTML pre Tag: How to Display Preformatted Text

The HTML <pre> tag keeps the original format of your text. It shows spaces and line breaks exactly as you…

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.