On this page9 sections
  1. Learning Objectives
  2. 1. The Opacity Scale
  3. 2. Fade on Hover
  4. 3. Opacity Fades the Children Too
  5. 4. Text on a Photo
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 019: Opacity

Category4: Layout Basics
Lecture019 of 057
Estimated video length6 min
TargetCSS3 · any modern browser
LevelBeginner

opacity makes 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

html
<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

html
<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

html
<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

html
<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

  1. What's the range of opacity values?
  2. Make an image half transparent.
  3. Why does opacity: 0.5 on a card also fade its text?
  4. How do you fade only the background colour?
  5. Is an element with opacity: 0 still clickable?

Practice Answers

  1. From 0 (invisible) to 1 (fully visible).
  2. img { opacity: 0.5; }
  3. Opacity applies to the whole element, including all its children.
  4. Use an alpha colour, e.g. background: rgb(0 0 0 / 0.5);
  5. Yes. It's invisible but still there.

Key Takeaways

  • opacity: 0–1 fades a whole element, children included.
  • Combine with transition for smooth hover fades.
  • For transparent backgrounds only, use RGBA / alpha colours.