HTML <iframe> Element

Publish in HTML Tutorial el 24/05/2025 15:43

The HTML <iframe> (Inline Frame) element embeds another HTML page within the current document. It creates a nested browsing context, essentially allowing you to display a webpage within a webpage. IFrames are commonly used for embedding videos, maps, advertisements, or other external content while keeping it isolated from your main page.


Key Attributes

  • src: Specifies the URL of the page to embed
  • width/height: Sets dimensions (in pixels or percentage)
  • frameborder: Shows/hides border (0 or 1, deprecated in HTML5)
  • allowfullscreen: Enables fullscreen mode
  • sandbox: Applies security restrictions
  • loading: Controls lazy loading (lazy/eager)


Basic Syntax

Here's the basic structure of an iframe:


Examples of Usage

Example 1: Embedding Google Maps

This example shows how to embed a Google Maps location:

Play Code


Example 2: Embedding YouTube Video

Embedding a YouTube video with responsive sizing:

Play Code


Example 3: Sandboxed Content

This example shows a sandboxed iframe with restricted permissions:

Play Code


Example 4: Dynamic Iframe with JavaScript

Changing iframe content dynamically with JavaScript:

Play Code


Example 5: Communication Between Parent and Iframe

Basic communication between parent window and iframe using postMessage:

Play Code


Tips and Best Practices

  • Always include title attribute for accessibility
  • Use loading="lazy" for better performance on offscreen iframes
  • Implement proper sandboxing for security with untrusted content
  • Consider using the srcdoc attribute for inline HTML content
  • For responsive iframes, use CSS with padding-bottom technique
  • Be mindful of XSS (Cross-Site Scripting) risks with iframes


Security Considerations

IFrames can pose security risks if not properly configured:

  • Use sandbox attribute to restrict capabilities
  • Set X-Frame-Options header on your own pages to prevent clickjacking
  • Be cautious with third-party content that might contain malicious scripts
  • Consider Content Security Policy (CSP) to control which sources can be framed


Performance Impact

IFrames can significantly impact page performance:

  • Each iframe creates a separate document context with its own resources
  • Browser limits the number of concurrent connections per iframe
  • Consider lazy loading for non-critical iframes
  • Measure performance impact using browser developer tools

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...