I'm trying to use CSS vh
units inside of an iframe. I'm finding that they are scaled to the size of the iframe somehow. In other words, 100vh isn't the windowheight. It's set to the height of the iframe.
Does this seem right?
Is there a workaround?
This is an excellent question. Sadly, I haven't been able to figure out a solution in CSS but I have been able to figure out a solution in JavaScript which I think is your best bet at the moment. Remember that the frames must be on the same domain for this to work.
Hope this helps. If this answer needs improvement, please comment below :-)
Solution in Theory (can't use here on SO because of frame origin issue):
Edit (Dec. 2018): In the comments, I was asked to supply an example. I can't do an exact example because the codepens on Stackoverflow load over a different frame origin than the page. However, I can mimic the effect. For practical applications, please reference the code snippet above. This snippet is meant merely to illustrate how it works.
Practical Application. Uses the concept explained above but without frame reference.
I know this is an old question, but as people move toward the
vh
unit, this question will become much more common.To clarify, here's an example of the problem. We have an HTML file that loads an
iframe
:And its
iframe
:The important thing to note here is that both the
iframe
and theiframe's
div
element are designated as having a height of50vh
. The intended behaviour may be that theiframe
honor the parent context's viewport height or width. Instead, the result looks like this:That is, the height of the blue element is ~25% of the browser window, instead of the expected 50% (100% of the
iframe
). Although we may wish theiframe
to respect the viewport of its parent, this example makes a good case for how unintuitive that may be, though it surely would make thev*
units more valuable for content beingiframe
'd in. The problem has to do with how viewport height is determined.From the spec:
Both an
iframe
and the browser window can be the initial containing block, as they are both valid viewports. A viewport is not limited to the browser window, but instead is defined as a window or other viewing area on the screen through which users consult a document.An
iframe
creates a nested browsing context when inserted into a document, and thus has its own viewport.So yes, this is the intended behaviour - and unfortunately there is no pure CSS workaround - however, www139 has provided an example of how this can be accomplished using JavaScript. The problem with this begins when many elements' size are controlled using
v*
units.