iframe size with CSS on iOS
Asked Answered
C

4

47

There's an iframe, which basically has more content than fits into the frame. The sizing of the frame is based on the browser screen size and lets the overflow scroll, which works perfectly on all browsers, except for iOS. On iOS, safari decides to resize the frame to fit the content. Not what you'd expect.

Example code on jsFiddle:
http://jsfiddle.net/R3PKB/2/

Try it out on your iOS devices:
http://jsfiddle.net/R3PKB/2/embedded/result

The HTML:

<div class="frame_holder">
  <iframe class="my_frame">
    // The content
  </iframe>
</div>

The CSS:

body {
  position: relative;
  background: #f0f0f0;
}

.frame_holder {
  position: absolute;
  top: 50px;
  bottom: 50px;
  left: 50px;
  right: 50px;
  background: #ffffff;
}

.my_frame {
  width: 100%;
  height: 100%;
  border: 1px solid #e0e0e0;
}
Claudelle answered 5/6, 2013 at 10:4 Comment(1)
Upvote from me for your clear analysis of this problem.Emilemile
M
52

You can make it work by adding a wrapping div with overflow: auto; and -webkit-overflow-scrolling:touch;. Here's your example with it: http://jsfiddle.net/R3PKB/7/

According to previous questions on SO it's a bug since iOS 4. I found more info here: https://mcmap.net/q/139038/-iframe-size-on-ipad iframe on iOS (iPad) content cropping issue

Marilyn answered 5/6, 2013 at 12:26 Comment(6)
Wow! That's definitely the closest solution I've seen to date.But the implementation is a bit more complicated and does not allow the iframe to be larger than the intended size.Claudelle
Can you explain more what you mean and how we can help you more? What is the intended size, screensize with 50px padding? (like what you're using in the example)Marilyn
Marked as answer because there's no other solution due to bug / security feature in iOS.Claudelle
Thanks, I need to set iFrame size to subframes for my html based iPhone app.Ostracoderm
This will break position:fixed elements inside the iframe.Balm
The answer below from @AndreiCojea actualy worked for me as I would expect this.Rowel
F
11

This is an old question, but since it comes first on google and the issue exists on nowadays ios devices, I repost a better fix that I found on this page: How to get an IFrame to be responsive in iOS Safari?

Basically, if you have an iframe with scroll (let's say a twitter widget), the solution above won't work very well because it makes the parent scrollable. The fix that worked for me is replacing height: 100% with height: 1px; min-height: 100%;.

Foetor answered 17/3, 2015 at 16:28 Comment(1)
This worked for me, the one above does not respect responsive layouts of the iframe content.Rowel
S
7

If iOS Safari is displaying your iframe content from a different origin than expected (i.e. it is shifted over by some pixels), try adding scrolling="no" as an attribute to the iframe. This should prevent it from automatically fitting its content.

More here.

Snailpaced answered 17/2, 2016 at 20:34 Comment(3)
After 2 hours of seeking for a good solution.. this one fixed my issue! Thank you!Schrick
This did not work for me, but I was loading also from the same origine.Rowel
I wish I could give multiple up votes to this answer. Bang on, solved my issue in iOS (Safari), iframe content was being cut off horizontally. Thanks a lot Kyle DumovicMarcin
B
1

using height: 1px; min-height: 100%; did not work for me, though I did not need a scrolling element. I had to use the overflow:auto; on a surrounding div instead. Note that this method is discouraged as it may have unintended consequences, but I tested on Android/iOS and desktop browsers and could not find any issues yet. fingers crossed.

This is a nice post from Andy Shora on some iOS iframe nuances: http://andyshora.com/iframes-responsive-web-apps-tips.html

Bowdlerize answered 9/7, 2015 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.