CSS opacity Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Visual Effects

What You’ll Learn

The opacity property controls how see-through an element is. It is one of the easiest CSS tools for fades, overlays, and subtle visual effects.

01

Transparency

Values from 0 to 1.

02

Syntax

One simple number.

03

Default 1

Fully visible by default.

04

Fade Effects

Pair with transition.

05

Overlays

Dim backgrounds easily.

06

Whole Element

Affects all content inside.

Introduction

The opacity property in CSS is used to set the transparency level of an element. This property affects the entire element, including its content and background, making it useful for creating effects like overlays, fading elements in and out, and more.

The opacity value ranges from 0 to 1, where 0 represents full transparency and 1 represents full opacity (no transparency).

Definition and Usage

Use opacity when you want the whole box — background, border, text, and children — to become more or less visible together.

For a transparent background only, consider rgba() or hsla() instead so text stays fully readable.

💡
Beginner Tip

Start with opacity: 0.5; on a paragraph to see the effect instantly, then try hover fades with transition: opacity 0.3s;.

📝 Syntax

The syntax for the opacity property is simple. You can apply it to any HTML element.

syntax.css
element {
  opacity: <number>;
}

Here, the value is a number between 0 and 1, indicating the transparency level.

Basic Example

opacity.css
p {
  opacity: 0.5;
}

Syntax Rules

  • Use a number from 0 (invisible) to 1 (fully visible).
  • Values outside 0–1 are clamped by the browser.
  • opacity applies to the entire element, not just the background.
  • It can be animated smoothly with transition or @keyframes.

🎯 Default Value

The default value of the opacity property is 1, which means the element is fully opaque and not transparent at all.

You only need to set opacity when you want something to appear faded or hidden visually.

⚡ Quick Reference

QuestionAnswer
Initial value1
Applies toAll elements
InheritedNo
AnimatableYes
Common useFades, overlays, disabled states, hover effects

💎 Property Values

ValueExampleDescription
0opacity: 0;The element is fully transparent
0.5opacity: 0.5;The element is semi-transparent with 50% opacity
1opacity: 1;The element is fully opaque (default value)
0 to 1opacity: 0.75;Any number in this range sets the transparency level
1 — fully visible 0.75 — slightly faded 0.5 — half transparent 0 — invisible

👀 Live Preview

These boxes use the same blue background with different opacity values:

1
0.75
0.5
0.25

Examples Gallery

From basic semi-transparent text to hover fades and overlay backdrops.

📄 Basic Transparency

Start with a simple paragraph at opacity: 0.5.

Example 1 — Paragraph with 50% Opacity

In this example, we set the opacity of a paragraph to 0.5, making it semi-transparent.

semi-transparent.html
<style>
  p {
    opacity: 0.5;
  }
</style>

<h1>Paragraph with Reduced Opacity</h1>
<p>This paragraph is semi-transparent with 50% opacity.</p>
Try It Yourself

How It Works

The entire paragraph, including its text, becomes 50% transparent.

Example 2 — Hover Fade on a Button

Reduce opacity on hover for a simple interactive effect.

hover-opacity.css
.btn {
  opacity: 1;
  transition: opacity 0.3s ease;
}

.btn:hover {
  opacity: 0.7;
}
Try It Yourself

How It Works

transition makes the opacity change smooth instead of instant.

🎨 Visual Effects

Use low opacity for overlays and animated fades.

Example 3 — Dark Overlay Backdrop

A semi-transparent layer dims content behind a modal or image.

overlay.css
.overlay {
  position: absolute;
  inset: 0;
  background: #0f172a;
  opacity: 0.6;
}
Try It Yourself

How It Works

The overlay covers the full area and dims everything underneath without removing it from the layout.

Example 4 — Fade In with Animation

Animate from invisible to fully visible using keyframes.

fade-in.css
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.card {
  animation: fadeIn 1s ease forwards;
}
Try It Yourself

How It Works

Opacity animations are lightweight and work well for page-load and reveal effects.

opacity vs rgba() and visibility

opacity fades the entire element. rgba() or hsla() can fade only the background color while keeping text at full strength.

opacity: 0 hides an element visually but it can still receive clicks unless you also use pointer-events: none. visibility: hidden also hides content but behaves differently in animations and interaction.

rgba-vs-opacity.css
/* Whole box fades */
.panel-a { opacity: 0.5; }

/* Only background fades */
.panel-b { background: rgba(37, 99, 235, 0.5); }

♿ Accessibility

  • Maintain contrast — Low opacity can make text hard to read. Check contrast ratios for important content.
  • Do not hide essential info — Avoid relying on very faded text for instructions or labels.
  • Respect reduced motion — Some users prefer fewer fade animations when prefers-reduced-motion: reduce is set.
  • Hidden but clickable — Remember that opacity: 0 elements may still block clicks unless disabled.

🧠 How opacity Works

1

You set a value from 0 to 1

The browser calculates how visible the element should be.

Value
2

The whole element is affected

Background, border, text, and child elements all share the same transparency level.

Scope
3

Content behind shows through

Lower opacity lets underlying colors and images appear more clearly.

Blend
=

Smooth transparency control

You get simple, flexible control over element visibility.

Browser Compatibility

The opacity property is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is safe to use in most web projects.

Baseline · Universal support

Excellent support everywhere

opacity has been supported in browsers for many years and works reliably on desktop and mobile.

99% Global browser support
Google Chrome 1+ · All versions
Full support
Mozilla Firefox 1+ · All versions
Full support
Apple Safari 1+ · macOS & iOS
Full support
Microsoft Edge 12+ · All versions
Full support
Opera 9+ · All versions
Full support
opacity property 99% supported

Bottom line: opacity is one of the safest CSS properties to use in production.

Conclusion

The opacity property is a versatile and powerful tool in CSS that allows you to control the transparency of elements on your web page.

Whether you’re creating subtle hover effects, fading elements in and out, or designing overlay components, understanding and utilizing the opacity property can enhance the visual appeal and functionality of your website. Experiment with different values to achieve the desired effect for your web projects.

💡 Best Practices

✅ Do

  • Use opacity for whole-element fades
  • Pair with transition for smooth hover effects
  • Use low opacity for modal backdrops and overlays
  • Test readability when text is semi-transparent
  • Prefer rgba() when only the background should fade

❌ Don’t

  • Confuse opacity: 0 with removing an element from layout
  • Assume invisible elements cannot receive clicks
  • Make important text too faint to read
  • Overuse heavy fade animations on every element
  • Forget reduced-motion preferences for long fades

Key Takeaways

Knowledge Unlocked

Five things to remember about opacity

Use these points when adding transparency to your layouts.

5
Core concepts
02

Default 1

Fully opaque.

Default
📝 03

Whole Element

Affects all content.

Scope
🎨 04

Fades & Overlays

Common use cases.

Use case
💻 05

Universal Support

Works everywhere.

Support

❓ Frequently Asked Questions

opacity sets how transparent an element is. A value of 1 is fully visible, 0 is fully transparent, and values in between create semi-transparent effects.
The default value is 1, which means the element is fully opaque with no transparency.
Yes. opacity applies to the entire element, including its background, border, text, and all child content.
opacity makes the whole element transparent, while rgba() or hsla() can make only the background color transparent and leave text fully opaque.
Yes. opacity is one of the most common properties used for fade-in and fade-out transitions and animations.

Practice in the Live Editor

Open the HTML editor and experiment with opacity values, hover fades, and overlay effects.

HTML Editor →

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.

5 people found this page helpful