294

所有浏览器都支持 iframe height=100% 吗?

我使用 doctype 作为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

在我的 iframe 代码中,如果我说:

<iframe src="xyz.pdf" width="100%" height="100%" />

我的意思是它实际上会占用剩余页面的高度(因为顶部还有另一个固定高度为 50px 的框架)所有主要浏览器(IE/Firefox/Safari)都支持吗?

同样关于滚动条,即使我说scrolling="no",我可以在 Firefox 中看到禁用的滚动条...如何完全隐藏滚动条并自动设置 iframe 高度?

4

18 回答 18

324

您可以使用框架集作为先前的答案状态,但如果您坚持使用 iFrame,以下 2 个示例应该可以工作:

<body style="margin:0px;padding:0px;overflow:hidden">
    <iframe src="http://www.youraddress.com" frameborder="0" style="overflow:hidden;height:100%;width:100%" height="100%" width="100%"></iframe>
</body>

替代:

<body style="margin:0px;padding:0px;overflow:hidden">
    <iframe src="http://www.youraddress.com" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe>
</body>

如上所示,使用 2 个选项隐藏滚动:

<body style="margin:0px;padding:0px;overflow:hidden">
    <iframe src="http://www.youraddress.com" frameborder="0" style="overflow:hidden;height:150%;width:150%" height="150%" width="150%"></iframe>
</body>

用第二个例子破解:

<body style="margin:0px;padding:0px;overflow:hidden">
    <iframe src="http://www.youraddress.com" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:150%;width:150%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="150%" width="150%"></iframe>
</body>

为了隐藏 iFrame 的滚动条,让父级overflow: hidden隐藏滚动条,并使 iFrame 的宽度和高度达到 150%,这会迫使滚动条在页面外,并且由于正文没有滚动条人们可能不会期望 iframe 超出页面的边界。这会以全宽隐藏 iFrame 的滚动条!

于 2011-05-03T10:41:26.930 回答
199

创建全屏的 3 种方法iframe


  • 方法 1 -视口百分比长度

    支持的浏览器中,您可以使用视口百分比长度,例如height: 100vh.

    where100vh代表视口的高度,同样100vw代表宽度。

    示例在这里

    body {
        margin: 0;            /* Reset default margin */
    }
    iframe {
        display: block;       /* iframes are inline by default */
        background: #000;
        border: none;         /* Reset default border */
        height: 100vh;        /* Viewport-relative units */
        width: 100vw;
    }
    <iframe></iframe>


  • 方法 2 - 固定定位

    这种方法相当简单。只需设置fixed元素的位置并添加height/ widthof 100%

    示例在这里

    iframe {
        position: fixed;
        background: #000;
        border: none;
        top: 0; right: 0;
        bottom: 0; left: 0;
        width: 100%;
        height: 100%;
    }
    <iframe></iframe>


  • 方法 3

    对于最后一种方法,只需将height//body元素的设置为htmliframe100%

    示例在这里

    html, body {
        height: 100%;
        margin: 0;         /* Reset default margin on the body element */
    }
    iframe {
        display: block;       /* iframes are inline by default */
        background: #000;
        border: none;         /* Reset default border */
        width: 100%;
        height: 100%;
    }
    <iframe></iframe>

于 2015-01-09T04:28:57.967 回答
48

我遇到了同样的问题,我正在将 iframe 拉入 div。试试这个:

<iframe src="http://stackoverflow.com/" frameborder="0" scrolling="yes" seamless="seamless" style="display:block; width:100%; height:100vh;"></iframe>

高度设置为 100vh,代表视口高度。此外,宽度可以设置为 100vw,尽管如果源文件是响应式的(您的框架将变得非常宽),您可能会遇到问题。

于 2015-07-30T14:37:33.863 回答
47

1. 将您的 DOCTYPE 更改为不那么严格的东西。不要使用 XHTML;这很愚蠢。只需使用 HTML 5 doctype 就可以了:

<!doctype html>

2. 您可能需要确保(取决于浏览器)iframe 的父级具有高度。和它的父母。和它的父母。ETC:

html, body { height: 100%; }
于 2011-05-03T15:26:32.273 回答
33

这对我来说效果很好(仅当 iframe 内容来自同一个域时):

<script type="text/javascript">
function calcHeight(iframeElement){
    var the_height=  iframeElement.contentWindow.document.body.scrollHeight;
    iframeElement.height=  the_height;
}
</script>
<iframe src="./page.html" onLoad="calcHeight(this);" frameborder="0" scrolling="no" id="the_iframe" width="100%" ></iframe>
于 2012-09-07T04:22:41.690 回答
9

body {
    margin: 0;            /* Reset default margin */
}
iframe {
    display: block;       /* iframes are inline by default */
    background: #000;
    border: none;         /* Reset default border */
    height: 100vh;        /* Viewport-relative units */
    width: 100vw;
}
<iframe></iframe>

于 2016-12-15T10:21:29.183 回答
8
<iframe src="http://youraddress.com" style="width: 100%; height: 100vh;">

</iframe>
于 2020-04-17T18:47:33.930 回答
7

<iframe src="" style="top:0;left: 0;width:100%;height: 100%; position: absolute; border: none"></iframe>

于 2017-02-27T20:22:09.960 回答
5

以下测试工作

<iframe style="width:100%; height:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" src="index.html" frameborder="0" height="100%" width="100%"></iframe>
于 2018-08-10T02:43:56.167 回答
2

除了 Akshit Soota 的回答:显式设置每个父元素的高度很重要,如果有的话,也包括表和的高度:

<body style="margin: 0px; padding:0px; height: 100%; overflow:hidden; ">
<form id="form1" runat="server" style=" height: 100%">
<div style=" height: 100%">


    <table style="width: 100%; height: 100%" cellspacing="0"  cellpadding="0">
        <tr>
            <td valign="top" align="left" height="100%">
                <iframe style="overflow:hidden;height:100%;width:100%;margin: 0px; padding:0px;" 
                        width="100%" height="100%" frameborder="0" scrolling="no"
                        src="http://www.youraddress.com" ></iframe> 
            </td>
于 2013-02-12T07:59:02.637 回答
2

你先添加css

html,body{
height:100%;
}

这将是html:

 <div style="position:relative;height:100%;max-width:500px;margin:auto">
    <iframe src="xyz.pdf" frameborder="0" width="100%" height="100%">
    <p>Your browser does not support iframes.</p>
    </iframe>
    </div>
于 2016-04-25T18:00:05.553 回答
2

http://embedresponsively.com/

这是一个很好的资源,效果很好,我用过几次。创建以下代码....

<style>
.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } 
.embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
</style>
<div class='embed-container'>
<iframe src='http://player.vimeo.com/video/66140585' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
于 2016-12-06T15:45:22.163 回答
1

这是一个简洁的代码。它确实依赖于 jquery 方法来查找当前窗口高度。在加载 iFrame 时,它​​将 iframe 的高度设置为与当前窗口相同。然后为了处理页面的大小调整,body 标签有一个 onresize 事件处理程序,它在文档被调整大小时设置 iframe 的高度。

<html>
<head>
    <title>my I frame is as tall as your page</title>
     <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body onresize="$('#iframe1').attr('height', $(window).height());" style="margin:0;" >
    <iframe id="iframe1" src="yourpage.html" style="width:100%;"  onload="this.height=$(window).height();"></iframe>
</body>
</html>

这是一个工作示例:http: //jsbin.com/soqeq/1/

于 2014-10-30T01:29:45.743 回答
0

另一种构建流畅全屏的方法iframe


viewport页面加载时嵌入的视频会填满整个区域

登陆带有视频或任何嵌入内容的页面的好方法。您可以在嵌入视频下方添加任何其他内容,这些内容在向下滚动页面时会显示出来。

例子:

CSS 和 HTML 代码。

body {
    margin: 0;
    background-color: #E1E1E1;
}
header {
    width: 100%;
    height: 56vw;
    max-height: 100vh;
}
.embwrap {
    width: 100%;
    height: 100%;
    display: table;
}
.embwrap .embcell {
    width: auto;
    background-color: black;
    display: table-cell;
    vertical-align: top;
}
.embwrap .embcell .ifrwrap {
    height: 100%;
    width: 100%;
    display: inline-table;
    background-color: black;
}

.embwrap .embcell .ifrwrap iframe {
    height: 100%;
    width: 100%;
}
<header>
  <div class="embwrap">
    <div class="embcell">
      <div class="ifrwrap">
        <iframe webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen="true" allowtransparency="true" scrolling="no" frameborder="0" width="1920" height="1440" src="http://www.youtube.com/embed/5SIgYp3XTMk?autoplay=0&amp;modestbranding=1&amp;iv_load_policy=3&amp;showsearch=0&amp;rel=1&amp;controls=1&amp;showinfo=1&amp;fs=1"></iframe>
      </div>
    </div>
  </div>
</header>
<article>
  <div style="margin:30px; text-align: justify;">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lorem orci, rhoncus ut tellus non, pharetra consequat risus. </p>
    <p>Mauris aliquet egestas odio, sit amet sagittis tellus scelerisque in. Fusce in mauris vel turpis ornare placerat non vel purus. Sed in lobortis </p>
  </div>
</article>

于 2015-10-18T09:42:02.763 回答
0

只有这对我有用(但对于“同域”):

function MakeIframeFullHeight(iframeElement){
    iframeElement.style.width   = "100%";
    var ifrD = iframeElement.contentDocument || iframeElement.contentWindow.document;
    var mHeight = parseInt( window.getComputedStyle( ifrD.documentElement).height );  // Math.max( ifrD.body.scrollHeight, .. offsetHeight, ....clientHeight,
    var margins = ifrD.body.style.margin + ifrD.body.style.padding + ifrD.documentElement.style.margin + ifrD.documentElement.style.padding;
    if(margins=="") { margins=0;  ifrD.body.style.margin="0px"; }
    (function(){
       var interval = setInterval(function(){
        if(ifrD.readyState  == 'complete' ){
            iframeElement.style.height  = (parseInt(window.getComputedStyle( ifrD.documentElement).height) + margins+1) +"px";
            setTimeout( function(){ clearInterval(interval); }, 1000 );
        } 
       },1000)
    })();
}

你可以使用:

MakeIframeFullHeight(document.getElementById("iframe_id"));

或者

<iframe .... onload="MakeIframeFullHeight(this);" ....
于 2017-12-02T16:31:20.107 回答
0

根据https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe,不再允许使用百分比值。但以下对我有用

<iframe width="100%" height="this.height=window.innerHeight;" style="border:none" src=""></iframe>

虽然width:100%有效,height:100%但无效。所以window.innerHeight一直使用。您还可以使用 css 像素作为高度。

工作代码:链接工作站点: 链接

于 2018-06-13T10:35:52.787 回答
0

您可以调用一个函数,该函数将在加载 iframe 时计算 iframe 的主体高度:

<script type="text/javascript">
    function iframeloaded(){
       var lastHeight = 0, curHeight = 0, $frame = $('iframe:eq(0)');
       curHeight = $frame.contents().find('body').height();
       if ( curHeight != lastHeight ) {
           $frame.css('height', (lastHeight = curHeight) + 'px' );
       }
    }
</script>

<iframe onload="iframeloaded()" src=...>
于 2020-05-05T12:56:30.907 回答
0

要在 iframe 内获得没有滚动条的全屏 iframe,请使用以下 css。无需更多

iframe{
            height: 100vh;
            width: 100vw
        }
    
    iframe::-webkit-scrollbar {
        display: none;
    }
于 2021-01-08T10:32:39.487 回答