HTML Iframes

6 min read HTML 10 views

HTML Iframes

HTML iframes are used to embed another webpage or external content inside the current webpage. The <iframe> (Inline Frame) element creates a rectangular area where another document can be displayed.

Iframes are commonly used to embed:

  • YouTube videos.
  • Google Maps.
  • External webpages.
  • PDF documents.
  • Online applications and widgets.
  • Other supported external content.

1. Basic HTML Iframe

The <iframe> element is used to embed another webpage within an HTML document.

Example:

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

Explanation:

  • src specifies the URL of the webpage to embed.
  • width sets the iframe width.
  • height sets the iframe height.

2. HTML Iframe Attributes

The <iframe> element supports several attributes to control its size, behavior, permissions, and appearance.

Attribute Description
srcSpecifies the URL of the embedded document.
widthDefines the iframe width.
heightDefines the iframe height.
titleProvides an accessible description of the iframe.
nameAssigns a name to the iframe for use as a browsing target.
loadingControls when the iframe should load.
allowSpecifies permissions for supported browser features.
allowfullscreenAllows the embedded content to enter fullscreen mode.
sandboxApplies security restrictions to the embedded content.
referrerpolicyControls the referrer information sent when loading the iframe.

3. Set Iframe Width and Height

You can specify the dimensions of an iframe using the width and height attributes or CSS properties.

Example:

<iframe
    src="https://example.com"
    width="800"
    height="450"
    title="Example Website">
</iframe>

4. Embed a YouTube Video

You can embed YouTube videos into your webpage using the <iframe> element. YouTube provides an embed URL for each video.

Example:

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

Replace VIDEO_ID with the actual YouTube video ID.

5. Make an Iframe Responsive

A responsive iframe adjusts to the available screen width. You can use CSS to make embedded content suitable for desktop, tablet, and mobile devices.

Example:

<style>
    .iframe-container {
        width: 100%;
        aspect-ratio: 16 / 9;
    }

    .iframe-container iframe {
        width: 100%;
        height: 100%;
        border: none;
    }
</style>

<div class="iframe-container">
    <iframe
        src="https://www.youtube.com/embed/VIDEO_ID"
        title="Responsive YouTube Video"
        allowfullscreen>
    </iframe>
</div>

6. Embed Google Maps

You can display an interactive Google Map on your webpage using an iframe. Google Maps provides an embed option for generating the required HTML code.

Example:

<iframe
    src="YOUR_GOOGLE_MAPS_EMBED_URL"
    width="600"
    height="450"
    style="border:0;"
    loading="lazy"
    allowfullscreen
    referrerpolicy="no-referrer-when-downgrade"
    title="Google Maps Location">
</iframe>

Replace YOUR_GOOGLE_MAPS_EMBED_URL with the embed URL provided by Google Maps.

7. Embed a PDF Document

You can display a PDF document inside a webpage using an iframe, provided the browser supports PDF viewing and the server allows the resource to be embedded.

Example:

<iframe
    src="document.pdf"
    width="100%"
    height="500"
    title="PDF Document">
</iframe>

8. HTML Iframe Border

You can customize or remove an iframe border using CSS. The modern approach is to use the border property.

Example:

<iframe
    src="https://example.com"
    title="Example Website"
    style="width:100%; height:300px; border:2px solid #2563eb;">
</iframe>

9. HTML Iframe Loading Attribute

The loading attribute controls when an iframe is loaded by the browser.

  • eager: Loads the iframe immediately.
  • lazy: Delays loading until the iframe approaches the visible area.

Example:

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

10. HTML Iframe Sandbox Attribute

The sandbox attribute applies additional security restrictions to the content inside an iframe. By default, it restricts several potentially unsafe actions.

You can selectively allow certain features using sandbox tokens.

Example:

<iframe
    src="https://example.com"
    sandbox
    title="Restricted Embedded Content">
</iframe>

Common sandbox tokens:

Token Purpose
allow-scriptsAllows scripts to run inside the iframe.
allow-formsAllows form submission.
allow-popupsAllows popups to open.
allow-downloadsAllows downloads initiated by the embedded content.
allow-same-originAllows the embedded document to retain its origin instead of receiving a special opaque origin.
Note: Use sandbox permissions carefully. Only grant the permissions that embedded content actually needs. Combining allow-scripts and allow-same-origin for same-origin content can significantly weaken sandbox protection.

11. Iframe Target Attribute

The name attribute gives an iframe a browsing context name. You can use that name as the target value of a link or form to display its result inside the iframe.

Example:

<iframe
    name="myframe"
    width="600"
    height="300"
    title="Embedded Page">
</iframe>

<p>
    <a href="https://example.com" target="myframe">
        Open Website Inside Iframe
    </a>
</p>

12. Important Iframe Security Considerations

When embedding external websites, it is important to understand iframe security and browser restrictions.

  • Some websites prevent iframe embedding using security headers such as X-Frame-Options or Content Security Policy.
  • Cross-origin restrictions prevent ordinary JavaScript from freely accessing another website's iframe content.
  • Use the sandbox attribute when embedding untrusted content.
  • Use a meaningful title attribute for accessibility.
  • Only embed content from trusted sources.
  • Use HTTPS URLs whenever possible.

Conclusion

HTML iframes allow developers to embed external webpages, videos, maps, PDF documents, and other supported content inside their websites. By understanding iframe attributes, responsive design, loading behavior, and security restrictions, you can use iframes effectively in modern web development.

html Example
Run Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Iframes</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f1f5f9;
            padding: 20px;
        }

        .container {
            max-width: 750px;
            margin: auto;
            background: white;
            padding: 25px;
            border-radius: 12px;
        }

        h2 {
            color: #2563eb;
        }

        .iframe-box {
            width: 100%;
            aspect-ratio: 16 / 9;
            margin-bottom: 25px;
        }

        iframe {
            width: 100%;
            height: 100%;
            border: 2px solid #cbd5e1;
            border-radius: 8px;
        }
    </style>
</head>
<body>

<div class="container">

    <h1>HTML Iframes Tutorial</h1>

    <h2>1. Embed a Website</h2>

    <div class="iframe-box">
        <iframe
            src="https://www.wikipedia.org/"
            title="Wikipedia Website">
        </iframe>
    </div>

    <h2>2. Embed a YouTube Video</h2>

    <div class="iframe-box">
        <iframe
            src="https://www.youtube.com/embed/VIDEO_ID"
            title="YouTube Video"
            allowfullscreen>
        </iframe>
    </div>

    <h2>3. Embed a Google Map</h2>

    <div class="iframe-box">
        <iframe
            src="YOUR_GOOGLE_MAPS_EMBED_URL"
            title="Google Map">
        </iframe>
    </div>

    <h2>4. Embed a Local HTML Page</h2>

    <div class="iframe-box">
        <iframe
            src="about.html"
            title="Local HTML Page">
        </iframe>
    </div>

</div>

</body>
</html>

Related Tutorials