CSS Portal

CSS opacity Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The CSS property opacity controls how transparent an element appears when it is painted to the page. Rather than changing layout or the element’s box model, it changes the element’s final composited alpha so the element and its descendants are blended with whatever is behind them. Because it affects the rendered output, using opacity is a straightforward way to create fade effects and to make content visually recede without removing it from the document flow.

When you apply opacity to an element, that visual transparency applies to the element’s children and pseudo‑elements as well — the browser composites that subtree together and then blends the result. Changing opacity can also affect painting and stacking behavior: in many cases an element with reduced opacity will create a new stacking context, which changes how z-ordering is resolved; see z-index for related stacking considerations. Because the compositing step is separate from layout, you’ll often see GPU acceleration used for animating opacity, producing smoother visual transitions.

It’s important to distinguish visual transparency from interactivity and accessibility. An element made invisible by opacity still occupies its layout space and remains in the accessibility tree and in hit testing — clicks, focus, and keyboard events will still target it unless you change pointer behavior or visibility. To alter whether an element can receive pointer events, use pointer-events; to remove it from the rendering but keep layout space, see visibility; and to remove it entirely from layout and the accessibility tree, use display.

For common patterns, opacity is often combined with transitions or animations to create fades — these are typically driven by the transition or animation system. If you need only a translucent background without dimming children, prefer using semi‑transparent colors on backgrounds (for example via background-color) or an overlay pseudo‑element so child content remains fully opaque. For more complex visual effects (blur, hue shifts, brightness), consider the filter property as an alternative or complement to opacity-based approaches.

Definition

Initial value
1
Applies to
All elements
Inherited
No
Computed value
The same as the specified value after clipping the [alphavalue] to the range [0.0,1.0]
Animatable
No
JavaScript syntax
object.style.opacity

Interactive Demo

The rusty swing set creaked a lonely lullaby in the twilight, shadows lengthening like grasping fingers across the dew-kissed grass. A lone dandelion seed, adrift on the breeze, whispered secrets of faraway fields, of dandelion suns and galaxies spun from fluff.

Syntax

opacity: <alphavalue> | inherit

Values

  • <alphavalue>A floating-point value between 0.0 (fully transparent) and 1.0 (fully opaque), inclusive. Any values outside the range will be clamped to this range.
  • inherit

Example

<div class='container'>
<h1>CSS opacity - demo</h1>

<div class='row'>
<div class='box full'>
<span class='label'>opacity: 1 (default)</span>
</div>

<div class='box semi'>
<span class='label'>opacity: 0.6</span>
</div>

<div class='box low'>
<span class='label'>opacity: 0.2</span>
</div>
</div>

<section class='note'>
<h2>Opacity affects children</h2>
<div class='parent'>
<p>Parent element with opacity: 0.7</p>
<button class='btn'>Child button</button>
</div>
</section>
</div>
/* Basic reset and layout */
* {
  box-sizing: border-box;
}

body {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
  background: #f4f6f8;
  padding: 24px;
  color: #222;
}

/* Container */
.container {
  max-width: 800px;
  margin: 0 auto;
}

/* Row of example boxes */
.row {
  display: flex;
  gap: 16px;
  margin: 16px 0;
}

.box {
  flex: 1;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  color: white;
  font-weight: 600;
}

.box.full {
  background: linear-gradient(135deg, #667eea, #764ba2);
  opacity: 1; /* fully opaque */
}

.box.semi {
  background: linear-gradient(135deg, #f6d365, #fda085);
  opacity: 0.6; /* semi-transparent */
  transition: opacity 200ms ease;
  cursor: pointer;
}

.box.semi:hover {
  opacity: 1; /* hover to show change */
}

.box.low {
  background: linear-gradient(135deg, #89f7fe, #66a6ff);
  opacity: 0.2; /* mostly transparent */
}

.label {
  background: rgba(0, 0, 0, 0.18);
  padding: 6px 10px;
  border-radius: 4px;
}

/* Example showing that opacity affects children */
.note {
  margin-top: 24px;
}

.parent {
  background: #2b2d42;
  color: white;
  padding: 16px;
  border-radius: 8px;
  opacity: 0.7; /* both this element and its children are affected */
  display: flex;
  gap: 12px;
  align-items: center;
}

.btn {
  padding: 8px 12px;
  border-radius: 6px;
  border: none;
  background: #ef233c;
  color: white;
  cursor: pointer;
}

Browser Support

The following information will show you the current browser support for the CSS opacity property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!