HTML - iFrame

In HTML, an inline frame (represented by the <iframe> tag) is used to embed another HTML document directly within the current web page. Iframes are widely utilized across the web to embed YouTube videos, interactive Google Maps, third-party payment gateways, advertising units, and isolated web applications.


1. Syntax and Basic Attributes

An <iframe> requires an opening tag and a closing tag. Any text placed between <iframe> and </iframe> serves as fallback text for obsolete browsers that do not support inline frames.

<iframe src="URL" title="Descriptive Title" width="600" height="400">
  Your browser does not support inline frames.
</iframe>

Original Examples: Dimensions and Border Control

<!-- Basic iframe -->
<iframe src="test.htm"></iframe>

<!-- iframe with explicit dimensions -->
<iframe src="test.htm" width="500" height="400"></iframe>

<!-- Modern borderless iframe using CSS -->
<iframe src="test.htm" style="border: none; width: 100%; height: 350px;"></iframe>

2. Essential <iframe> Attributes Reference

Attribute Requirement Purpose & Best Practice
src Required Specifies the web URL or relative path of the embedded external document.
title Mandatory (WCAG) Provides an accessible title for assistive screen readers describing the iframe's content.
sandbox Security Enables extra security restrictions on untrusted third-party code (restricting scripts, popups, and form submissions).
loading Optional (HTML5) Set to loading="lazy" to defer loading offscreen iframes until the user scrolls near them, speeding up initial page load.
allowfullscreen Optional Allows video players inside the iframe (like YouTube or Vimeo) to expand to full-screen mode.
srcdoc Optional (HTML5) Specifies inline HTML content to display inside the frame, overriding the src attribute.
name Optional Assigns a target identifier so hyperlinks (<a target="...">) can load new pages inside this frame.

3. Target a Named Iframe (Classic BCA Lab Exam Topic)

You can use an iframe as a destination window for hyperlinks on your page. By giving the iframe a name attribute, any anchor tag with a matching target attribute will open its link inside that iframe rather than navigating away from the current page:

<!-- Navigation buttons -->
<p>
  <a href="https://veewom.com/html/intro.htm" target="contentFrame">Load Introduction</a> |
  <a href="https://veewom.com/html/tags.htm" target="contentFrame">Load HTML Tags</a>
</p>

<!-- Target frame -->
<iframe name="contentFrame" src="https://veewom.com/html/intro.htm" width="100%" height="300" style="border: 1px solid #ccc;"></iframe>

4. Critical Iframe Security: The 'sandbox' Attribute

Embedding external websites poses security risks: malicious scripts inside the embedded frame could attempt to hijack your cookies, redirect your users, or trigger unwanted downloads. The sandbox attribute locks down the frame by default:

  • sandbox (empty): Applies maximum security restrictions—disables JavaScript, blocks form submissions, blocks popups, and isolates the frame in an unique origin.
  • sandbox="allow-scripts": Allows the embedded frame to run JavaScript.
  • sandbox="allow-forms": Permits the embedded frame to submit forms.
  • sandbox="allow-same-origin": Treats the frame content as being from the same origin.
  • sandbox="allow-popups": Allows the embedded frame to open new browser tabs/windows.
<!-- Securely sandboxed embedded widget -->
<iframe src="https://trusted-partner.com/widget"
        title="Partner Live Price Widget"
        sandbox="allow-scripts allow-forms"
        width="400" height="250">
</iframe>

5. Responsive Iframes for Mobile Devices (16:9 Video Aspect Ratio)

Iframes with hardcoded pixel widths break mobile layouts by causing horizontal scrolling. Modern CSS provides the aspect-ratio property to ensure embeds scale responsively across all screen sizes:

<style>
  .responsive-iframe {
    width: 100%;
    max-width: 720px;
    aspect-ratio: 16 / 9;
    border: none;
    border-radius: 8px;
  }
</style>

<iframe class="responsive-iframe"
        src="https://www.youtube.com/embed/dQw4w9WgXcQ"
        title="HTML5 Tutorial Video"
        loading="lazy"
        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
        allowfullscreen>
</iframe>

6. Hands-on Practice Session: Responsive Media & Maps Showcase

Assignment Scenario: Create a responsive contact and showcase page containing an embedded YouTube tutorial video and an interactive Google Maps location frame, both fully accessible with title attributes and modern responsive styling.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Responsive Iframe Embed Practice</title>
  <style>
    .embed-card {
      max-width: 680px;
      margin: 25px auto;
      background: #ffffff;
      border: 1px solid #e0e0e0;
      border-radius: 8px;
      padding: 20px;
      font-family: Arial, sans-serif;
      box-shadow: 0 4px 12px rgba(0,0,0,0.06);
    }
    .video-container {
      width: 100%;
      aspect-ratio: 16 / 9;
      margin: 15px 0;
    }
    .video-container iframe {
      width: 100%;
      height: 100%;
      border: 0;
      border-radius: 6px;
    }
  </style>
</head>
<body style="background-color: #f4f6f9; padding: 20px;">

  <div class="embed-card">
    <h2>Veewom Campus & Video Lecture</h2>
    <p>Watch the orientation lecture below or locate our institute using the interactive map:</p>

    <!-- Responsive YouTube Embed -->
    <div class="video-container">
      <iframe src="https://www.youtube-nocookie.com/embed/ScMzIvxBSi4" 
              title="Introduction to HTML5 Web Architecture"
              loading="lazy"
              allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
              allowfullscreen>
      </iframe>
    </div>

    <!-- Google Map Embed -->
    <h3>Campus Location Map</h3>
    <div class="video-container" style="aspect-ratio: 4 / 3;">
      <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3671.954!2d72.5714!3d23.0225!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zMjPCsDAxJzIxLjAiTiA3MsKwMzQnMTcuMCJF!5e0!3m2!1sen!2sin!4v1600000000000"
              title="Veewom Learning Center Location Map"
              loading="lazy"
              referrerpolicy="no-referrer-when-downgrade">
      </iframe>
    </div>
  </div>

</body>
</html>

7. BCA University Exam Viva Questions

An <iframe> (inline frame) embeds an independent external HTML document inside the body of an existing web page. In contrast, the legacy <frameset> tag replaced the entire document body with a fixed grid of frames, which caused severe usability and bookmarking issues and was officially deprecated in HTML5.
Clickjacking is a cyber attack where a malicious website loads a target site (e.g., a banking portal) inside an invisible, transparent iframe over enticing buttons, tricking users into making unintended clicks. Webmasters prevent this by configuring the HTTP response header X-Frame-Options: SAMEORIGIN (or DENY) or using the Content Security Policy (CSP) directive frame-ancestors 'self'.
Screen readers announce the presence of an inline frame to visually impaired users by reading its title attribute (e.g. "Google Maps navigation frame"). Without a title, users have no idea what content the frame contains without tediously navigating into its inner DOM tree.
The sandbox attribute puts the iframe into an isolated environment with restricted privileges: it disables JavaScript, prevents form submissions, blocks window popups, and disables plugins. Developers can selectively re-enable specific features by passing values like allow-scripts or allow-forms.
The src attribute points to an external URL or file path to download. The srcdoc attribute allows you to embed raw HTML code directly inline inside the attribute string. If both are specified, the browser renders the srcdoc content and uses src as a fallback for older browsers.

8. Frequently Asked Questions (FAQ)

Major websites like Google, Facebook, and Twitter send security headers (X-Frame-Options: DENY or SAMEORIGIN) that strictly forbid other websites from displaying their pages in an iframe. You can only embed pages from servers that allow cross-origin framing or provide dedicated embed endpoints (like YouTube's /embed/ URL).
Use inline CSS: <iframe src="..." style="border: 0;"></iframe> or in a stylesheet set iframe { border: none; }. The legacy HTML attribute frameborder="0" is obsolete in HTML5.
Yes, because each iframe initiates multiple HTTP requests, scripts, and stylesheet downloads. To prevent PageSpeed penalties, always include loading="lazy" on all iframes located below the fold so they only fetch when scrolled near view.
Only if both the parent page and the iframe share the exact same protocol, domain, and port (Same-Origin Policy). If they belong to different domains, cross-document DOM access is blocked by browsers, though safe communication can still occur using the window.postMessage() API.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!