Why CSS opacity Also Fades Child Elements, and How to Make Only the Background Transparent
When you set opacity on a parent element, the text and buttons inside it fade too. If you want only the background to be semi-transparent, don't use opacity. Use a background color with an alpha value (transparency), such as rgb(0 0 0 / 0.5). This is the fix when you put text on a photo, or want a semi-transparent header background but the text also becomes see-through.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened a test page with Playwright, took screenshots, and read the pixel colors at fixed positions to compare them. I did not test Firefox or Safari.
opacity makes an element and its children transparent together
I placed a parent element with a black background on top of a light blue background (rgb(0 200 232)), and put a child element with a white background inside it. I then set opacity: 0.5 on the parent and read the colors on screen.
| Where measured | Color on screen |
|---|---|
| Parent background (black) | rgb(0, 99, 115) |
| Child background (white) | rgb(128, 227, 243) |
The child's white also blended with the light blue underneath, at half strength. Setting opacity: 1 on the child did not change the result. The child's opacity read by getComputedStyle() was 1 whether I set it or not. The child's opacity stays at 1, yet it still looks transparent.
The reason is that opacity does not apply to each element separately. It treats an element and all its contents as one unit and makes that whole unit transparent. The opacity section of the CSS Color Module Level 4 spec explains this by describing how the element, including its descendants, is first drawn into an off-screen image, and that image is then composited onto the screen using the opacity value. opacity is not inherited, so the child's value stays at 1. However, the child is part of the image of the parent, so it fades along with it. Setting a value greater than 1 on the child, such as opacity: 2, does not cancel this. opacity is clamped to the range 0 to 1, and the child's value read by getComputedStyle() was 1.
You can see that the elements are treated as one unit when two children overlap. I placed two black squares, each overlapping the other by half, on a white background.
| Setting | Only one square | Where the two overlap |
|---|---|---|
opacity: 0.5 on the parent |
rgb(126, 126, 126) |
rgb(126, 126, 126) |
opacity: 0.5 on each child |
rgb(127, 127, 127) |
rgb(63, 63, 63) |
With the setting on the parent, the overlapping area is the same gray as the rest, so the two squares fade as one shape. With the setting on each child, the semi-transparency is applied twice where they overlap, so only that area gets darker.
To make only the background color transparent, add an alpha value to the color
To make only the background transparent, remove opacity and use a semi-transparent color for the background itself. MDN's opacity page also says that to change only the background's opacity, you should use a color with an alpha channel in background.
.card {
/* Fill with black at 50% opacity */
background: rgb(0 0 0 / 0.5);
}
In the rgb() function, you write the alpha value after a slash (/). 0 (0%) is fully transparent and 1 (100%) is fully opaque. rgba() is an alias of rgb(), and the older comma-separated form rgba(0, 0, 0, 0.5) produced the same color.
On the same page, I removed the parent's opacity and set the background to rgb(0 0 0 / 0.5). The parent background was rgb(0, 100, 116), almost the same color as with opacity: 0.5. The child background was rgb(255, 255, 255), staying white and not becoming transparent.
If the color is stored in a variable, use color-mix() or a relative color
You may store a color in a custom property (such as --bg: #000) and want to make that color semi-transparent. In that case, mix it with transparent using color-mix(), or change only the alpha value with the relative color syntax rgb(from …).
.card {
--bg: #000;
/* Make the --bg color 50% opaque (use one of the two lines) */
background: color-mix(in srgb, var(--bg) 50%, transparent);
/* background: rgb(from var(--bg) r g b / 50%); */
}
Both versions gave the same result as rgb(0 0 0 / 0.5): the parent background was rgb(0, 100, 116) and the child background was rgb(255, 255, 255). The background-color value read by getComputedStyle() was color(srgb 0 0 0 / 0.5) in both cases. With relative colors, you keep the red, green, and blue of the original color as they are, and write a new alpha value after the slash, as in rgb(from original-color r g b / alpha) (from the relative color section of MDN's rgb() page).
To make a background image transparent, move it to ::before
You cannot add an alpha value to a background image (background-image) the way you can with a color. To make only the image transparent, move the background image to a ::before pseudo-element and apply opacity to that pseudo-element. A pseudo-element does not contain the child text or buttons, so only the image becomes transparent.
.card {
position: relative;
isolation: isolate; /* Keep ::before from going behind the background outside .card */
}
.card::before {
content: "";
position: absolute;
inset: 0;
z-index: -1; /* Place behind the text and buttons */
background-image: url("photo.jpg");
opacity: 0.5;
}
I tested this on a page with a section that has a background color outside the card. The ::before background image was a solid black gradient (linear-gradient(#000, #000)). The color where the parent's background should be was rgb(0, 100, 116), which is black at half strength, and the child background stayed white.
When I removed isolation: isolate, the area where the parent's background should be turned light blue (rgb(0, 200, 232)), and the black of ::before disappeared. When I set the outer section background to transparent, the black showed again (rgb(0, 100, 116)) even without isolation. This shows that the z-index: -1
::before was being hidden behind the section background.
As described on MDN's isolation page, isolation: isolate creates a new stacking context for the element. When I set it on the parent, the z-index: -1
::before was no longer hidden behind the outer background, and it appeared behind the text and buttons.






