CSS Opacity & Visibility
Control element transparency and visibility in CSS. Opacity creates see-through effects, visibility toggles element rendering without removing layout space, and these properties are essential for overlays, modals, and fade animations.
Opacity & Visibility
- opacity: 1 — Fully visible (default). opacity: 0.5 = 50% transparent. opacity: 0 = invisible (but still in layout)
- opacity affects children — If parent is opacity: 0.5, ALL children are also 50% transparent. No way to override
- RGBA for background only — rgba(0,0,0,0.5) makes ONLY the background transparent, not the text inside
- visibility: hidden — Element is invisible but keeps its space. visibility: visible shows it again
- display: none — Removes element completely from layout (no space). Different from opacity: 0 and visibility: hidden
- pointer-events: none — Element is visible but cannot be clicked. Useful for disabling interactions without changing appearance
- Fade effect — Combine opacity with transition for smooth fade-in/fade-out animations
Opacity & Visibility Code
/* Overlay with transparent background */
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5); /* Only BG is transparent */
}
/* Image hover overlay */
.image-card {
position: relative;
overflow: hidden;
}
.image-card .overlay {
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.6);
opacity: 0;
transition: opacity 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.image-card:hover .overlay {
opacity: 1;
}
/* Disabled button */
.btn:disabled {
opacity: 0.5;
pointer-events: none;
cursor: not-allowed;
}
/* Comparison of hiding methods: */
.method-1 { display: none; } /* No space, no events */
.method-2 { visibility: hidden; } /* Keeps space, no events */
.method-3 { opacity: 0; } /* Keeps space, HAS events! */Tip
The image hover overlay pattern (opacity: 0 → 1 on :hover with transition) is widely used on portfolio and gallery sites. Combine it with a flex centering trick inside the overlay div to perfectly center a 'View Project' button on hover.
Display determines an element's rendering behavior
Common Mistake
Using opacity: 0 to hide elements when you actually need to remove them from interaction. With opacity: 0, the element keeps its space AND still receives click events — causing invisible buttons to accidentally intercept user clicks. Use visibility: hidden or display: none for non-interactive hidden elements.
Practice Task
Build an image card with a hover overlay: (1) card with position: relative and overflow: hidden, (2) overlay with position: absolute; inset: 0; opacity: 0; background: rgba(0,0,0,0.6), (3) add transition: opacity 0.3s ease, (4) on .card:hover .overlay set opacity to 1. Add a centered 'View' button inside the overlay.
Quick Quiz
Key Takeaways
- Control element transparency and visibility in CSS.
- opacity: 1 — Fully visible (default). opacity: 0.5 = 50% transparent. opacity: 0 = invisible (but still in layout)
- opacity affects children — If parent is opacity: 0.5, ALL children are also 50% transparent. No way to override
- RGBA for background only — rgba(0,0,0,0.5) makes ONLY the background transparent, not the text inside