On this page9 sections
Lecture 019: Opacity
| Category | 4: Layout Basics |
| Lecture | 019 of 057 |
| Estimated video length | 6 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
opacitymakes an element see-through, from fully visible (1) to invisible (0). It's perfect for fading images on hover and dimming disabled items. Just remember that it fades everything inside the element, text included.
Learning Objectives
- Fade elements with
opacity - Animate opacity on hover
- Make only the background transparent with an RGBA colour
1. The Opacity Scale
Example
<style>
img { margin: 4px; border-radius: 6px; }
</style>
<img src="https://picsum.photos/id/1080/120/90" alt="Strawberries" style="opacity: 1;">
<img src="https://picsum.photos/id/1080/120/90" alt="Strawberries" style="opacity: 0.6;">
<img src="https://picsum.photos/id/1080/120/90" alt="Strawberries" style="opacity: 0.3;">2. Fade on Hover
Example
<style>
.gallery img { opacity: 0.6; transition: opacity 0.3s; border-radius: 6px; margin: 4px; }
.gallery img:hover { opacity: 1; }
</style>
<div class="gallery">
<img src="https://picsum.photos/id/1015/140/100" alt="River valley">
<img src="https://picsum.photos/id/1016/140/100" alt="Canyon">
<img src="https://picsum.photos/id/1018/140/100" alt="Mountains">
</div>3. Opacity Fades the Children Too
Example
<style>
.box { padding: 14px; margin-bottom: 8px; color: white; font-weight: bold; }
</style>
<div class="box" style="background: #1c7ed6; opacity: 0.4;">opacity: 0.4 fades the text too</div>
<div class="box" style="background: rgb(28 126 214 / 0.4); color: #0b3a66;">An RGBA background fades only the colour</div>To make only the background transparent, use an alpha colour: rgb(… / 0.4) or rgba(…, 0.4).
4. Text on a Photo
Example
<div style="background: url('https://picsum.photos/id/1036/500/220') center / cover; padding: 60px 20px;">
<p style="background: rgb(255 255 255 / 0.75); padding: 12px; margin: 0;">
A semi-transparent white panel keeps this text readable over the photo.
</p>
</div>Common Beginner Mistakes
1. Using opacity for a see-through background. The text fades too. Use an RGBA background.
2. opacity: 0 to hide things. The element is invisible but still clickable. Use visibility: hidden or display: none.
3. Low-contrast faded text. It becomes hard to read.
Practice Questions
- What's the range of
opacityvalues? - Make an image half transparent.
- Why does
opacity: 0.5on a card also fade its text? - How do you fade only the background colour?
- Is an element with
opacity: 0still clickable?
Practice Answers
- From 0 (invisible) to 1 (fully visible).
img { opacity: 0.5; }- Opacity applies to the whole element, including all its children.
- Use an alpha colour, e.g.
background: rgb(0 0 0 / 0.5); - Yes. It's invisible but still there.
Key Takeaways
opacity: 0–1fades a whole element, children included.- Combine with
transitionfor smooth hover fades. - For transparent backgrounds only, use RGBA / alpha colours.