2013-03-06 64 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