TExcelTAD
Home › CSS › CSS Opacity

CSS Opacity

The opacity property makes an element see-through. It takes a number from 0 (fully transparent) to 1 (fully opaque), or a percentage from 0% to 100%.

Basic opacity

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; background: repeating-linear-gradient(45deg, #fff 0 10px, #e0e0e0 10px 20px); }
  .row { display: flex; gap: 10px; padding: 10px; }
  .row div { width: 80px; height: 80px; background: #6200ea; color: white; display: grid; place-items: center; font-weight: bold; border-radius: 10px; }
</style>
</head>
<body>
  <div class="row">
    <div style="opacity: 1">1</div>
    <div style="opacity: .75">0.75</div>
    <div style="opacity: .5">0.5</div>
    <div style="opacity: .25">0.25</div>
    <div style="opacity: .1">0.1</div>
  </div>
</body>
</html>

The striped background shows through more and more as opacity drops.

Opacity affects children too

opacity applies to the whole element including all its children and text. A child can't be more opaque than its parent — opacity: 1 on a child doesn't undo it. If you only want a see-through background, use a colour with an alpha channel instead.

Technique Fades background Fades text & children
opacity: .5 Yes Yes
background: rgb(0 0 0 / .5) Yes No
background: #00000080 (8-digit hex) Yes No
color: rgb(0 0 0 / .5) No Text only
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { margin: 0; font-family: system-ui, sans-serif; }
  .scene { height: 260px; display: flex; gap: 14px; align-items: center; justify-content: center;
           background: linear-gradient(135deg, #ff9800, #e91e63, #3f51b5); }
  .panel { width: 150px; padding: 14px; color: white; font-weight: bold; border-radius: 10px; }
  .op { background: black; opacity: .5; }
  .rgba { background: rgb(0 0 0 / .5); }
</style>
</head>
<body>
  <div class="scene">
    <div class="panel op">opacity: .5 — my text faded too</div>
    <div class="panel rgba">rgb(0 0 0 / .5) — crisp text</div>
  </div>
</body>
</html>

Hover effects

Opacity combines nicely with transition for fade effects — dimming other items, revealing captions, or showing that something is clickable.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .gallery { display: flex; gap: 8px; }
  .gallery div { width: 90px; height: 90px; border-radius: 10px; transition: opacity .3s; }
  .gallery:hover div { opacity: .35; }
  .gallery div:hover { opacity: 1; }
  .a { background: linear-gradient(#ff8a65, #d84315); }
  .b { background: linear-gradient(#81c784, #2e7d32); }
  .c { background: linear-gradient(#64b5f6, #1565c0); }
  .d { background: linear-gradient(#ba68c8, #6a1b9a); }
</style>
</head>
<body>
  <p>Hover a tile — the others dim:</p>
  <div class="gallery"><div class="a"></div><div class="b"></div><div class="c"></div><div class="d"></div></div>
</body>
</html>

Text over images

A classic pattern: a caption bar with a translucent background sitting over a picture.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .photo { position: relative; width: 280px; height: 180px; border-radius: 12px; overflow: hidden;
           background: radial-gradient(circle at 70% 30%, #fff59d 0 30px, transparent 31px), linear-gradient(#4fc3f7, #0277bd 60%, #2e7d32 60%); }
  .caption { position: absolute; left: 0; right: 0; bottom: 0; padding: 10px; color: white;
             background: rgb(0 0 0 / .55); transform: translateY(100%); transition: transform .3s; }
  .photo:hover .caption { transform: translateY(0); }
</style>
</head>
<body>
  <div class="photo"><div class="caption">Sunny meadow — hover to reveal me</div></div>
</body>
</html>

Tip: opacity: 0 hides an element visually but it still takes up space and can still receive clicks. Add pointer-events: none or visibility: hidden if it should be fully inert.

Note: Any opacity below 1 creates a new stacking context, which can change how z-index behaves for its children.

Test yourself

What range of values does opacity accept?

  1. 0 to 100
  2. 0 to 1 (or 0% to 100%)
  3. -1 to 1
  4. 1 to 10

How do you make only a box's background semi-transparent while keeping its text solid?

  1. opacity: .5 on the box
  2. opacity: 1 on the text
  3. Use a background colour with an alpha channel
  4. Use visibility: half

An element with opacity: 0…

  1. Is removed from layout
  2. Is invisible but still takes space and can be clicked
  3. Cannot be transitioned
  4. Is the same as display: none