Menu

How to Auto-Resize Cross-Origin iframes Without Breaking Security
📰

How to Auto-Resize Cross-Origin iframes Without Breaking Security

DEV Community: frontend·Edmond Rätzel·18 days ago
#6xzosTdT
#devtofrontend#devto#dev#client#embed
Reading 0:00
15s threshold

Embedding a dynamic application inside an iframe sounds straightforward until the content starts changing height.

A photo grid loads more images. A customer opens a comment panel. A video player expands. Suddenly, the embedded content is cut off, surrounded by unnecessary whitespace, or trapped inside a second scrollbar.

Setting height: 100% usually doesn't solve the problem.

The real challenge is that the parent website and the embedded application may run on different origins. Browser security prevents the parent from directly inspecting the iframe's document dimensions.

Fortunately, there is a reliable way to handle this without weakening the browser's same-origin protections.

Why Fixed iframe Heights Fail

Consider an online gallery for photographers embedded inside a photography website.

The gallery might contain 20 images initially, but its height can change when:

More images finish loading

Responsive grids reorganize

Clients expand comments

Different fonts finish rendering

Interactive…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.