You will use CSS opacity to make an element, its text, and its children partially or fully transparent, and choose when to use opacity instead of transparent colors.
What it is
opacity controls how transparent an element is. A value of 1 is fully opaque, 0 is fully transparent, and values between them blend the element with what is behind it. The property applies to the whole element as a group: background, border, text, images, and child elements are all affected.
Related terms include alpha (the transparency channel in colors), stacking context (a layer created when opacity is less than 1), and compositing (how the browser blends layers).
Why it matters
- Creates watermarks, overlays, and disabled-looking controls.
- Enables smooth fade-in and fade-out animations with
transitionor@keyframes. - Helps focus attention by dimming non-active content.
- Allows layered designs without removing elements from the page flow.
Syntax or steps
The smallest useful pattern is:
.faded {
opacity: 0.5;
}
Use a number from 0 to 1. To fade only on hover, add a transition:
.card {
opacity: 1;
transition: opacity 0.3s ease;
}
.card:hover {
opacity: 0.7;
}
Example
A common use is a faint watermark over content:
<div class="watermark">CONFIDENTIAL</div>
.watermark {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(-20deg);
font-size: 4rem;
color: #000;
opacity: 0.15;
pointer-events: none;
}
Part by part:
position: absolutelets the watermark sit over other content.top,left, andtransformcenter and rotate it.opacity: 0.15makes it very faint.pointer-events: noneprevents the invisible text from blocking clicks.
Common mistakes
- Using opacity to fade only a background. If you set
opacityon a parent, its text and children fade too. Usergba()orhsla()for the background color instead. - Thinking
opacity: 0removes the element. It still takes up space and may still receive clicks. Usedisplay: noneto remove layout, orvisibility: hiddento hide while keeping space. - Writing
opacity: 50oropacity: 50%. The value must be a number between0and1, such as0.5. - Forgetting stacking context. An element with opacity below
1creates a new stacking context, which can change howz-indexworks.
When to use it
Use opacity when you want the entire element to become transparent. Use transparent colors when you only want the background or border to be see-through.
| Method | Best for | Affects children? | Keeps layout? |
|---|---|---|---|
opacity | Fading a whole component or overlay | Yes | Yes |
rgba() / hsla() | Transparent background or border only | No | Yes |
visibility: hidden | Hiding content while preserving space | Yes | Yes |
display: none | Removing content from layout | Yes | No |
Practice
Guided exercise: Create a .watermark element with opacity: 0.2 and pointer-events: none. Expected result: the text is faint and does not block clicks on elements underneath.
Challenge: Make a card background semi-transparent without fading the card text. Hint: use background-color: rgba(255, 255, 255, 0.7); instead of opacity on the card.
Quick check
If a parent has opacity: 0.5 and a child has opacity: 1, what does the child look like?
The child appears faded as part of the parent group, because the parent’s opacity is applied to the whole rendered element.
Summary
opacity is a simple way to make an entire element transparent, useful for overlays, watermarks, and fades. Remember that it affects children and creates a stacking context, so use transparent colors when you need only a background to be see-through.