iframe-resizer

Keep same and cross domain iFrames sized to their content with support for window/content resizing, in page links, nesting and multiple iFrames

Github星跟踪图

iFrame Resizer V4

NPM version
NPM Downloads

Coverage Status
Donate

This library enables the automatic resizing of the height and width of both same and cross domain iFrames to fit their contained content. It provides a range of features to address the most common issues with using iFrames, these include:

  • Height and width resizing of the iFrame to content size.
  • Works with multiple and nested iFrames.
  • Domain authentication for cross domain iFrames.
  • Provides a range of page size calculation methods to support complex CSS layouts.
  • Detects changes to the DOM that can cause the page to resize using MutationObserver.
  • Detects events that can cause the page to resize (Window Resize, CSS Animation and Transition, Orientation Change and Mouse events).
  • Simplified messaging between iFrame and host page via postMessage.
  • Fixes in page links in iFrame and supports links between the iFrame and parent page.
  • Provides custom sizing and scrolling methods.
  • Exposes parent position and viewport size to the iFrame.
  • Works with ViewerJS to support PDF and ODF documents.
  • Supports IE 11 (V3 supports back to IE8)

Iframe-resizer is the result of many 100s of hours of work, if you would like to join others in showing support for the development of this project, then please feel free to buy me a coffee.

Getting Started

Install

This package can be installed via NPM (npm install iframe-resizer --save).

Usage

The package contains two minified JavaScript files in the js folder. The first (iframeResizer.min.js) is for the page hosting the iFrames. It can be called with via JavaScript:

const iframes = iFrameResize( [{options}], [css selector], [iframe] );

The second file (iframeResizer.contentWindow.min.js) needs placing in the page(s) contained within your iFrame. This file is designed to be a guest on someone else's system, so has no dependencies and won't do anything until it's activated by a message from the containing page.

Typical setup

The normal configuration is to have the iFrame resize when the browser window changes size or the content of the iFrame changes. To set this up you need to configure one of the dimensions of the iFrame to a percentage and tell the library to only update the other dimension. Normally you would set the width to 100% and have the height scale to fit the content.

<style>
  iframe {
    width: 1px;
    min-width: 100%;
  }
</style>
<iframe id="myIframe" src="http://anotherdomain.com/iframe.html"></iframe>
<script>
  iFrameResize({ log: true }, '#myIframe')
</script>

Note: Using min-width to set the width of the iFrame, works around an issue in iOS that can prevent the iFrame from sizing correctly.

If you have problems, check the troubleshooting section.

Example

To see this working take a look at this example and watch the console.

API Documentation

IFrame-Resizer provides an extensive range of options and APIs for both the parent page and the iframed page.

License

Copyright © 2013-20 David J. Bradshaw.
Licensed under the MIT License.

主要指标

概览
名称与所有者davidjbradshaw/iframe-resizer
主编程语言JavaScript
编程语言HTML (语言数: 6)
平台
许可证Other
所有者活动
创建于2013-06-14 19:18:32
推送于2025-07-22 09:31:36
最后一次提交2025-07-22 10:31:23
发布数167
最新版本名称v5.4.7 (发布于 )
第一版名称0.1.0 (发布于 )
用户参与
星数6.8k
关注者数140
派生数1k
提交数2.5k
已启用问题?
问题数845
打开的问题数4
拉请求数310
打开的拉请求数3
关闭的拉请求数316
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?