How to Make an Element Transparent with opacity in CSS
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
<selector> {
opacity: <amount>;
}
| Part | What 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:

<!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 screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- The three paragraphs have the opacities
1,0.6and0.3. The first has noopacityrule, and1is the default. - The white page shows through the faded ones, so their blue looks lighter.
- The text faded with the background, because
opacityapplies to the whole element.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Giving a child opacity: 1 to keep it solid inside a faded parent | The child stays faded: inside a parent at 0.3, a paragraph with opacity: 1 still looked faded | Fade only the parent's background, with a background color that has transparency |
| Using a value above 1 | It's treated as 1: opacity: 6 gave 1 | Use 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
| Symptom | Likely cause | Fix |
|---|---|---|
| The text is faded too | opacity fades the whole element | Use a background color with transparency instead |
| Part of an element can't be made solid again | A parent element has a lower opacity | Remove 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
- opacity, on MDN.
