Opacity - Property CSS
Summary of characteristics of the opacity
property
1
Computed value
: during an animation, the opacity
property gradually changes from one value to another.Description of the opacity
property.
The opacity
property defines the opacity of an element, that is to say its degree of transparency. This property allows making an element
semi-transparent (or completely transparent). It applies to an element as a whole: its outline, background, and its descendants.
Note: To manage the opacity of a background alone, it is preferable to apply a semi-transparent color to this background: using the rgba()
function or another method, or an 8-digit color code. See Colors.
The property opacity
has the particularity that it applies to the entire set consisting of the element itself and its descendants.
And this in a global way, rather than element by element. In other words, if an opacity of 0.5 is requested, it will be applied to the entire constructed set,
and not to each of the elements. Therefore, this is not the usual inheritance mechanism; in fact, it is not possible to restore the initial opacity of the child elements.
This first image shows you the construction of our example: a first block called "Rear" is colored blue and has a background image.
It contains the block named "Médian" with a background image (the circles), which in turn contains the front block with a white background.
Here the property opacity
has not been used: all the blocks are therefore completely opaque.
‹div id="rear"›
‹div id="median"›
‹div id="front"›
‹/div›
‹/div›
‹/div›
The opacity of the middle block is now reduced to 0.5. The color and image of the back block are therefore slightly visible. It is also visible through the front block but without the background of the middle block being visible. So it is not the front block that has lost opacity, but rather the entire structure made up of the two blocks (middle block and its descendant, the front block).
In this third sample, the middle block still has an opacity of 0.5, but the front block has a requested opacity of 1. We can see that this does not change anything: the front block still lets the blue color and the image of the back block show through. Therefore, it is not the usual inheritance mechanism at work.
This overall opacity persists even in the parts that overflow from the parent, as is the case here.
Values for opacity
.
- opacity: 0.5;
A number without a unit, between 0 and 1. Negative values are treated as 0.
1
corresponds to full opacity (no transparency)
0
corresponds to total transparency (invisible element).
In this example, the opacity of the element will be 50% (semi-transparent).
The default value is 1 (total opacity). - opacity: 30%;
Opacity can also be indicated in percentages,
100%
corresponding to total opacity, and0%
to an invisible element as it is completely transparent.
Common values:
opacity: initial (1
)
opacity: inherit
opacity: revert
opacity: revertLayer
opacity: unset
These values ​​are described in more detail on their respective page: initial
, inherit
, revert
, revert-layer
, unset
.
Animation of the opacity
property.
In the example below, the logos of Edge and Internet Explorer are overlaid. By adjusting the transparency of the Edge logo (which is on top), we reveal more or less of the Internet Explorer logo.


Manipulating the opacity
property programmatically.
Change the opacity in Javascript.
Here is how to modify the value of opacity
for an element el
using Javascript. We see that Javascript offers a syntax with
the typical CSS notation, in kebab-case
(a hyphen to separate the words), and another syntax with the camel-case
notation
(an uppercase letter to separate the words).

let el = document.getElementById('id');
el.style['opacity'] = '0.5';
// or
let el = document.getElementById('id');
el.style.opacity = '0.5';
Read the opacity in Javascript.
The code given below explores the style
attribute of the element. It therefore only retrieves a value if opacity
has been
initialized in this attribute, and not via a CSS selector.

let el = document.getElementById('id');
let value = el.style['opacity'];
// or
let el = document.getElementById('id');
let value = el.style.opacity;
Read the computed value of opacity
in Javascript.
The calculated value is the one resulting from the cascade of inheritances. It is returned in the form of a decimal number between 0
and 1
, even if it was defined in percentages.

let el = document.getElementById('id');
let value = window.getComputedStyle(el).getPropertyValue('opacity');
Change the opacity with JQuery.

$('#id').css('opacity', '0.5');
Read the computed value of opacity
in JQuery
The value of opacity is returned as a decimal number between 0
and 1
.

let value = $('#id').css('opacity');
Other code examples.
Other examples of Javascript and JQuery code are provided on the page Javascript and CSS.
Take the test yourself.
The buttons below apply the entered value to the property opacity
and then display either the value as it has been applied, or the calculated value.
This second option allows us to see how the value of opacity
is stored (serialized).
It is particularly noticeable that percentages are converted to decimal numbers.
Simulator.
The opacity set on an element affects the entire group composed of the element itself and its descendants. This is particularly noticeable on the table, where we can see that the transparency also applies to the rows and cells of the table.
Effect of opacity on text

1 | 2 | 3 |
4 | 5 | 6 |
Browsers compatibility with opacity
.
The property opacity
is very well managed by the main browsers. No compatibility issues have been reported, including
for values expressed in percentages, which took a little longer to be accounted for.
the opacity
value.opacity
property
percentage
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Opéra Mobile

Chrome

Edge

Opéra

Safari

Safari sur IOS

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Samsung Internet

Firefox

Baidu Browser

QQ Browser

UC Browser pour Androïd

Opéra mini
Historic of the opacity
property.
-
CSS Color Module Level 3
Introduction of theopacity
property.June 23, 1999Working Draft.May 14, 2003Candidate Recommendation.October 28, 2010Proposed for recommendation.June 07, 2011Recommendation . -
CSS Color Module Level 4
Adding percentages as a value for theopacity
property.July 05, 2016Working Draft.July 05, 2022Candidate Recommendation.
See also, regarding colors.
The CSS specifications published by the W3C are organized into modules. The property opacity
is part of the CSS Color Module.
The following definitions are also described in this same module.
Properties:

Functions:

sRGB
.




L*a*b*
system.
L*C*H*
system.
L*a*b*
system with a perceptual correction.
L*C*H*
system with a perceptual correction
At-rules:

color()
function.Descriptors :

@color-profile
at-rule.
@color-profile
directive. Defines the method for converting from one color profile to another.