Loading
The opacity property in CSS controls how transparent or opaque an element is. It is commonly used to create visual effects such as fading elements, overlay backgrounds, or highlighting.



What is Opacity in CSS ?

The opacity property defines the transparency level of an element. It accepts values from 0 (fully transparent) to 1 (fully visible).



Syntax:

selector {
  opacity: value;
}

Value:
 A number between 0.0 (transparent) to 1.0 (opaque)



Key Point

  • Opacity affects both the element and its children.
  • To avoid child elements becoming transparent, use rgba() color values instead of opacity if you only want to make the background transparent.
  • Older browsers like IE8 or earlier require the filter: alpha(opacity=...) fallback.



Example:

<!DOCTYPE html>
<html>

<head>
  <style>
    img.trans {
      opacity: 0.4;
      filter: alpha(opacity=40);
      /* IE8 and earlier */
    }
  </style>
</head>

<body>
  <p>Normal Image</p>
  <img src="rose.jpg" alt="normal rose">

  <p>Transparent Image</p>
  <img class="trans" src="rose.jpg" alt="transparent rose">
</body>

</html>

Output:

Uploaded Image




Property Value

ValueDescription
0.0Fully transparent ( invisible )
1.0Fully opaque ( default )
0.1 - 0.9Various transparency levels
initialResets to the default (1.0)
inheritInherits from the parent element



Alternative: RGBA for Transparent Background

If you only want a transparent background (not the text or children), use rgba():

div {
  background-color: rgba(0, 0, 0, 0.5); /* 50% transparent background */
}

This way, child elements remain fully visible.