Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IFrame height issues on iOS (mobile safari)

Example page source:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Page</title>
</head>
<body>
<div class="main" style="height: 100%;">
    <div class="header" style="height: 100px; background-color: green;"></div>

    <iframe src="http://www.wikipedia.com"
            style="height: 200px; width: 100%; border: none;"></iframe>

    <div class="footer" style="height: 100px; background-color: green;"></div>
</div>
</body>
</html>

The problem is, that height of 200px from the IFrames inline style is ignored on mobile safari:

enter image description here

Also I'd like to change the height of the IFrame dynamically via vanilla JavaScript which is not working at all with the following code:

document.getElementsByTagName('iframe')[0].style.height = "100px"

The value of the height style is changed correctly according to the dev tools but it's simply ignored since the actually rendered height of the IFrame doesn't change.

This only seems to be a problem in mobile Safari and is working as expected on the latest versions of desktop Safari, Firefox, Chrome, Androids WebView etc.

Testpage: http://devpublic.blob.core.windows.net/scriptstest/index.html

Ps.: I tested this with various devices on browserstack and also took the screenshots there since I don't have no actual iDevice at hand.

like image 794
suamikim Avatar asked Dec 16 '15 19:12

suamikim


People also ask

Do iframes work on Safari?

If the iframe content is fully responsive and can resize itself without internal scroll bars, then iOS Safari will resize the iframe without any real issues. Then this works without issues in iOS 7.1 Safari. You can change between landscape and portrait without any issues.

Why iframe is not working in Safari?

Why Safari doesn't allow to store cookie for iFrame? Answer: A: Answer: A: Try going to Safari/Preferences/Privacy and uncheck Prevent cross-site tracking.

How do I enable iframe in Safari?

Step 1: Go to Settings icon. Step 2: Select “Safari” Page 4 Confidential 11/16/2017 iPhone and Macbooks Safari SETTINGS to LOAD IFRAMES IN MEMBERS PORTAL 3 Step 3: Ensure that the “Block Pop-ups”, “Prevent Cross-Site Tracking” and “Block all Cookies” are not selected (not green).

How do I adjust the iframe height automatically?

Answer: Use the contentWindow Property You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.


3 Answers

It looks like this: How to get an IFrame to be responsive in iOS Safari?

iFrames have an issue on iOS only, so you have to adapt your iframe to it.

You can put a div wrapping the iframe, set css on the iframe and, what worked for me, was to add: put the attribute scrolling='no'.

Wishing you luck.

like image 179
Gabriel Augusto Avatar answered Oct 13 '22 18:10

Gabriel Augusto


I got the same issue. And after tried all solutions I could find, I finally found how to solve it.

This issue is caused by the iOS Safari, it will auto-expend the hight of iframe to fit the page content inside.

If you put the scrolling='no' attribute to the iframe as <iframe scrolling='no' src='content.html'>, this issue could be solved but the iframe could not show the full content of the page, the content which exceeds the frame will be cut.

So we need to put a div wrapping the iframe, and handle the scroll event in it.

<style>
.demo-iframe-holder {
  width: 500px;
  height: 500px;
  -webkit-overflow-scrolling: touch;
  overflow-y: scroll;
}

.demo-iframe-holder iframe {
  height: 100%;
  width: 100%;
}
</style>

<html>
<body>
    <div class="demo-iframe-holder">
        <iframe src="content.html" />
    </div>
</body>
</html>

references:

https://davidwalsh.name/scroll-iframes-ios

How to get an IFrame to be responsive in iOS Safari?

Hope it helps.

like image 32
Freya Yu Avatar answered Oct 13 '22 19:10

Freya Yu


PROBLEM:

I was having the same issue. Sizing/styling the iframe's container div and adding scrolling="no" to the iframe didn't work for me. Having a scrolling overflow like Freya describes wasn't an option, either, because the contents of my iframe needed to size depending on the parent container. Here's how my original (not working, overflowing its container) iframe code was structured:

<style>
    .iframe-wrapper {
        position: relative;
        height: 500px;
        width:   100%;
    }

    .iframe {
        display: block;
        position: absolute;
        top:    0;
        bottom: 0;
        left:   0;
        right:  0;
        width:  100%;
        height: 100%;
    }
</style>

<div class="iframe-wrapper">
    <iframe frameborder="0" scrolling="no" class="iframe" src="content.html"></iframe>
</div>

SOLUTION:

This super simple little CSS hack did the trick:

<style>
    .iframe-wrapper {
        position: relative;
        height: 500px;
        width:   100%;
    }

    .iframe {
        display: block;
        position: absolute;
        top:    0;
        bottom: 0;
        left:   0;
        right:  0;
        width:  100px;
        min-width:  100%;
        height: 100px;
        min-height: 100%;
    }
</style>

<div class="iframe-wrapper">
    <iframe frameborder="0" scrolling="no" class="iframe" src="content.html"></iframe>
</div>

Set the iframe's height/width to some small, random pixel value. Set it's min-height & min-width to what you actually want the height/width to be. This completely fixed the issue for me.

like image 3
AnnaFractuous Avatar answered Oct 13 '22 19:10

AnnaFractuous