🚨 $100K in Sight: Follow Bitcoin’s Final Push Live! TRACK NOW

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.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

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

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

Mobile browser support

Browser image
Chrome
18+
Browser image
Firefox
4+
Browser image
Opera
10.1+
Browser image
Safari
1+