Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

opacity — element opacity

The opacity property sets the opacity of an entire element, including its descendants. A value of 0 means fully transparent, while 1 means fully opaque.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

opacity: <number> | <percentage>;

opacity accepts a number or percentage: 0.5 equals 50%. Its initial value is 1; it is not inherited. Values outside 0–1 are valid but are clamped to that range when computed.

The element and its descendants are rendered as a group whose opacity is then applied. Consequently, opacity: 1 on a child cannot undo its parent's effect. To change only background transparency, use a color with an alpha channel instead of opacity.

A value below 1 creates a stacking context. opacity: 0 does not disable hit testing: an invisible element can still intercept clicks.

Examples

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Opacity on an element and its child</title>
<style>
  body { background: white; color: black; }
  .sample { padding: 0.5rem; font: bold 2rem sans-serif; }
  #faded { opacity: 0.5; }
  .sample strong { opacity: 1; }
  #reference { opacity: 1; }
</style>
</head>
<body>
<h1>Parent opacity and child text</h1>
<figure>
  <figcaption>Parent at half opacity</figcaption>
  <div class="sample" id="faded">Parent <strong>Child</strong></div>
</figure>
<figure>
  <figcaption>Parent at full opacity</figcaption>
  <div class="sample" id="reference">Parent <strong>Child</strong></div>
</figure>
<script>
  const faded = document.getElementById('faded');
  const reference = document.getElementById('reference');
  console.log(`Faded parent: ${getComputedStyle(faded).opacity}`);
  console.log(`Faded parent's child: ${getComputedStyle(faded.querySelector('strong')).opacity}`);
  console.log(`Reference parent: ${getComputedStyle(reference).opacity}`);
</script>
</body>
</html>

Result

Faded parent: 0.5
Faded parent's child: 1
Reference parent: 1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources