Skip to content

[css-sizing] Auto-resize iframes based on content #1771

Open
2 of 3 issues completed
Open

Description

@tabatkins

In https://lists.w3.org/Archives/Public/www-style/2017Aug/0045.html, Craig Francis said:

Just re-raising the suggestion of allowing an iframe or textarea to increase its height based on its content.

Mats Palmgren suggested that I raise this issue again, as Firefox is unlikely to implement it unless the CSSWG agrees to spec it.

For iframes, this feature would allow easy and secure (isolated) embedding of third party content without issues relating to scroll bars (i.e. needing custom JavaScript to ask the parent page, via postMessage, to change the iframe height).

For textrareas, this is just a convenience feature, as trying to get JavaScript to do this is tricky (e.g. the browser can automatically add/remove scroll bars, which can throw off the calculations).

When we discussed this last time (after the @seamless attribute on the <iframe> was removed from the WHATWG spec), it was suggested that we used:

iframe,
textrarea {
    height: max-content;
}

In the case of the iframe, the child page would need to send a HTTP header (e.g. Expose-Height-Cross-Origin: 1), so it does not leak information about that website (e.g. if the user is logged in).

PS: This has been discussed in a number of times before, the earliest one I've found is 16 years old:

https://bugzilla.mozilla.org/show_bug.cgi?id=80713

And I've written up some notes about this feature, which includes the suggestion of using it for element height animation (e.g. disclosure widgets):

https://github.com/craigfrancis/iframe-height

Activity

  1. tomhodgins commented on Aug 31, 2017

    @tomhodgins

    I don't understand why “expand to fit scrollHeight” hasn't been the default behaviour for <textarea> all along. In a lot of my demos where I want to add this sort of functionality I achieve this effect by somehow running the following 2 lines of JavaScript from the context of an element:

    element.style.height = 'inherit'
    element.style.height = element.scrollHeight + 'px'

    I've written a little JavaScript function that can help me apply an auto-height or auto-width to elements that match CSS selectors here that uses this same logic: https://github.com/tomhodgins/reprocss/blob/master/mixins/auto-expand.js

    Mixin Demo: https://tomhodgins.github.io/reprocss/test/auto-expand-mixin.html

    Another Auto-Height demo: https://codepen.io/tomhodgins/pen/KgazaE

    Another Auto-Width demo: https://codepen.io/tomhodgins/pen/ZpLxjy

    As for auto-height <iframe>, I believe I've seen this happen on iOS before, but I'm not sure I could dig up the CSS. I'll see if I can recreate it :D

  2. craigfrancis commented on Aug 31, 2017

    @craigfrancis

    With iOS, this is their default behaviour for iframes... they call it frame flattening.

  3. eeeps commented on Aug 31, 2017

    @eeeps
    Contributor

    What happens when

    <style>
      p { font-size: 100px; }
      @media (min-height: 50px) { p { font-size: 1px; } }
    </style>
    <p>hi</p>

    gets iframed into

    <style>iframe { height: max-content; }</style>
    <iframe src="is-this-a-circular-dependency.html"></iframe>

    ?

    UPDATE: I see this has already been asked and that there are already some proposed answers.

  4. craigfrancis commented on Aug 31, 2017

    @craigfrancis

    As a website developer, I'd be happy for the iframe to simply increase in size (so the scroll bars weren't required), and to not bother scaling back again.

    Which works quite well with the name max-content.

  5. liamquin commented on Aug 31, 2017

    @liamquin

    This could be a win for accessibility (and monetization/conversion) for inserted adverts, where e.g. the user has specified a larger minimum text size than the default, but at the expense of page reflows as each ad slot gets populated.

  6. css-meeting-bot commented on Nov 6, 2017

    @css-meeting-bot
    Member

    The Working Group just discussed Auto resizing of iframes and textarea based on content size, and agreed to the following resolutions:

    • RESOLVED: Add textarea sizing to Sizing L4
    The full IRC log of that discussion <gregwhitworth> Topic: Auto resizing of iframes and textarea based on content size
    <astearns> github: https://github.com//issues/1771
    <gregwhitworth> TabAtkins: there have been many requests for textarea and iframes resize on content
    <gregwhitworth> TabAtkins: we talked it over and thought, yeah probably ok
    <gregwhitworth> TabAtkins: we started experimenting with this
    <gregwhitworth> TabAtkins: figure our some mechanism content based sizing for textarea and iframes
    <gregwhitworth> TabAtkins: we learned some issues impl seamless with iframes due to COR leaking information
    <gregwhitworth> TabAtkins: we suspect we'd walk up the frame tree until we hit a fixed size to resolve the mqs
    <gregwhitworth> TabAtkins: Someone from Moz impl this
    <gregwhitworth> dbaron: we got the spec to say that's how media queries work, but seamless was removed
    <gregwhitworth> dbaron: we have code for it - but it's not necessarily something you can write up in a spec
    <gregwhitworth> dbaron: you need to figure out how to spec it
    <gregwhitworth> iank_: what type of interesting things
    <gregwhitworth> dbaron: I'd need to look, like it tries to do layout
    <fantasai> s/do layout/do layout, and then tries again/
    <gregwhitworth> rossen: we used to have a technology that would allow you to do layout based on then content size and we killed it
    <smfr> q+
    <gregwhitworth> Rossen: performance becomes a concern for those
    <tantek> The iframe use-case for me is known width (set by container), auto (preferably auto-expanding) height
    <gregwhitworth> Rossen: when you consider extensions they try to size the box, and it's shrink to fit it becomes very bad for perf
    <gregwhitworth> Rossen: Our experimentation for this suggests it was bad, but maybe you'll find something
    <gregwhitworth> TabAtkins: they're both pretty separate features anyways
    <Rossen> q?
    <gregwhitworth> TabAtkins: are we ok experimenting in this space
    <gregwhitworth> TabAtkins: the textarea would go into sizing 4
    <gregwhitworth> smfr: we've had the auto sizing of the iframe even COR
    <gregwhitworth> smfr: it makes your frame layout outside in rather than inside out
    <gregwhitworth> smfr: we've had quite a few media query bugs
    <gregwhitworth> smfr: we ran into media query cycles
    <gregwhitworth> TabAtkins: that means that you weren't doing media queries the way we defined
    <smfr> q-
    <gregwhitworth> smfr: it does bring about nasty things in the code
    <gregwhitworth> TabAtkins: how is this different from regular layout
    <gregwhitworth> smfr: you can avoid laying out the iframe, if you have to you have to dirty the other nodes
    <gregwhitworth> TabAtkins: ok, let's talk about this later
    <gregwhitworth> dbaron: I think the media query problems you had were due to doing what authors weren't expecting
    <gregwhitworth> TabAtkins: this would be opt in
    <gregwhitworth> tantek: how do you trigger behavior in iOS
    <gregwhitworth> smfr: you always get it
    <gregwhitworth> smfr: users don't experience nested scrollers, we always wanted page scrolling to win so you don't get trapped
    <gregwhitworth> tantek: width is expected and height is auto
    <gregwhitworth> smfr: yes
    <gregwhitworth> Rossen: ok, so - it seems that Google wants to experiment with this - Apple has it and wants to remove it
    <gregwhitworth> Rossen: do you want a resolution
    <gregwhitworth> TabAtkins: The textarea one is simple enough to go into sizing 4
    <gregwhitworth> TabAtkins: the iframe one can go in WICG
    <gregwhitworth> Rossen: any objections to adding textareas to CSS Sizing L3
    <gregwhitworth> fantasai: yes
    <gregwhitworth> TabAtkins: I said 4
    <gregwhitworth> Rossen: Ok, L4
    <gregwhitworth> fantasai: ok - I'm ok with that
    <gregwhitworth> Rossen: Changes to sizing L4
    <fantasai> fantasai: we can add a note for L3
    <gregwhitworth> Resolved: Add textarea sizing to Sizing L4
    <fantasai> RESOLVED: Add textarea sizing to Sizing L4
    <gregwhitworth> *discuss whether to do in WICG*
    <gregwhitworth> TabAtkins to spin up a WICG regarding auto sizing of iframes
    <tantek> I'm a bit surprised that we're not even putting into a WD something that's been shipping in iOS for 10 years
  7. tantek commented on Nov 7, 2017

    @tantek
    Contributor

    The auto-resizing (specifically height) of iframes is a highly desirable feature for authors.

    I have been using this feature for quite some time on my event posts, e.g.:

    http://tantek.com/2017/305/e1/homebrew-website-club

    I use an iframe to embed an unknown number of RSVPs generated by an external service, and it is quite handy (e.g. on iOS Safari) for that iframe of RSVPs to auto-grow (in height) as more RSVPs come in.

    I'd very much like to see iframe auto-sizing added to Sizing L4 as well.

  8. 113 remaining items

  9. annevk commented on Apr 8, 2026

    @annevk
    Member

    I don't think WebExtensions are a valid use case here, but they also could set headers I think if provided with the right API.

    And the first such <meta> is an extremely fragile setup that's definitely considered a mutable policy as you start out without a policy and if you find such a <meta> element you have one. But there's absolutely no guarantee when you might find that <meta> element. This has been hugely problematic for <meta charset> for instance and that's still not interoperable today. Adding to that problem does not seem good.

  10. tabatkins commented on Apr 8, 2026

    @tabatkins
    MemberAuthor

    Charset is problematic because it's a bizarre misfeature in the first place. Having a document declare its own encoding in that encoding is already weird and fragile, but if you want to do it, you need that info as fast as possible so you're not forced to hang onto the entire bytestream just in case you end up needing to reinterpret it.

    This one, on the other hand, only has an effect on layout. We simply don't need to know it before head is parsed in ~100% of cases (nobody, in practice, ever causes head to be displayed).

    Can you elaborate on any actual issues with doing it this way, for such a late-mattering decision that doesn't affect how the document itself is handled?

  11. WebReflection commented on Jun 19, 2026

    @WebReflection

    Hello folks, I am currently creating 2 ResizeObserver:

    • one on the main page that postMessage to the iframe the container width
    • one on the iframe that postMessage on the main thread the current iframe height

    The former works mostly for when you open devtools, resize the window, go fullscreen and so on (the different size is tracked and never triggered twice) while the latest is needed because the iframe contains runtime code that can produce runtime output.

    The width is enforced because the iframe itself has an overflow: hidden, the height though should flow as if the output was appended directly on the main thread.

    The reason generated HTML cannot land in there (the main thread) is ... well ... security, untrusted HTML/Image and whatnot.

    Is this dance really needed or is there any alternative? I couldn't find much around frame-size even if merged and I am not sure where to look at to remove the double ResizeObserver dance, thank you.

  12. WebReflection commented on Jun 19, 2026

    @WebReflection

    follow up: somebody on Xwitter mentioned this https://chromestatus.com/feature/5108373464547328 ... is that the thing that is going to close automatically this issue too? Thanks in advance for expanding around current state and that proposal!

  13. chrishtr commented on Jun 24, 2026

    @chrishtr
    Contributor

    is that the thing that is going to close automatically this issue too?

    Yes, this is the feature that is now in a CSS spec and hopefully will soon be shipped in Chromium and other browsers. Thanks for the positive feedback!

  14. annevk commented on Jul 3, 2026

    @annevk
    Member

    As far as I can tell whatwg/html#12444 still has outstanding concerns. Seems a bit premature to ship based only on the CSS side of things.

  15. annevk commented on Jul 3, 2026

    @annevk
    Member

    Also, does that mean the "Needs Edits" and "Needs Testcase (WPT)" labels here are incorrect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions