IE浏览器中iframe会出现默认的白色背景及边框,影响美观。

有边框的正常,但IE默认的HTML背景色为白色,而FF默认背景色为透明色。

此外iframe在IE中长宽为300*150,其中边框为2px宽;FF 中为304*154,边框也同样为2px。(这里所说宽算边框的宽度)

现有两个文件:main.html 和transparentBody.html

其中transparentBody.htm是嵌套在main.htm之中,为了便于看效果在BODY中加入蓝色背景请看效果图:

main.html代码:

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>main</title>
</head>
<body style="background-color:blue;">
	<iframe name="main" src="transparentBody.htm"></iframe>
</body>
</html>

ransparentBody.html代码

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>transparentBody</title>
</head>
<body>
...
</body>
</html>

首先,在transparentBody.htm的<body>中设定style="background-color:transparent",即为透明样式

然后,在<iframe name="main" src="transparentBody.htm"></iframe>之中加入

frameborder="0"和allowTransparency="true"

<iframe name="main" src="transparentBody.htm" frameborder="0" allowTransparency="true"></iframe>

到此,iframe背景透明效果搞定。

代码中
allowTransparency指是否允许透明,
frameborder="0"是指边框为不可见。