Skip to main content

How to Make an Element Transparent with opacity in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

opacity fades a whole element, background and text together. 1 is fully visible, 0 is invisible, and anything in between lets what's behind the element show through.

Syntax​

Syntax
<selector> {
opacity: <amount>;
}
PartWhat it does
<selector>The element to fade.
<amount>A number from 0, invisible, to 1, fully visible, such as 0.6. A percentage, such as 60%, works too.

MDN's opacity page explains that opacity applies to the element as a whole, including its contents.

The Example​

The example is three blue paragraphs with white text: one fully visible, one at 0.6 and one at 0.3.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element giving every paragraph a 0f6cbd background and white text, a class faded with opacity 0.6 and a class very-faded with opacity 0.3, and three paragraphs, the second with the class faded and the third with very-faded.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Opacity</title>
<style>
p {
background: #0f6cbd;
color: #ffffff;
}

.faded {
opacity: 0.6;
}

.very-faded {
opacity: 0.3;
}
</style>
</head>
<body>
<p>Fully visible</p>
<p class="faded">opacity: 0.6</p>
<p class="very-faded">opacity: 0.3</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: three blue bars with white text, Fully visible in solid blue, opacity: 0.6 lighter, and opacity: 0.3 lighter still.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The three paragraphs have the opacities 1, 0.6 and 0.3. The first has no opacity rule, and 1 is the default.
  • The white page shows through the faded ones, so their blue looks lighter.
  • The text faded with the background, because opacity applies to the whole element.

Common Mistakes​

MistakeWhat happensDo this instead
Giving a child opacity: 1 to keep it solid inside a faded parentThe child stays faded: inside a parent at 0.3, a paragraph with opacity: 1 still looked fadedFade only the parent's background, with a background color that has transparency
Using a value above 1It's treated as 1: opacity: 6 gave 1Use a number from 0 to 1

MDN's page suggests the fix in the first row: to change the opacity of a background only, use a background color with an alpha channel, such as rgb(0 0 0 / 40%).

Troubleshooting​

SymptomLikely causeFix
The text is faded tooopacity fades the whole elementUse a background color with transparency instead
Part of an element can't be made solid againA parent element has a lower opacityRemove opacity from the parent

FAQ​

Can I write opacity as a percentage?​

Yes. opacity: 60% gave the same 0.6 as opacity: 0.6.

Is an element with opacity: 0 gone?​

No. MDN's page explains that it's invisible but still part of the page: it still responds to the mouse, and if it's in the tab order, it still gets focus.

Conclusion​

Set opacity between 0 and 1 to fade an element and everything inside it. To fade only a background, give it a color with transparency instead.

Additional Resources​