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

  • Get and set the text of an element (without markup)
  • Check if two arrays or objects are equal
  • Make a resizable element
  • How to change the value of an object which is inside an array

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
Advertisement