2010-11-16 67 views
3

我想對外部網站添加一些廣告,這樣做,我使用iframe:帶有css固定位置的iframe:可能嗎?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<body style="background:#ddd"> 
<center>My ad</center> 
<iframe src="http://www.google.com" style="position:fixed; top:50px; left:0; width: 100%; bottom: 0; border: 0 "> 
<p>Your browser does not support iframes.</p> 
</iframe> 
</body> 
</html> 

的iframe是在正確的位置,但「底部:0」不工作:爲什麼呢? 我想iFrame遵循窗口大小調整:如何繼續?

回答

0

只要做到:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <body style="background:#ddd"> 
     <center>My ad</center> 
     <div style="position:fixed; top:50px; left:0; width: 100%; bottom: 0; border: 0 "> 
      <iframe src="http://www.google.com" style="width: 100%; height: 100%; border: 0"> 
       <p>Your browser does not support iframes.</p> 
      </iframe> 
     </div> 
    </body> 
</html> 
+0

這幾乎可以工作,但iframe內容的底部會低於容器的底部,因爲top:50px和height:100% – Richard 2012-02-25 00:11:14

2

嘗試添加樣式標籤和樣式的IFRAME。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<style type='text/css'> 
    body { background-color: #DDD; margin: none; } 
    iframe { position: fixed; top: #px; left: #px; } 
</style> 
<body> 
<center>My ad</center> 
<iframe src="http://www.google.com" border="0"> 
<p>Your browser does not support iframes.</p> 
</iframe> 
</body> 
</html> 
+0

的完美結合! – Macumbaomuerte 2013-11-27 17:01:27

12

我知道這有點晚,但我通過谷歌發現了這個問題,並且無疑其他人也會這樣。如果你想修復一個iframe的位置,就像修復一個div的位置一樣,你可以將它包裝在一個固定的位置div中,並使其大小爲100%。

此代碼在整個頁面上展開iframe,爲頂部的菜單留出空間。

CSS:

#iframe_main { 
    height: 100%; 
    width: 100%; 
} 

#idiv { 
    position: fixed; 
    top: 41px; 
    left: 0px; 
    right: 0px; 
    bottom: 0px; 
} 

HTML:

<div id='idiv'> 
    <iframe id="iframe_main"> 
    </iframe> 
</div> 
0

你爲什麼不刪除CSS屬性 「頂:50px的」?然後,「底部」將工作良好。