Tillitsdone
down Scroll to discover
CSS Opacity A Comprehensive Guide
CSS opacity is a fundamental property for controlling the transparency of elements.

It applies to the entire element and its contents, offering options like numbers, percentages, and global values.
thumbnail

Introduction

The CSS opacity property is a powerful tool for web developers. It helps control the transparency of an element, making it fully transparent, translucent, or fully opaque. This property is widely supported across devices and browsers. By using opacity, you can enhance the visual appeal of your web pages.

Specification

The opacity property is defined in the CSS Color Module Level 4 specification. For detailed information, you can refer to the official specification:

  • [CSS Color Module Level 4 - Transparency]WebsiteUrl

Description

The opacity property controls how transparent an element is. A value of 0 makes the element fully transparent, while 1 makes it fully opaque. You can also use percentages, where 0% is fully transparent and 100% is fully opaque.

When you set opacity, it applies to the entire element, including its contents. However, it does not affect child elements individually. To change the opacity of just the background, use the background property with a color value that includes an alpha channel.

Syntax

The opacity property is easy to use. Here is the basic syntax:

opacity: value;

You can set the opacity property using various values:

  • A number between 0 and 1 (e.g., 0.9).
  • A percentage between 0% and 100% (e.g., 90%).
  • Global values such as inherit, initial, revert, revert-layer, and unset.

Example:

opacity: 0.9;
opacity: 90%;
/* Global values */
opacity: inherit;
opacity: initial;
opacity: revert;
opacity: revert-layer;
opacity: unset;

Values

The opacity property accepts several types of values:

  • <alpha-value>: A <number> in the range 0.0 to 1.0, or a <percentage> in the range 0% to 100%. Any value outside this range will be clipped to the nearest limit.
ValueMeaning
0The element is fully transparent (invisible).
Any <number> strictly between 0 and 1The element is translucent (content behind the element can be seen).
1 (default value)The element is fully opaque (visually solid).

Examples

Here are practical examples to illustrate how the opacity property can be used in CSS.

Setting Opacity

This example shows how the opacity property changes the opacity of the entire element and its content.

HTML
<div class="light">You can barely see this.</div>
<div class="medium">This is easier to see.</div>
<div class="heavy">This is very easy to see.</div>
CSS
div {
background-color: yellow;
font-weight: bold;
font-size: 130%;
}
.light {
opacity: 0.2; /* Barely see the text over the background */
}
.medium {
opacity: 0.5; /* See the text more clearly over the background */
}
.heavy {
opacity: 0.9; /* See the text very clearly over the background */
}

Setting Opacity on Hover

In this example, the opacity is changed on hover, so the striped background image on the parent element shows through the image.

HTML
<div class="wrapper">
<img
src="//interactive-examples.mdn.mozilla.net/media/dino.svg"
alt="MDN Dino"
width="128"
height="146"
class="opacity" />
</div>
CSS
img.opacity {
opacity: 1;
}
img.opacity:hover {
opacity: 0.5;
}
.wrapper {
width: 200px;
height: 160px;
background-color: #f03cc3;
background-image: linear-gradient(
90deg,
transparent 50%,
rgb(255 255 255 / 50%) 50%
);
background-size: 20px 20px;
}

Styling Based on User Preferences

To style elements based on the user’s operating system transparency preferences, use the prefers-reduced-transparency media query.

CSS
.element {
opacity: 0.5;
}
@media (prefers-reduced-transparency) {
.element {
opacity: 1;
}
}

Transitioning Opacity

Transitioning the opacity of elements can create smooth and visually appealing animations. This is particularly useful when you want to gradually show or hide elements, enhancing the user experience.

CSS
.card {
transition:
opacity 5s,
display 5s;
background-color: orange;
transition-behavior: allow-discrete;
@starting-style {
opacity: 0;
}
}
.card.hidden {
display: none;
opacity: 0;
}

Accessibility

When using the opacity property, it’s crucial to consider accessibility to ensure that your web content is usable by everyone, including those with visual impairments. Here are some key points to keep in mind:

Text Contrast

If you adjust the opacity of text, make sure that the contrast ratio between the text color and the background color remains high enough for people with low vision conditions to read the content. According to the Web Content Accessibility Guidelines (WCAG), a contrast ratio of at least 4.5:1 is required for text content, and 3:1 for larger text such as headings.

  • Large Text: Defined as 18.66px and bold, or 24px and larger.

User Preferences

Various operating systems provide a preference for reducing transparency. To respect these preferences, use the prefers-reduced-transparency media query. This allows you to set the opacity based on the user’s operating system transparency preferences.

Example

.element {
opacity: 0.5;
}
@media (prefers-reduced-transparency) {
.element {
opacity: 1;
}
}

Hidden Elements

Setting the opacity value to 0 makes elements and their children invisible but keeps them in the DOM. They still respond to pointer events and can receive focus if they’re in the tabbing order. For good usability, make these elements visible when they receive user interactions, or use the pointer-events property to disable pointer events. You can also take the element out of the tab order by using the disabled attribute or setting tabindex="-1" for non-form-related interactive elements.

Screen Readers

Don’t use opacity alone to provide information to screen readers. Instead, use the hidden attribute, visibility, or display properties. If an element is hidden with opacity, also hide it from screen readers using the aria-hidden attribute to ensure accessibility.

Tools and Resources

  • [WebAIM: Color Contrast Checker]WebsiteUrl
  • [MDN Understanding WCAG, Guideline 1.4 explanations]WebsiteUrl
  • [Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0]WebsiteUrl

Formal Definition

The opacity property in CSS has specific characteristics for consistent behavior:

PropertyValue
Initial Value1
Applies ToAll elements
InheritedNo
PercentagesMap to the range [0,1]
Computed ValueSame as the specified value after clipping the <number> to the range [0.0, 1.0]
Animation TypeBy computed value type

Formal Syntax

The formal syntax of the opacity property is:

opacity = <opacity-value>
<opacity-value> = <number> | <percentage>
  • <number>: A number between 0 and 1, inclusive.
  • <percentage>: A percentage between 0% and 100%, inclusive.

Browser Compatibility

The opacity property is widely supported across modern browsers:

  • Chrome: Since version 4.0 (January 2010).
  • Firefox: Since version 2.0 (January 2010).
  • IE/Edge: Since version 9.0 (March 2011).
  • Opera: Since version 9.0 (June 2006).
  • Safari: Since version 3.1 (March 2008).

For detailed compatibility information, refer to the BCD (Browser Compatibility Data) tables.

See Also

For further reading and related topics, check out:

  • [prefers-reduced-transparency]WebsiteUrl media query
  • [CSS color]WebsiteUrl module

Syntax

The opacity property in CSS is straightforward:

opacity: value;

You can set the opacity property using various values:

  • A number between 0 and 1 (e.g., 0.9).
  • A percentage between 0% and 100% (e.g., 90%).
  • Global values such as inherit, initial, revert, revert-layer, and unset.

Example usage:

opacity: 0.9;
opacity: 90%;
/* Global values */
opacity: inherit;
opacity: initial;
opacity: revert;
opacity: revert-layer;
opacity: unset;

Values

The opacity property accepts several types of values:

  • <alpha-value>: A <number> in the range 0.0 to 1.0, or a <percentage> in the range 0% to 100%. Any value outside this range will be clipped to the nearest limit.
ValueMeaning
0The element is fully transparent (invisible).
Any <number> strictly between 0 and 1The element is translucent (content behind the element can be seen).
1 (default value)The element is fully opaque (visually solid).

Examples

Here are practical examples of using the opacity property.

Setting Opacity

This example shows how the opacity property affects the entire element and its content:

HTML
<div class="light">You can barely see this.</div>
<div class="medium">This is easier to see.</div>
<div class="heavy">This is very easy to see.</div>
CSS
div {
background-color: yellow;
font-weight: bold;
font-size: 130%;
}
.light {
opacity: 0.2; /* Barely see the text over the background */
}
.medium {
opacity: 0.5; /* See the text more clearly over the background */
}
.heavy {
opacity: 0.9; /* See the text very clearly over the background */
}

Setting Opacity on Hover

This example changes the opacity of an image on hover:

HTML
<div class="wrapper">
<img
src="//interactive-examples.mdn.mozilla.net/media/dino.svg"
alt="MDN Dino"
width="128"
height="146"
class="opacity" />
</div>
CSS
img.opacity {
opacity: 1;
}
img.opacity:hover {
opacity: 0.5;
}
.wrapper {
width: 200px;
height: 160px;
background-color: #f03cc3;
background-image: linear-gradient(
90deg,
transparent 50%,
rgb(255 255 255 / 50%) 50%
);
background-size: 20px 20px;
}

Styling Based on User Preferences

Use the prefers-reduced-transparency media query to adjust opacity based on user preferences:

CSS
.element {
opacity: 0.5;
}
@media (prefers-reduced-transparency) {
.element {
opacity: 1;
}
}

Transitioning Opacity

Create smooth transitions for opacity changes:

CSS
.card {
transition: opacity 5s;
background-color: orange;
}
.card.hidden {
opacity: 0;
}

These examples show how to use the opacity property to enhance the visual appeal and interactivity of your web designs.

Accessibility

When using the opacity property, it’s crucial to consider accessibility to ensure that your web content is usable by everyone, including those with visual impairments. Here are some key points to keep in mind:

Text Contrast

If you adjust the opacity of text, make sure that the contrast ratio between the text color and the background color remains high enough for people with low vision conditions to read the content. According to the Web Content Accessibility Guidelines (WCAG), a contrast ratio of at least 4.5:1 is required for text content, and 3:1 for larger text such as headings.

  • Large Text: Defined as 18.66px and bold, or 24px and larger.

User Preferences

Various operating systems provide a preference for reducing transparency. To respect these preferences, use the prefers-reduced-transparency media query. This allows you to set the opacity based on the user’s operating system transparency preferences.

Example

.element {
opacity: 0.5;
}
@media (prefers-reduced-transparency) {
.element {
opacity: 1;
}
}

Hidden Elements

Setting the opacity value to 0 makes elements and their children invisible but keeps them in the DOM. They still respond to pointer events and can receive focus if they’re in the tabbing order. For good usability, make these elements visible when they receive user interactions, or use the pointer-events property to disable pointer events. You can also take the element out of the tab order by using the disabled attribute or setting tabindex="-1" for non-form-related interactive elements.

Screen Readers

Don’t use opacity alone to provide information to screen readers. Instead, use the hidden attribute, visibility, or display properties. If an element is hidden with opacity, also hide it from screen readers using the aria-hidden attribute to ensure accessibility.

Tools and Resources

  • [WebAIM: Color Contrast Checker]WebsiteUrl
  • [MDN Understanding WCAG, Guideline 1.4 explanations]WebsiteUrl
  • [Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0]WebsiteUrl

Formal Definition

The opacity property in CSS has specific characteristics for consistent behavior:

PropertyValue
Initial Value1
Applies ToAll elements
InheritedNo
PercentagesMap to the range [0,1]
Computed ValueSame as the specified value after clipping the <number> to the range [0.0, 1.0]
Animation TypeBy computed value type

Formal Syntax

The formal syntax of the opacity property is:

opacity = <opacity-value>
<opacity-value> = <number> | <percentage>
  • <number>: A number between 0 and 1, inclusive.
  • <percentage>: A percentage between 0% and 100%, inclusive.

Browser Compatibility

The opacity property is widely supported across modern browsers:

  • Chrome: Since version 4.0.
  • Firefox: Since version 2.0.
  • IE/Edge: Since version 9.0.
  • Opera: Since version 9.0.
  • Safari: Since version 3.1.

See Also

For further reading and related topics, check out:

  • [prefers-reduced-transparency]WebsiteUrl media query
  • [CSS color]WebsiteUrl module

Examples

Here are practical examples of using the opacity property.

Setting Opacity

This example shows how the opacity property affects the entire element and its content:

HTML
<div class="light">You can barely see this.</div>
<div class="medium">This is easier to see.</div>
<div class="heavy">This is very easy to see.</div>
CSS
div {
background-color: yellow;
font-weight: bold;
font-size: 130%;
}
.light {
opacity: 0.2; /* Barely see the text over the background */
}
.medium {
opacity: 0.5; /* See the text more clearly over the background */
}
.heavy {
opacity: 0.9; /* See the text very clearly over the background */
}

Setting Opacity on Hover

This example changes the opacity of an image on hover:

HTML
<div class="wrapper">
<img
src="//interactive-examples.mdn.mozilla.net/media/dino.svg"
alt="MDN Dino"
width="128"
height="146"
class="opacity" />
</div>
CSS
img.opacity {
opacity: 1;
}
img.opacity:hover {
opacity: 0.5;
}
.wrapper {
width: 200px;
height: 160px;
background-color: #f03cc3;
background-image: linear-gradient(
90deg,
transparent 50%,
rgb(255 255 255 / 50%) 50%
);
background-size: 20px 20px;
}

Styling Based on User Preferences

Use the prefers-reduced-transparency media query to adjust opacity based on user preferences:

CSS
.element {
opacity: 0.5;
}
@media (prefers-reduced-transparency) {
.element {
opacity: 1;
}
}

Transitioning Opacity

Create smooth transitions for opacity changes:

CSS
.card {
transition: opacity 5s;
background-color: orange;
}
.card.hidden {
opacity: 0;
}

These examples show how to use the opacity property to enhance the visual appeal and interactivity of your web designs.

Accessibility

When using the opacity property, it’s crucial to consider accessibility to ensure that your web content is usable by everyone, including those with visual impairments. Here are some key points to keep in mind:

Text Contrast

If you adjust the opacity of text, make sure that the contrast ratio between the text color and the background color remains high enough for people with low vision conditions to read the content. According to the Web Content Accessibility Guidelines (WCAG), a contrast ratio of at least 4.5:1 is required for text content, and 3:1 for larger text such as headings.

  • Large Text: Defined as 18.66px and bold, or 24px and larger.

User Preferences

Various operating systems provide a preference for reducing transparency. To respect these preferences, use the prefers-reduced-transparency media query. This allows you to set the opacity based on the user’s operating system transparency preferences.

Example

.element {
opacity: 0.5;
}
@media (prefers-reduced-transparency) {
.element {
opacity: 1;
}
}

Hidden Elements

Setting the opacity value to 0 makes elements and their children invisible but keeps them in the DOM. They still respond to pointer events and can receive focus if they’re in the tabbing order. For good usability, make these elements visible when they receive user interactions, or use the pointer-events property to disable pointer events. You can also take the element out of the tab order by using the disabled attribute or setting tabindex="-1" for non-form-related interactive elements.

Screen Readers

Don’t use opacity alone to provide information to screen readers. Instead, use the hidden attribute, visibility, or display properties. If an element is hidden with opacity, also hide it from screen readers using the aria-hidden attribute to ensure accessibility.

Tools and Resources

  • [WebAIM: Color Contrast Checker]WebsiteUrl
  • [MDN Understanding WCAG, Guideline 1.4 explanations]WebsiteUrl
  • [Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0]WebsiteUrl

Formal Definition

The opacity property in CSS has specific characteristics for consistent behavior:

PropertyValue
Initial Value1
Applies ToAll elements
InheritedNo
PercentagesMap to the range [0,1]
Computed ValueSame as the specified value after clipping the <number> to the range [0.0, 1.0]
Animation TypeBy computed value type

Formal Syntax

The formal syntax of the opacity property is:

opacity = <opacity-value>
<opacity-value> = <number> | <percentage>
  • <number>: A number between 0 and 1, inclusive.
  • <percentage>: A percentage between 0% and 100%, inclusive.

Browser Compatibility

The opacity property is widely supported across modern browsers:

  • Chrome: Since version 4.0.
  • Firefox: Since version 2.0.
  • IE/Edge: Since version 9.0.
  • Opera: Since version 9.0.
  • Safari: Since version 3.1.

See Also

For further reading and related topics, check out:

  • [prefers-reduced-transparency]WebsiteUrl media query
  • [CSS color]WebsiteUrl module
Get in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.