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+
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index