• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Resize an iframe to fit its content

Resize an iframe to fit its content

Resize an iframe to fit its content

This snippet gets the height of an iframe’s content and then resizes the height of the iframe to match.

Assume that frame represents the iframe element.

frame.addEventListener('load', function() {
    // Get the height of the content
    const height = frame.contentDocument.body.scrollHeight;

    // Set the height of iframe
    frame.setAttribute('height', `${height}px`);
});

Source

https://htmldom.dev/resize-an-iframe-to-fit-its-content/

Miscellaneous

Related Snippets:

  • Multiple Choice Quiz
  • Get an attribute on an element
  • Check if the given string contains any whitespace characters
  • Remove a Character From a String in JavaScript

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers