/

HTML / CSS / JavaScript Tutorial

CSS Property: opacity

[this page | pdf | back links]

The CSS (CSS3) opacity property sets the degree of opacity (transparency) of an element. 0 is completely transparent, 1 is completely non-transparent. Note also sets the transparency of all relevant child elements (if you don’t want this to happen then use RGBA colouring).

 

Valid property values (other than inherit and initial) are:

 

Value

Description

number

From 0.0 to 1.0

 

Default Value:

1

JavaScript syntax:

e.g. object.style.opacity="0.6"

Inherited:

No

Animatable:

Yes

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
td {width: 300px; height: 40px; background-color: blue; text-align: center;}
p {width: 250px; height: 35px; background-color: yellow;}
p.x1 {opacity: 0.5;}
</style>
</head>
<body>
<table>
<tr><td><p>Element with default property</p></td></tr>
<tr><td><p class="x1">Property set using in-file HTML style</p></td></tr>
<tr><td><p id="x2">Property set using JavaScript</p></td></tr>
</table>

<script>
document.getElementById("x2").style.opacity = "0.5";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyOpacity() {
  var x = document.createElement("DIV"); x.style.opacity = "0.5"; return (window.getComputedStyle(x, null).opacity == "0.5");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile