2013-03-06 125 views
11

我需要嵌入一个框架内的HTML页面,现在用的是下面的代码:设置iframe的高度自动

<iframe src="http://www.google.com" style="width: 90%; height: 300px" 
    scrolling="no" marginwidth="0" marginheight="0" frameborder="0" 
    vspace="0" hspace="0"> 

我试图使高度设置为auto,使画面自动调整大小以页面长度不必硬编码高度,因为我在这里做。我试过height:autoheight:inherit但它没有工作。

+1

你有没有尝试设置'高度:100%'如果iframe是在身体,应该使之补身体? – Xander 2013-03-06 19:15:54

+0

** Duplicates:** [This](http://stackoverflow.com/questions/9975810/how-can-i-make-iframe-automatically-adjust-height-according-to-the-contents-with)或[this](http://stackoverflow.com/questions/819416/adjust-width-height-of-iframe-to-fit-with-content-in-it)都应该回答你的问题。 – anroesti 2013-03-06 19:16:24

回答

13

试试这个编码

<div> 
    <iframe id='iframe2' src="Mypage.aspx" frameborder="0" style="overflow: hidden; height: 100%; 
     width: 100%; position: absolute;" height="100%" width="100%"></iframe> 
</div> 
+0

注:我使用的样式高度发现和宽度是出问题。这里的高度和宽度属性是重要的,它似乎一直工作。你有两个理由使用两个? – Andrew 2017-02-03 16:13:57

+0

iframe的高度/宽度无效HTML5根据规范(尽管如此,它仍然有效) – vaxquis 2018-01-07 14:44:17

3

如果这些网站位于不同的域中,由于跨浏览器域的限制,调用页面无法访问iframe的高度。如果您有权访问这两个网站,则可以使用[文档域黑客]。 1然后anroesti的链接应该有所帮助。

+0

GitHub上的这个库解决了跨域问题,并确保在事情发生变化时iFrame保持对内容的大小。 https://github.com/davidjbradshaw/iframe-resizer – 2014-04-10 14:40:52