CSS Opacity
You can paint solids, photos, and gradients. Sometimes you need the whole element to look softer — a muted overlay, a disabled button, a card that steps back. That is the job of opacity: how solid or see-through the entire box appears.
What does opacity do?
The opacity property sets how opaque an element is. The value runs from 1 (fully solid, the default for most elements) down to 0 (fully invisible). Decimal values like 0.5 sit in between. Importantly, opacity applies to the element and everything inside it — text, borders, backgrounds, and child boxes all fade together.
Opacity vs color alpha
These two ideas are easy to mix up. Keep them separate.
📊 Opacity vs color alpha
| Tool | What fades | Typical use |
|---|---|---|
opacity |
The whole element (and its children) | Soft overlays, disabled look, fade-in motion later |
rgba() / hsla() alpha |
Only that color layer | Transparent backgrounds with solid text on top |
transparent keyword |
A fully clear color value | Hiding a border color without removing the property |
visibility: hidden |
Hides visually but keeps layout | Different tool — not covered in depth here |
Fully solid and half faded
Start by comparing 1 and 0.5 on the same card design.
Solid vs half opacity
css.card-solid {
opacity: 1;
background-color: #ccfbf1;
color: #042f2e;
padding: 1rem;
}
.card-soft {
opacity: 0.5;
background-color: #ccfbf1;
color: #042f2e;
padding: 1rem;
}
What you should notice
On .card-soft, both the background and the text look washed out. Opacity does not spare the letters.
Children fade with the parent
If a parent has opacity: 0.4, nested headings and buttons cannot “opt out” by setting their own opacity: 1. They are already painted inside a faded layer.
Parent opacity washes children
css.overlay {
opacity: 0.4;
background-color: #0f172a;
color: white;
padding: 1rem;
}
.overlay strong {
opacity: 1; /* cannot restore full strength inside a faded parent */
}
Design implication
Need a frosted panel with crisp text? Fade the background color with rgba, not the whole element with opacity.
Prefer rgba when only the fill should soften
Here the panel background is translucent, but the text stays solid at full strength.
Soft fill, solid text
css.panel {
background-color: rgba(15, 118, 110, 0.25);
color: #042f2e; /* full strength text */
padding: 1rem;
}
Muted UI habit
A common pattern: slightly lower opacity on secondary actions or disabled controls so they feel quieter — still keep contrast readable.
Muted secondary button
css.btn-secondary {
opacity: 0.75;
background-color: #e2e8f0;
color: #0f172a;
border: none;
padding: 0.5rem 1rem;
}
Zero is invisible — space may remain
opacity: 0 makes the element invisible, but it can still occupy layout space and may still receive pointer events depending on context. If you need it gone from the layout, that is a different tool (display: none), not opacity alone.
Invisible but still in the flow
css.ghost {
opacity: 0;
}
Try it yourself
- Build two cards with the same colors; set one to
opacity: 1and one toopacity: 0.5. - Nest a bold line inside an
opacity: 0.4parent — confirm you cannot fully “unfade” the child. - Rebuild the soft look with
background-color: rgba(…, 0.25)and solidcolortext. - Style a secondary button at
opacity: 0.75and check readability. - Optional: set
opacity: 0on a box and notice the gap it still leaves in the layout.
Common mistakes
- Putting
opacityon a parent when you only wanted a transparent background - Expecting a child with
opacity: 1to look fully solid inside a faded parent - Using
opacity: 0as a substitute for removing layout space - Dropping opacity so low that text fails contrast
- Confusing the
transparentcolor keyword with theopacityproperty
Summary
opacitysets how see-through the whole element is (0–1)- Children fade with their parent — you cannot fully override that from inside
- For soft backgrounds with solid text, prefer
rgba/hslaalpha - Low opacity can mute UI; keep accessibility in mind
- Invisible (
0) is not the same as removed from the layout - Practice the card pair and the rgba rebuild until the difference feels obvious
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top