Question
Is it possible to use only CSS to make an element's background semi-transparent while keeping its content, such as text and images, fully opaque?
I want to do this without splitting the text and background into separate elements.
For example, with code like this:
p {
position: absolute;
background-color: green;
filter: alpha(opacity=60);
opacity: 0.6;
}
span {
color: white;
filter: alpha(opacity=100);
opacity: 1;
}
<p>
<span>Hello world</span>
</p>
The child element still appears faded because a parent's opacity affects all of its contents. Why does this happen, and what is the correct CSS approach if I want only the background to be transparent?
Short Answer
By the end of this page, you will understand why opacity affects an entire element including its children, and how to make only the background transparent using color values such as rgba() or hsla(). You will also see practical examples, common mistakes, and a small project you can build yourself.
Concept
opacity in CSS applies to the entire rendered element, not just its background. That means when you write:
.box {
opacity: 0.6;
}
CSS lowers the opacity of the whole box after it has been painted, including:
- the background
- the text
- images inside it
- borders
- child elements
So even if a child has opacity: 1, it cannot escape the parent's transparency. The parent and all of its contents are treated as one visual layer.
If your goal is to make only the background color transparent, you should not use the opacity property on the container. Instead, use a background color format that includes an alpha channel, such as:
rgba()hsla()- 8-digit hex like
#RRGGBBAA
Example:
.box {
background: rgba(0, 128, 0, 0.6);
color: white;
}
Here, only the background color is semi-transparent. The text remains fully opaque.
Mental Model
Think of opacity like lowering the transparency of a whole sheet of printed paper. If the paper contains a green background and white text, making the paper 60% opaque fades everything on it.
Using rgba() for the background is different. It is like painting the paper with a semi-transparent green highlighter before printing solid white text on top. The background is see-through, but the text stays sharp and fully visible.
Syntax and Examples
Core idea
Use a transparent background color instead of the opacity property.
Correct approach with rgba()
.box {
background-color: rgba(0, 128, 0, 0.6);
color: white;
padding: 12px;
}
<div class="box">Hello world</div>
Why this works
0, 128, 0is the green color0.6is the alpha value- only the background color becomes semi-transparent
- the text remains fully opaque
Example with hsla()
.box {
background-color: hsla(120, , , );
: white;
}
Step by Step Execution
Consider this example:
.card {
background-color: rgba(0, 0, 0, 0.5);
color: white;
padding: 10px;
}
<div class="card">Welcome</div>
What happens step by step
- The browser creates the
divelement. - It applies
padding: 10px, giving space around the text. - It applies
color: white, so the text will be white. - It applies
background-color: rgba(0, 0, 0, 0.5). - The background is painted as black with 50% opacity.
- The text is painted normally in solid white.
Because the transparency is part of the background color itself, only the background is see-through.
Compare with opacity
.card {
: black;
: white;
: ;
}
Real World Use Cases
This pattern is very common in UI design.
Common uses
- Hero banners: dark transparent overlay behind heading text on top of an image
- Modal dialogs: a semi-transparent panel with solid readable text
- Notification badges: soft background color with clear label text
- Cards and panels: slightly transparent surface over photos or gradients
- Tooltips: translucent tooltip box with sharp text
Example: hero text overlay
.hero-text {
background: rgba(0, 0, 0, 0.55);
color: white;
padding: 20px;
display: inline-block;
}
This makes the panel dark and transparent while keeping the heading readable.
Real Codebase Usage
In real projects, developers usually avoid opacity on containers unless they intentionally want to fade the entire element.
Common patterns
1. Transparent backgrounds for overlays
.overlay-card {
background: rgba(255, 255, 255, 0.85);
color: #222;
}
Used for cards on top of images or videos.
2. Pseudo-element overlay
When developers need more control, they often use ::before or ::after.
.banner {
position: relative;
color: white;
}
.banner::before {
content: "";
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 0;
}
> * {
: relative;
: ;
}
Common Mistakes
1. Using opacity on the parent element
Broken example:
.box {
background: green;
opacity: 0.6;
}
Problem:
- text and child elements also become transparent
Fix:
.box {
background: rgba(0, 128, 0, 0.6);
}
2. Trying to reset child opacity to 1
Broken example:
.parent {
opacity: 0.5;
}
.child {
opacity: 1;
}
Problem:
- the child still appears faded because it is inside the parent's rendered layer
Fix:
- remove
opacityfrom the parent - use a transparent background color instead
Comparisons
| Technique | What becomes transparent? | Good for | Avoid when |
|---|---|---|---|
opacity: 0.6 | Entire element and all children | Fading a whole button, image, or card intentionally | You want text to stay fully opaque |
background: rgba(...) | Only the background color | Panels, overlays, banners, tooltips | You need to fade the whole element |
background: hsla(...) | Only the background color | Same as rgba(), but useful if you prefer HSL | You are not comfortable with HSL values |
#RRGGBBAA | Only the background color | Modern concise color notation | Supporting very old browsers |
Cheat Sheet
Quick rule
If you want only the background to be transparent, do not use opacity on the container.
Use this
.box {
background-color: rgba(0, 128, 0, 0.6);
color: white;
}
Avoid this
.box {
background-color: green;
opacity: 0.6;
}
Transparent color options
background-color: rgba(0, 128, 0, 0.6);
background-color: hsla(120, 100%, 25%, 0.6);
background-color: #00800099;
Important rule
A child's opacity: 1 cannot override a parent's lower opacity.
FAQ
Why does opacity affect child elements in CSS?
Because opacity is applied to the entire rendered element after the browser paints both the parent and its contents.
How do I make a background transparent but keep text solid?
Use a background color with alpha, such as rgba(0, 0, 0, 0.5).
Can I set opacity: 1 on a child to cancel the parent's opacity?
No. A parent's opacity affects the final composited result of the whole subtree.
Should I still use filter: alpha(opacity=60)?
No for modern development. It is a legacy Internet Explorer technique.
Is rgba() better than opacity?
For transparent backgrounds only, yes. For fading the whole element, use opacity.
Can I use hex with transparency in CSS?
Yes. Example: #00000080 is black with transparency.
What if I need a dark overlay over an image behind text?
Use a semi-transparent background or a ::before pseudo-element overlay.
Does this work for images inside the element too?
Mini Project
Description
Build a readable notification card that sits on top of a page background image. The card should have a semi-transparent dark background, but its heading and message text must stay fully opaque and easy to read. This demonstrates the correct way to apply transparency only to the background in a practical UI component.
Goal
Create a card overlay with transparent background styling that does not fade the text.
Requirements
- Create a container with a background image or gradient.
- Add a card element with a semi-transparent background.
- Keep all text inside the card fully opaque and readable.
- Add padding and rounded corners to make the card look like a real UI component.
- Do not use the
opacityproperty on the card itself.
Keep learning
Related questions
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.
CSS Parent Selector Explained: Selecting a Parent <li> from an <a>
Learn whether CSS has a parent selector, how :has() works, and practical alternatives for styling a parent li from a child anchor.
CSS Previous Sibling Selector: What Exists and How to Work Around It
Learn whether CSS has a previous sibling selector, why it does not, and practical ways to style earlier elements using CSS alternatives.