@FahadM to JavaScriptEnglish • 4 days ago

How do you create a responsive full-screen iframe with 100% height and width?

message-square
0
1
How do you create a responsive full-screen iframe with 100% height and width?
@FahadM to JavaScriptEnglish • 4 days ago

To create a responsive full-screen <iframe> with 100% height and width in HTML and CSS, reset outer page margins (margin: 0) and set width: 100vw and height: 100dvh (or 100% on html, body paired with position: fixed; inset: 0;), while setting border: none and display: block so the frame fills the viewport cleanly without unwanted scrollbars or inline baseline gaps. Using 100dvh (dynamic viewport height) alongside 100% fallback handles mobile browser address-bar resizing more reliably than 100vh alone.


1. Full-Viewport Fixed <iframe> Layout

Positioning the <iframe> with position: fixed; inset: 0; and width: 100%; height: 100%; anchors every edge to the viewport and prevents parent overflow issues:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Full-Screen Iframe</title>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
      overflow: hidden;
    }

    .fullscreen-frame {
      position: fixed;
      inset: 0;
      width: 100%;
      height: 100%;
      height: 100dvh;
      border: none;
      display: block;
    }
  </style>
</head>
<body>
  <iframe
    class="fullscreen-frame"
    src="https://example.com"
    title="Embedded full-screen view"
    loading="lazy"
    allowfullscreen
  ></iframe>
</body>
</html>
  • Use inset: 0 with 100% / 100dvh: Pins top, right, bottom, and left edges cleanly across desktop and mobile viewports.
  • Always include a descriptive title: Screen readers rely on the title attribute to identify what the <iframe> embeds.

2. Full-Screen Container Approach vs. Fixed Positioning

When the <iframe> lives inside a layout shell (such as a header bar or sidebar) rather than occupying the entire browser window from pixel (0, 0), size a flex or grid wrapper to 100dvh and let the <iframe> fill flex: 1; width: 100%; height: 100%;:

/* Layout shell with a compact top bar and full-height remaining iframe */
.shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
}

.shell-header {
  flex: 0 0 auto;
}

.shell-frame {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}
  • Avoid 100vw when horizontal scrollbars or gutters matter: In some environments, 100vw can include scrollbar width or cause minor horizontal overflow, whereas 100% inside a non-overflowing root is exact.
  • Set display: block: Inline elements like <iframe> sit on the text baseline by default, which can add a tiny bottom gap if not set to block.

3. Security, Sandbox, and Embedding Gotchas

Many websites send X-Frame-Options: DENY / SAMEORIGIN or a restrictive Content-Security-Policy: frame-ancestors header that blocks embedding in an <iframe>, so always verify cross-origin embedding permissions and apply sandbox and referrerpolicy attributes thoughtfully:

<!-- Harden cross-origin iframe embedding when needed -->
<iframe
  class="fullscreen-frame"
  src="https://example.com/widget"
  title="Interactive embedded application"
  sandbox="allow-scripts allow-same-origin allow-forms"
  referrerpolicy="strict-origin-when-cross-origin"
  allow="fullscreen"
></iframe>
  • Check frame-ancestors headers if blank: If a third-party site refuses to load inside your <iframe>, inspect the network response headers for X-Frame-Options or Content-Security-Policy.
  • Combine sandbox carefully: Adding allow-scripts and allow-same-origin together lets a same-origin frame drop its sandbox restrictions—only grant the exact permissions the embedded page needs.