CSS opacity
The CSS opacity property was introduced in CSS3 to enable developers to enable transparency in HTML elements.
The opacity property allows you to specify a level of opacity against an object so that it becomes semi-transparent, or even fully transparent (if that's the desired effect).
Syntax
Possible Values
Here are the possible values:
<alpha-value>- Specifies the amount of transparency that should be applied to the element. This can be provided as a number or a percentage. A value of
0.0(or0%) specifies fully transparent and a value of1.0(or100%) specifies fully opaque (no transparency).
In addition, all CSS properties also accept the following CSS-wide keyword values as the sole component of their property value:
initial- Represents the value specified as the property's initial value.
inherit- Represents the computed value of the property on the element's parent.
unset- This value acts as either
inheritorinitial, depending on whether the property is inherited or not. In other words, it sets all properties to their parent value if they are inheritable or to their initial value if not inheritable.
Basic Property Information
- Initial Value
1- Applies To
- All elements
- Inherited?
- No
- Media
- Visual
- Animatable
- Yes (see example)
Example Code
Basic CSS
Working Example within an HTML Document
CSS Specifications
- The
opacityproperty is defined in CSS Color Module Level 4.
More Transparency
The opacity property isn't the only way to specify opacity/transparency in CSS. CSS provides for alpha colors, as well as the transparent keyword. More about these below.
- Alpha Color Functions
Modern CSS allows you to specify the opacity directly at the color level using functions like
rgb()andhsl()(and their alpha-specific versionsrgba()andhsla()).For example,
background-color: rgb(0 0 0 / 0.4)provides the color black (i.e.0 0 0) with an opacity of 0.4. And for an HSL example,background-color: hsl(0 100% 50% / 0.8)specifies the color red (i.e.0 100% 50%) with an opacity of 0.8.- The
transparentKeyword The
transparentkeyword can be applied against any element when using thebackground-color,border-colororcolorproperties.The CSS
transparentkeyword specifies that the element should be fully transparent. It is shorthand forrgba(0,0,0,0)(transparent black).In CSS 1, the
transparentkeyword was limited to thebackground-colorproperty. In CSS level 2, it was extended to theborder-colorproperty. In CSS 3, thetransparentkeyword has been extended to any element that uses thecolorproperty.
Browser Support
Browser support for CSS properties can vary, particularly for newer or experimental features. Check current browser compatibility before using a property in production, especially when supporting older browsers.
The following table provided by Caniuse.com shows the level of browser support for this feature.
Initial Value
CSS properties have an initial value that applies when the property has not been explicitly set. The initial value is different from the value inherited from a parent element and may vary between properties.
Inheritance
Some CSS properties inherit their computed value from their parent element by default, while others do not. The inherit, initial, unset, and revert keywords can also be used to control how a property gets its value.
Vendor Prefixes
Most modern CSS properties use the standard, unprefixed syntax. Vendor-prefixed properties such as -webkit- are mainly relevant to older browser versions or specific features and should only be used when required by your browser support targets.