Code has been added to clipboard!

The Opacity Property in CSS: Transparency Explained

Reading time 1 min
Published Aug 8, 2017
Updated Oct 15, 2019

Manipulating CSS transparency

By using the opacity property, you can define a custom CSS transparency level for an element:

Example
div {
    opacity: 0.6;
}

The level of opacity or transparency in CSS applies to the whole element, including its text and other content.

Syntax requirements

To define CSS transparency for an element, follow the syntax example below:

opacity: alpha;

The alpha level is a unitless number in the range from 0.0 (full CSS transparency) to 1.0 (full CSS opacity). 1.0 is also the default value of this property.

CSS transparency: alternatives

Another way to change the opacity for an element is using the filter property. In the example below, you can see how filtering can be used to set various levels of CSS background color opacity:

Example
div {
    background-color: #41caf4;
    padding: 15px;
}

div.first {
    opacity: 0.2;
    filter: alpha(opacity=20); /* For IE8 and earlier */
}

div.second {
    opacity: 0.4;
    filter: alpha(opacity=40); /* For IE8 and earlier */
}

div.third {
    opacity: 0.8;
    filter: alpha(opacity=80); /* For IE8 and earlier */
}

You can also define the color you want to use in RGBA or HSLA values. The A in the acronyms stand for the alpha channel which is defined as the fourth value. See how it affects the CSS background color opacity in the example below:

Example
div {
    background: rgb(80, 180, 90);
    padding: 10px;
}

div.firstly {
    background: rgba(80, 180, 90, 0.2);
}

div.secondly {
    background: rgba(80, 180, 90, 0.4);
}

div.thirdly {
    background: rgba(80, 180, 90, 0.8);
}

You can change transparency using JavaScript as well:

Example
function LearnFunction(z) {
// Return the selected option's text
    var opacityL = z.options[z.selectedIndex].text;
    var ell = document.getElementById("p1");
    if (ell.style.opacityL !== undefined) {
        ell.style.opacityL = opacityL;
    } else {
        alert("This example is unsupported!");
    }
}

Browser support

Chrome
1+
Edge
12+
Firefox
1+
IE
9+
Opera
9+
Safari
2+

Mobile browser support

Chrome
18+
Firefox
4+
Opera
10.1+
Safari
1+
CSS3 Features
Syntax
Stylesheets
Inline
Internal
External
Children Selectors
Fonts
Colors
Gradients
Opacity / Transparency
Shadow Effects
Layout — Display
Layout — Float
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Borders
Margin
Padding
Height
Links
Lists
Tables
Navigation Bar
Images
Forms
Buttons
Box-Sizing
Code Examples
@media
border-image
box-shadow
flex-shrink
margin
opacity
position
text-align
text-transform
word-wrap