Same brown tile, three opacities. The white page shows through more and more, which is why the tiles get paler rather than darker. Put the tiles on a dark background and they’d get darker instead: opacity mixes an element with whatever is behind it.
Opacity values: 0 to 1, or a percentage
Any number from 0 to 1 works, and so does a percentage: opacity: 60% is the same as
opacity: 0.6. Numbers outside the range don’t cause an error, they’re clamped, so
opacity: 1.5 is just 1. Percentages are newer; very old browsers only understood the decimal form, so
many stylesheets still use that.
0.9to0.8: a gentle fade, good for a secondary link or a quiet icon.0.5to0.6: clearly faded, the usual disabled look.0: invisible, but still taking up space and still clickable (see below).
Opacity fades everything inside
Opacity applies to the element as a whole, children included, and a child can’t opt out:
The white box sets opacity: 1, but that means “as solid as my parent allows”, and the parent is 40%.
The browser paints the whole card, then fades the result. If you want a faded background behind solid text, don’t
fade the card. Make its background transparent instead.
A see-through background, solid text
Both cards below sit on a striped wall and both let half of it show through. The first uses
opacity: 0.5, the second a white background at 50%:
In the first card the text is faded along with everything else and the stripes shine through the letters. In the second, only the background lets the stripes through and the text stays fully dark. For cards over photos, frosted menus and overlays, the second way is nearly always the one you want.
Any color format can carry an alpha (transparency) value. These all give the same half-transparent black:
/* the same half-transparent black, three ways */
.shade { background: rgb(0 0 0 / 50%); }
.shade { background: rgba(0, 0, 0, 0.5); }
.shade { background: #00000080; }
/* any color: hsl works too */
.tint { background: hsl(25 47% 33% / 20%); }
In the 8-digit hex code, the last two digits are the alpha: 80 is 128 out of 255, so about 50%.
FF is solid and 00 is invisible. The slash form, rgb(0 0 0 / 50%), is the
easiest to read later. The HEX to RGB converter turns one into the other.
Fade on hover
Starting a little faded and coming to full strength on hover is a quiet way to show something is clickable. Add a transition so it eases in rather than snapping:
The rule includes :focus-visible as well as :hover, so someone tabbing through with a
keyboard sees the same change. Opacity is one of the cheapest things for a browser to animate, which makes it a
good choice for hover effects; the transitions tutorial shows how to
fade things in and out completely.
A disabled look
Lowering the opacity of a disabled button is the most common way to show it can’t be used yet:
The style is only half of it. The disabled attribute is what actually stops clicks and tells screen
readers the button is unavailable; opacity on its own would leave a faded button that still works. The WCAG
contrast rules don’t cover disabled controls, but people still need to read the label to know what they’ll be able
to do, so don’t go much below 0.5. If it matters why the button is disabled (“Add your address first”), say so
in text next to it.
opacity: 0 is still there
An element at opacity: 0 is invisible, but it still takes up space, still catches the mouse, and can
still be reached with the Tab key and read by screen readers. Here a “Saved!” message has faded out, and it’s
still sitting on top of the link:
Hover the link: it doesn’t turn red, and clicking it would do nothing, because the invisible message gets the mouse first. Two fixes, depending on what you need:
-
visibility: hiddenalongsideopacity: 0takes it away for the mouse, the keyboard and screen readers. Fading both is the usual pattern for menus and messages. -
pointer-events: nonelets the mouse go straight through it, but keyboard users can still Tab into anything inside. Fine for a purely decorative layer.
Add either line to .toast and the link works again. The display
tutorial compares opacity: 0, visibility: hidden and display: none side by
side.
Opacity changes what sits on top
Fading a box can also change which box is in front. When boxes overlap, later ones in the HTML normally cover earlier ones. Give the earlier one an opacity below 1, and it jumps in front:
The blue card comes first in the HTML, so the green one should cover its corner. But with opacity: 0.9,
blue is drawn in a later painting step and ends up on top. Change it to opacity: 1 and green covers it
again. The same thing explains a dropdown that disappears behind the next section as soon as a parent fades:
opacity below 1 creates a stacking context, and a z-index inside it can’t climb out.
The position and z-index tutorial walks through that case.
Transparent versions of a color
With a brand color in a CSS variable, you often want a pale, see-through version for backgrounds. Writing the hex
code with an alpha by hand means keeping two copies in sync. color-mix() builds it from the variable:
:root {
--brand: #7c4a2d;
}
.badge {
/* the brand color at 15%, for a soft background */
background: color-mix(in srgb, var(--brand) 15%, transparent);
color: var(--brand);
}
Mixing with transparent keeps the color and lowers the alpha, so the badge gets a light brown
background and solid brown text. Change --brand and both follow. Newer browsers also understand
relative colors, which do the same with a different syntax:
.badge {
background: rgb(from var(--brand) r g b / 15%);
}
Both worked in current Chrome, Edge, Firefox and Safari when this page was written, with color-mix()
supported for longer. The CSS variables tutorial covers
var() itself.
Questions people ask
How do I make an element transparent in CSS?
Use opacity with a value between 0 and 1: .card { opacity: 0.5; } makes the card half see-through. 1 is fully solid and 0 is invisible. A percentage works too, so opacity: 50% means the same as 0.5.
How do I make only the background transparent, not the text?
Don't use opacity, because it fades the text too. Give the background a color with an alpha value instead: background: rgb(255 255 255 / 75%); or the 8-digit hex #ffffffbf. The background lets what's behind it show through and the text stays fully solid.
Why can't a child element be more opaque than its parent?
Opacity applies to the parent and everything inside it as one picture: the browser paints the whole element, then fades the result. Setting opacity: 1 on a child only means "as solid as the parent allows". To keep a child solid, make the parent's background transparent with an rgb() or hex alpha color instead of using opacity.
What is the difference between opacity: 0 and visibility: hidden?
Both make the element invisible and keep its space. With opacity: 0 it is still there for everything else: it catches clicks, can be reached with the Tab key and is read by screen readers. visibility: hidden takes it away from the mouse, the keyboard and screen readers. Fade menus with both: opacity for the fade, visibility so the hidden menu can't be clicked.
How do I write a transparent hex color?
Add two more digits for the alpha: #RRGGBBAA. 00 is fully transparent, FF fully solid and 80 about 50%, so #00000080 is half-transparent black. The short form works too: #0008 is the same as #00000088.
How do I fade an element on hover?
Set a starting opacity and a transition on the element, and the target opacity in :hover: a { opacity: 0.6; transition: opacity 0.2s; } a:hover, a:focus-visible { opacity: 1; }. Including :focus-visible gives keyboard users the same effect.
Why did opacity change the stacking order of my elements?
Any opacity below 1 creates a new stacking context, and the element is painted in a later step than ordinary boxes. So a faded box can suddenly cover a later sibling, and a z-index inside it can no longer rise above elements outside it. Use opacity: 1 when you don't need the fade, or give the elements that overlap an explicit position and z-index.
Is opacity bad for accessibility?
Not by itself, but faded text has less contrast, so keep body text solid and check light text with a contrast checker. Disabled controls are exempt from the WCAG contrast rules, yet people still need to read them, so don't fade them much below 0.5. And remember that an element at opacity: 0 is still read by screen readers and still reachable with the keyboard.