CSS3 opacity 属性
设置一个div元素的透明度级别:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: red;
opacity: 0.5;
filter: Alpha(opacity=50);
/* IE8 and earlier */
}
</style>
</head>
<body>
<div>This element's opacity is 0.5! Note that both the text and the background-color are affected by the opacity level!</div>
</body>
</html>
浏览器支持
所有主流浏览器都支持opacity属性。.
注意:IE8和早期版本支持另一种过滤器属性。像:filter:Alpha(opacity=50)
属性定义及使用说明
Opacity属性设置一个元素了透明度级别。
默认值: | 1 |
---|---|
继承: | no |
版本: | CSS3 |
JavaScript 语法: | object.style.opacity=0.5 |
语法
opacity: value|inherit;
值 | 描述 |
---|---|
value | 指定不透明度。从0.0(完全透明)到1.0(完全不透明) |
inherit | Opacity属性的值应该从父元素继承 |
更多实例
更改某个元素的不透明度 - 这个例子演示了如何使用JavaScript来改变元素的不透明度。
<!DOCTYPE html>
<html>
<head>
<script>
function ChangeOpacity(x) {
// Return the text of the selected option
var opacity = x.options[x.selectedIndex].text;
var el = document.getElementById("p1");
if (el.style.opacity !== undefined) {
el.style.opacity = opacity;
} else {
alert("Your browser doesn't support this example!");
}
}
</script>
</head>
<body>
<p id="p1">Select a value from the list below, to change this element's opacity!</p>
<select onchange="ChangeOpacity(this);" size="5">
<option />0
<option />0.2
<option />0.5
<option />0.8
<option selected="selected" />1
</select>
</body>
</html>