有没有办法让 div HTML 元素半透明?
使用 CSS,这就是跨浏览器解决方案
div {
opacity: 0.5;
filter: alpha(opacity = 0.5);
filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0.5);
}
注意:现在在每个浏览器中,您只需要
opacity
查看CanIUse。即使在 IE9 中也能工作。
这适用于所有浏览器
div {
-khtml-opacity:.50;
-moz-opacity:.50;
-ms-filter:”alpha(opacity=50)”;
filter:alpha(opacity=50);
filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0.5);
opacity:.50;
}
如果您不希望透明度影响整个容器及其子容器,请检查此解决方法http://www.impressivewebs.com/css-opacity-that-doesnt-affect-child-elements/
使用半透明的背景PNG文件,并希望不必支持IE6?
如果您只希望 div 的 background 半透明,而不是其中的任何文本和元素,您可以简单地将背景颜色设置为透明颜色(即使用 alpha < 1).
一个示例位于我们的网站,这里是一个最小化的示例:
<html>
<head>
<title>Transparency test</title>
<style type="text/css">
body {
background-image: url(http://www.fencing-game.de/style/background6.png);
}
#trans {
background-color: rgba(255,0,0,0.5);
max-width: 80ex;
}
p {
max-width: 70ex;
margin: auto;
}
#nontrans {
background-color: rgb(255,255, 0);
}
</style>
</head>
<body>
<div id="trans">
<p>normal text</p>
<p id="nontrans">nontransparent</p>
<p>normal text</p>
</div>
</body>