CSS `opacity` Property: Controlling Element Transparency

Learn how to use the CSS `opacity` property to control the transparency of HTML elements. This tutorial explains its functionality, demonstrates its usage with various values (0.0 to 1.0), and highlights its application in creating visually appealing and dynamic web page designs.



Understanding CSS `opacity`

The CSS `opacity` property controls the transparency of an HTML element. A lower opacity value means a more transparent element; a higher value means a less transparent (more opaque) element.

How Opacity Works

Opacity is applied uniformly across the entire element. The value is a number between 0.0 (completely transparent) and 1.0 (completely opaque). Opacity is *not* inherited by child elements.

Applying Opacity

You apply opacity directly to an element using the `opacity` property. For example:


.my-element {
  opacity: 0.5; /* 50% transparent */
}

Example: Transparent Image

This example shows a normal image and the same image with opacity applied. (The "Test it Now" and "Output" sections are placeholders and would be replaced by actual image comparisons.)

Test it Now

Browser Compatibility Notes

  • Modern Browsers (Chrome, Firefox, Opera, Safari, IE9+): Use the `opacity` property directly with values from 0.0 to 1.0. Lower values mean greater transparency.
  • Older IE Versions: Use the `filter: alpha(opacity=x)` property, where `x` ranges from 0 to 100. Lower values mean greater transparency.