GithubHelp home page GithubHelp logo

xieerduos / iframe-height Goto Github PK

View Code? Open in Web Editor NEW

This project forked from craigfrancis/iframe-height

0.0 1.0 0.0 160 KB

Proposal for automatically setting an iframe height via HTML/CSS

JavaScript 39.03% HTML 60.19% CSS 0.78%

iframe-height's Introduction

iFrame Height

After the recent removal of the @seamless attribute on the <iframe> from the WHATWG spec (issue 331); we still need to consider the problem of setting the height of iframes, so they contain their content without scroll bars.

This proposal is to use 1 line of CSS:

<iframe src="./framed.html" id="iframe"></iframe>
<style>
	#iframe { height: max-content; }
</style>

And 1 header, to be set on the framed content (framed.html):

Expose-Height-Cross-Origin: 1;

This header is for security reasons, otherwise it can leak state information (e.g. the height of the page may determine if a user is logged in).

This was the main feature that @seamless needed to provide, rather than the <iframe> content being "rendered in a manner that makes it appear to be part of the containing document" (spec).

Further discussion on this proposal is on:

And the browser feature requests:


Current solutions

1). Same Domain

It is possible to set the height with JavaScript (demo):

var iframe = document.getElementById('iframe'),
	height = iframe.contentWindow.document.body.scrollHeight;

iframe.style.height = height + 'px';

But this needs to be done whenever the content changes, such as navigating to a new page, or when new content is exposed (e.g. JS disclosure widget).

This can be solved with a setTimeout(), which is not ideal.

In the future ResizeObserver might help, but it still requires quite a bit of JavaScript, and does not work Cross-Domain.

2). Cross Domain

Due to the security restrictions in place, this requires the document in the <iframe> to use postMessage() every time the content changes.

This is currently custom code on every website, as no-one can agree on what format the postMessage() should use.

An example can be seen in these child and parent JavaScript files.


Potential problems

  1. Infinite loops with media queries, raised by Jake Archibald (more details).
  2. ???

Requests from other developers


Additional uses

Textarea

This could be set on a <textarea>, so its height automatically increases (demo / source):

textarea {
    height: max-content;
}

Examples:

Feature requests:

Height animation

An alternative to "max-height: 10000px" when animating the opening/closing of a disclosure widget (demo / source):

#widget {
    overflow-y: hidden;
    height: max-content;
    transition-property: all;
    transition-duration: .5s;
}

#widget.closed {
    height: 0;
}

<p><a href="#widget">Show</a></p>
<div id="widget">
    <p>Hidden text</p>
</div>

Examples:

Feature requests:

iframe-height's People

Contributors

craigfrancis avatar rahul-ratusaria avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.