• 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:

  • Check if all elements in an array are equal
  • Open or close an element in fullscreen mode
  • Insert an element at the beginning of a set elements inside a shared parent
  • Find the difference between two arrays with JavaScript

Primary Sidebar

JSON Compare

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