CSS opacity

View Output Full Screen Preview

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 (or 0%) specifies fully transparent and a value of 1.0 (or 100%) 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 inherit or initial, 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

View Output Full Screen Preview

CSS Specifications

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() and hsl() (and their alpha-specific versions rgba() and hsla()).

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 transparent Keyword

The transparent keyword can be applied against any element when using the background-color, border-color or color properties.

The CSS transparent keyword specifies that the element should be fully transparent. It is shorthand for rgba(0,0,0,0) (transparent black).

In CSS 1, the transparent keyword was limited to the background-color property. In CSS level 2, it was extended to the border-color property. In CSS 3, the transparent keyword has been extended to any element that uses the color property.

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.