CSS opacity Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The opacity property sets how opaque an element is — the opposite of transparency. A value of 1 is fully solid. A value of 0 is fully invisible. Values in between let content behind the element show through.

It applies to the whole element: background, border, text, and children. For a transparent background only, use a color with an alpha channel instead, such as on background-color.

Baseline · Widely available.

Not deprecated and not experimental. Number values (0–1) are universal. Percentage values (0%–100%) need modern browsers (Chrome 78+, Firefox 70+, Safari 13.1+).

CSS Syntax

syntax.css
opacity: <number> | <percentage> | initial | inherit;

Example: opacity: 0.5; or opacity: 50%;. Out-of-range values are clamped to 0–1.

Property Values

ValueDescriptionTry it
<number>0 invisible, 1 fully opaque. Default is 1Try it
<percentage>0% to 100%. Same as 0 to 1Try it
0 / 0%Fully transparent. Still in the layout and can receive clicksTry it
initialSets this property to its default value, 1Try it
inheritUses the parent’s computed opacity valueTry it

How to Use opacity

Pick a target, set a value between invisible and solid, then animate only if you need a fade.

  1. Choose the element that should fade as a whole.
  2. Set opacity to a number (0–1) or percentage (0%–100%).
  3. For hover fades, add transition: opacity ….
  4. If only the background should fade, use an alpha color on background-color instead.
PieceRequired?Role
Target elementYesAny element. Opacity is not inherited
Number or percentageYesControls how see-through the whole layer is
transitionOptionalSmooths changes for hover and fade effects
Alpha colorAlternativeFades background only; keeps text solid

Live Preview

The blue box sits on a checkered stage. Change opacity and watch the pattern show through.

Lower values show more of the checkered background. 0 hides the box visually but keeps its space.

Sample

opacity: 0.5

CSS Examples

Each snippet focuses on opacity. Extra styles are kept minimal for study. Use Try It Yourself for a live editor.

Example 1 — Semi-Transparent Box

Fade a whole box to half strength so the pattern behind it shows through.

CSS
<style>
  section {
    background: #e2e8f0;
    padding: 16px;
  }
  div {
    opacity: 0.5;
    background: #2563eb;
    color: #fff;
    padding: 16px;
  }
</style>

<section>
  <div>Half opaque</div>
</section>
Try It Yourself

How It Works

  1. Give a solid fill. The blue background makes the fade easy to see.
  2. Set opacity: 0.5. The whole box — fill and text — becomes 50% opaque.
  3. Gray parent shows through. The section behind is still there; you see it through the box.
  4. Children share the fade. You cannot make the text fully opaque while the box stays at 0.5.

Example 2 — Hover Fade

Lower opacity on hover and smooth the change with transition.

CSS
<style>
  button {
    opacity: 1;
    transition: opacity 0.3s ease;
    background: #2563eb;
    color: #fff;
    border: 0;
    padding: 12px 16px;
  }
  button:hover {
    opacity: 0.7;
  }
</style>

<button type="button">Hover me</button>
Try It Yourself

How It Works

  1. Start fully solid. opacity: 1 is the default look.
  2. Add a transition. transition: opacity 0.3s ease animates only opacity.
  3. Change on hover. opacity: 0.7 softens the button while the pointer is over it.
  4. Keep contrast readable. Do not drop important text too far toward 0.

Example 3 — Dim Overlay

Cover content with a dark layer at opacity: 0.6.

CSS
<style>
  section {
    position: relative;
    background: #93c5fd;
    padding: 24px;
  }
  .overlay {
    position: absolute;
    inset: 0;
    background: #0f172a;
    opacity: 0.6;
  }
  p {
    position: relative;
    color: #fff;
    margin: 0;
  }
</style>

<section>
  <div class="overlay"></div>
  <p>Content over the dim layer</p>
</section>
Try It Yourself

How It Works

  1. Make a positioned parent. position: relative anchors the overlay.
  2. Stretch the layer. position: absolute; inset: 0 covers the section.
  3. Dim with opacity. A solid dark fill at 0.6 darkens what is behind it.
  4. Raise the text. The paragraph stays position: relative so it paints above the overlay.

Edge Cases & Pitfalls

If transparency looks wrong, one of these is usually why.

scope

The whole element fades

Text, borders, and children all share the same opacity. To fade only the fill, use an alpha color on background-color.

clicks

opacity: 0 can still be clicked

Invisible elements stay in the DOM and can receive pointer and focus events. Pair with pointer-events: none when needed.

stack

Values other than 1 create a stacking context

That can change how overlapping layers paint. Keep z-index plans simple when you fade cards or modals.

a11y

Low opacity hurts contrast

Faded text must still meet WCAG contrast. Do not hide meaning with opacity alone — prefer visibility or display for true hiding.

prefs

Respect reduced transparency

Some users prefer less see-through UI. Use prefers-reduced-transparency to raise opacity when that preference is set.

Browser Support

opacity is a browser CSS property for visual transparency. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

opacity

Number values: Chrome 1+, Firefox 1+, Safari 1.2+, Edge 12+, IE 9+. Percentages: Chrome 78+, Firefox 70+, Safari 13.1+, Edge 79+.

99% Number value support
Google Chrome 1+ number · 78+ %
Full support
Mozilla Firefox 1+ number · 70+ %
Full support
Apple Safari 1.2+ number · 13.1+ %
Full support
Microsoft Edge 12+ number · 79+ %
Full support
Opera 9+ number · 65+ %
Full support
Samsung Internet Supported
Full support
Internet Explorer 9+ number · No %
Partial
opacity Widely supported

Bottom line: Safe for production with number values. Prefer 0.5 over 50% if you must support older browsers. This is not a JavaScript runtime API.

Key Takeaways

  • Role: set how opaque the whole element is, from invisible (0) to solid (1).
  • Initial: 1. Not inherited. Children still look faded with the parent.
  • Background only? Use an alpha color instead of opacity.
  • Invisible ≠ gone: opacity: 0 can still receive clicks and keep layout space.

One line: opacity fades the entire painted layer — use alpha colors when only the fill should be see-through.

Frequently Asked Questions

It sets how opaque an element is. 1 (or 100%) is fully solid. 0 (or 0%) is fully transparent. Values in between make the whole element see-through.
Yes. Opacity applies to the element as a whole, including background, border, text, and children. Children cannot look more opaque than their parent.
opacity fades the entire element. A color with an alpha channel, such as rgb(0 0 0 / 40%), can fade only the background while text stays fully opaque.
No. It is invisible but still in the layout and can still receive clicks and focus. Use pointer-events, visibility, or display when you need different hide behavior.
No. The initial value is 1. Children are painted with the parent’s transparency because the whole layer is composited together.
Number values are Baseline Widely available in all modern browsers, including Internet Explorer 9+. Percentage values need Chrome 78+, Firefox 70+, Safari 13.1+, or Edge 79+. It is a browser CSS property, not a Node.js feature.

Next: order

Change the visual order of flex and grid items without rewriting HTML.

order tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful