CSS — Opacity
What is Opacity?
The opacity property controls how transparent an element is. The value ranges from 0 (fully invisible) to 1 (fully visible).
.transparent {
opacity: 0; /* invisible */
}
.semi {
opacity: 0.5; /* 50% transparent */
}
.solid {
opacity: 1; /* fully visible (default) */
}
How Opacity Works
Unlike visibility: hidden, opacity: 0 elements are:
- Still in the document flow (take up space)
- Still clickable/focusable
- Still present in the DOM
- Just invisible
.button {
opacity: 0;
pointer-events: none; /* also disable clicks */
}
Opacity vs Other Visibility Methods
| Method | Takes Space | Clickable | Transitionable |
|---|---|---|---|
opacity: 0 | Yes | Yes | Yes |
visibility: hidden | Yes | No | No (use visible) |
display: none | No | No | No |
Hover Effects
.button {
opacity: 0.8;
transition: opacity 0.2s ease;
}
.button:hover {
opacity: 1;
}
.thumbnail {
opacity: 0.7;
transition: opacity 0.3s ease;
}
.thumbnail:hover {
opacity: 1;
}
Fade-In Animation
.fade-in {
opacity: 0;
animation: fadeIn 0.5s ease forwards;
}
@keyframes fadeIn {
to { opacity: 1; }
}
Staggered Fade-In
.list-item {
opacity: 0;
animation: fadeIn 0.3s ease forwards;
}
.list-item:nth-child(1) { animation-delay: 0.1s; }
.list-item:nth-child(2) { animation-delay: 0.2s; }
.list-item:nth-child(3) { animation-delay: 0.3s; }
.list-item:nth-child(4) { animation-delay: 0.4s; }
Opacity with Transforms
.card {
opacity: 0;
transform: translateY(20px);
transition: all 0.3s ease;
}
.card.visible {
opacity: 1;
transform: translateY(0);
}
Background Opacity
To make only the background transparent (not the text), use rgba or hsla:
/* Background only — text stays solid */
.box {
background: rgba(0, 128, 255, 0.3); /* blue with 30% opacity */
color: black; /* unaffected */
}
/* Entire element transparent — text too */
.box {
opacity: 0.3;
}
Opacity and Accessibility
Screen readers can still read opacity: 0 elements. Use aria-hidden="true" if content should be completely hidden from assistive tech:
<div class="sr-only" aria-hidden="true">Decorative content</div>
Best Practices
- Use
rgba()for background transparency —opacityaffects children too - Add
transitionfor smooth fade effects - Don't use
opacity: 0to hide content from screen readers — usearia-hidden - Combine with
transformfor polished animations - Use
pointer-events: noneif you want invisible elements to not be clickable
Mini Practice
- Create a card that fades in on page load
- Add hover opacity change to a button
- Create a background overlay with
rgbatransparency - Build a staggered animation for a list of items
- Compare
opacity: 0withvisibility: hiddenbehavior
Up Next
Next: Float →
Related Topics
Frequently Asked Questions about Opacity
What is Opacity in CSS?
Opacity is a fundamental concept in CSS. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Opacity?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Opacity.
Why is Opacity important in CSS?
Opacity is essential for CSS development. Understanding this concept will help you write better code and solve real-world problems more effectively.