CSS Opacity
In this page:
The Opacity Scale
The opacity property sets the transparency level of an element. It accepts decimal values on a scale from 0.0 (completely transparent and invisible) to 1.0 (fully opaque and solid). Changing an element's opacity affects the element and all of its nested children.
Note: Use opacity changes to create smooth hover transitions and animations on your links or buttons.
Warning: Lowering an element's opacity makes all its nested children transparent as well, which can make text unreadable.
Example: The Opacity Scale
<style>
.faint {
background: seagreen;
color: white;
padding: 10px;
opacity: 0.3;
}
.solid {
background: seagreen;
color: white;
padding: 10px;
opacity: 1;
}
</style>
<div class="faint">30% opacity</div>
<div class="solid">Fully opaque</div>
Interactive Hover Transitions
You can use opacity to create elegant, responsive hover transitions on your buttons, links, or cards. By dimming the element slightly in its normal state and restoring full opacity on hover, you let users know that the element is interactive.
Note: Combine opacity changes with the CSS transition property to create smooth, polished hover animations.
Warning: Make sure your dimmed elements still have enough color contrast to be easily readable.
Example: Interactive Hover Transitions
<style>
.btn {
opacity: 0.7;
transition: opacity 0.2s;
}
.btn:hover {
opacity: 1;
}
</style>
<button class="btn">Hover me</button>
Opacity vs. RGBA Transparency
If you want to create a semi-transparent container background (like an overlay) but want its nested child elements (like titles or text) to remain solid and opaque, do not use the opacity property. Instead, use an RGBA color value for your background-color property.
Note: Use RGBA background-color rules to create semi-transparent overlay boxes without affecting nested text readability.
Warning: The opacity property affects the element and all of its nested children, making it poor for styling text containers.
Example: Opacity vs. RGBA Transparency
<style>
.opacity-box {
background: seagreen;
color: white;
padding: 10px;
opacity: 0.5;
}
.rgba-box {
background: rgba(0, 0, 0, 0.5);
color: white;
padding: 10px;
}
</style>
<div class="opacity-box">Text is also faded</div>
<div class="rgba-box">Text stays fully readable</div>
Opacity vs. visibility: hidden
If you want to hide an element on your page completely, you can set its opacity to 0 or use the visibility: hidden property. While both make the element invisible, the element still occupies its original layout space in the document flow, unlike setting display: none.
Note: Use opacity: 0 to hide an element that you want to fade into view smoothly later using JavaScript.
Warning: Invisible elements with opacity: 0 are still clickable by users on the screen unless you also set pointer-events: none.
Example: Opacity vs. visibility: hidden
<style>
.opacity-hidden {
opacity: 0;
}
</style>
<p>Before</p>
<p class="opacity-hidden">Invisible, but still takes up space and is clickable</p>
<p>After</p>
Opacity in Layered Backdrops
You can use opacity rules to build beautiful layered backdrops and complex image textures, blending background assets with foreground layers cleanly.
Note: Fade secondary backdrop containers slightly to make your primary card typography pop.
Warning: High transparency on primary cards can violate accessibility contrast guidelines.
Example: Opacity in Layered Backdrops
<style>
.backdrop {
opacity: 0.6;
}
.card {
position: relative;
}
</style>
<div class="card">
<img class="backdrop" src="https://placehold.co/300x150" alt="Faded backdrop">
<p>Foreground text pops</p>
</div>
- Using the opacity property to make background colors transparent, which accidentally makes all nested text unreadably transparent.
- Attempting to click links or buttons that are invisible (opacity: 0) but still active on the screen.
- Lowering card opacity so much that it violates standard web accessibility contrast thresholds.
- The opacity property sets the transparency level of an element on a scale from 0.0 to 1.0.
- Opacity affects the element and all of its nested children, making it poor for text container backgrounds.
- To create transparent container backgrounds with solid, readable text, use RGBA or HSLA color values instead.
The opacity property and standard RGBA color systems are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline