The opacity property in CSS controls how transparent an element is. It accepts values between 0 and 1:
0 ➝ completely transparent (invisible)1 ➝ fully opaque (fully visible)0.25, 0.5, 0.8 ➝ different levels of transparencyOpacity affects the entire element — its background, text, borders, and all child elements.
0 and 1 are valid.rgba() colors if you want only the background to be transparent.Use the opacity property on any element you want to make transparent:
// Basic opacity syntax (0 = invisible, 1 = fully visible)
selector {
opacity: value;
}
The value is a floating-point number between 0 and 1. For example, 0.3 means “30% visible”.
Here are some common opacity values used for different visual effects:
/* Different opacity values for demo boxes */
.transparent-box {
opacity: 0.25;
}
.semi-transparent {
opacity: 0.6;
}
.fully-visible {
opacity: 1;
}
Sometimes you only want the background to be transparent, not the text. In that case, use rgba() colors instead of opacity:
/* Using opacity: text and children also become transparent */
.opacity-card {
background-color: #2563eb;
color: #ffffff;
opacity: 0.5;
}
/* Using RGBA: only background is transparent, text stays solid */
.rgba-card {
background-color: rgba(37, 99, 235, 0.5);
color: #ffffff;
}
All three boxes above have the same background color, but different opacity values. Notice how the first box looks much lighter because it is more transparent.
Opacity is perfect for hover effects such as fading elements in or out smoothly:
/* Button that fades in when hovered */
.fade-button {
opacity: 0.5;
transition: opacity 0.3s ease;
}
.fade-button:hover {
opacity: 1;
}
0 and 1 only; other values are invalid.rgba() instead of opacity.opacity with transition for smooth fade-in / fade-out effects.opacity: 0; hides the element visually, but it still takes up space and can still be clickable unless you change pointer events.<div> elements and set their opacity to 0.2, 0.5, and 0.8. Observe how the transparency changes.transition.rgba() for background instead of opacity and compare the result.
/* Practice: play with different opacity values */
.box {
width: 120px;
height: 80px;
margin: 8px;
background-color: #2563eb;
color: #ffffff;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 0.9rem;
}
.box-low {
opacity: 0.2;
}
.box-medium {
opacity: 0.5;
}
.box-high {
opacity: 0.8;
}