Opacity & Blur — UI/UX

opacity (how see-through something is) makes the whole element and its children (the elements inside it) see-through. Alpha inside a colour, the last number…

The header (the top part of the page) in many apps looks like glass. As you scroll, the photos underneath pass faintly behind the title, but the title can still be read. That effect is made of two things, opacity and blur. If you don't measure, both can easily make text disappear.

Opacity is how see-through an element is: 1 is solid, 0 is not visible at all. Blur makes things soft, so shapes stop being sharp. Picture the bathroom window at Mrs. Sri's homestay, with its frosted glass. From inside you can still tell there's blue sky and green trees outside, but you can't see their shapes. The frosted glass works like blur. If the glass got a thicker milky-white coat, the colours from outside would fade and less would get through. The thickness of that coat is like opacity. CSS has both. Both change the colour the eye finally sees, so the contrast ratio must be calculated again.

Opacity: the whole element goes see-through

These three lines of CSS use two ways to make something see-through:

.note   { opacity: 0.6; }                           /* text, icons, borders, all at 60% */
.top    { background: rgba(255, 255, 255, 0.7); }   /* only the background at 70% */
.dialog-backdrop { background: rgba(0, 0, 0, 0.5); } /* the dark layer behind a dialog */

Opacity or a lighter colour

Mrs. Sri's booking page has the note "Free cancellation up to 3 days before check-in." The note is written in the main text colour #1f2937 (14.68:1 on white). Then it gets opacity: 0.6 so it looks like secondary text. Does it still pass 4.5:1? Guess first, then check the first row of the table below.

Opacity mixes the element's colour with what's behind it. This table calculates that note and the Terrace Room's price on several backgrounds with the WCAG contrast formula. For the photo, the background is one point in it, #73592b:

Text On Colour you see Contrast
#1f2937, opacity: 0.6 white card #797f87 4.04:1, fails
color: #475569 white card #475569 7.58:1
#f1f5f9, opacity: 0.6 dark card #1f2937 #9da3ab 5.77:1
white, opacity: 0.6 Terrace Room photo, point #73592b #c7bdaa 3.54:1, fails
color: #e2e8f0 Terrace Room photo, point #73592b #e2e8f0 5.34:1

The answer is it doesn't pass. Opacity at 60% doesn't leave 60% of the 14.68:1 contrast. The colour becomes #797f87, and the contrast is only 4.04:1. So secondary text should get a lighter colour that's been measured (the CSS variable --text-muted: #475569), not opacity. A solid colour keeps its contrast. The contrast of text with opacity changes every time the background changes, and over a photo it can drop below 4.5:1.

Disabled: opacity 0.5

A disabled element (one that can't be pressed yet) commonly gets opacity: 0.5. For example, the "3 nights" button when the Terrace Room is already booked on the third night. Here are the button, a note with the reason, and the CSS:

<button disabled>3 nights</button>
<p class="note">The night of 14 Dec is fully booked.</p>

<style>
  .nights button:disabled { opacity: 0.5; cursor: not-allowed; }
</style>

Blur behind: backdrop-filter

The booking page's header sticks to the top of the screen as the page scrolls (that's the job of position: sticky and top: 0). The header is also half see-through, so the room photos show behind it. Here is the CSS:

.top {
  position: sticky;
  top: 0;
  background: rgba(255, 255, 255, 0.7);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

Text contrast on glass

The glass's colour depends on what passes behind it. So measure the header text over the darkest colour that can pass. On Mrs. Sri's page, that colour is the night sky #0f172a in the Attic Room photo. The header text is #1f2937, and the background is white with a different alpha in each row of the table:

White alpha Glass colour over #0f172a Text contrast
0.3 #575d6a 2.22:1, fails
0.5 #878b94 4.30:1, fails
0.52 #8c9099 4.59:1, the thinnest that passes
0.7 #b7b9bf 7.48:1
0.92 #ececee 12.44:1

Blur doesn't rescue contrast. It smooths detail, but a wide dark area is still dark after it's blurred. What decides the contrast is the white alpha.

A fallback for browsers without backdrop-filter

A browser that doesn't know backdrop-filter yet needs a fallback background. @supports is a CSS rule. Only browsers that support the property in its brackets use its contents:

.top { background: rgba(255, 255, 255, 0.92); }  /* fallback: nearly solid */

@supports (backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px)) {
  .top {
    background: rgba(255, 255, 255, 0.7);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
  }
}

⚠️ See-through that makes text disappear

TL;DR: opacity (how see-through something is) makes the whole element and its children (the elements inside it) see-through. Alpha inside a colour, the last number in rgba(255, 255, 255, 0.7), makes only that colour see-through. Opacity mixes the colour with the background, so the contrast changes: text in #1f2937 at opacity: 0.6 is only 4.04:1, while the colour #475569 stays 7.58:1. For secondary text (less important text), use a colour. Disabled elements commonly get opacity: 0.5 plus the disabled attribute (WCAG exempts inactive components from the contrast rule), with the reason written next to them. The frosted glass effect is made with backdrop-filter: blur(), which blurs what's behind the element. 8 to 12px is the common value, and the browser's work grows with the element's size and the blur. Measure text on glass over the darkest colour that can pass behind it (#0f172a needs a white alpha of at least 0.52). For browsers without backdrop-filter, write a nearly solid fallback background outside the @supports block.

⚡ opacity or alpha in a colour

Write:

.note   { opacity: 0.6; }                           /* text, icons, borders, all at 60% */
.top    { background: rgba(255, 255, 255, 0.7); }   /* only the background at 70% */

Meaning: opacity (how see-through something is) applies to the whole element and its children (the elements inside it). Alpha, the last number in rgba(), makes only that colour see-through. So in .top, the text on that background stays solid.

⚡ Secondary text gets a colour, not opacity

Guess first: a note in #1f2937 (contrast 14.68:1 on white) gets opacity: 0.6 to look less important. Does it still pass 4.5:1?

No. Opacity mixes the text with the white background, so its colour becomes #797f87 and the contrast is only 4.04:1. Opacity at 60% doesn't leave 60% of the contrast.

Meaning: For secondary text (less important text), use a colour that's been measured, #475569 (7.58:1). The contrast of text with opacity changes every time the background changes, and over a photo it can drop below 4.5:1.

⚡ Disabled: opacity 0.5 plus disabled

Write:

<button disabled>3 nights</button>
<p class="note">The night of 14 Dec is fully booked.</p>

<style>
  .nights button:disabled { opacity: 0.5; cursor: not-allowed; }
</style>

Meaning: A disabled button (one that can't be pressed yet) commonly gets opacity: 0.5. WCAG exempts inactive components from the contrast rule. The disabled attribute (extra information inside the <button> tag) is what makes the button truly impossible to press. Opacity only changes its look. Write the reason too, like the note under that button.

⚡ Frosted glass with backdrop-filter

Meaning: backdrop-filter: blur() blurs what's behind the element, and that is the frosted glass effect. filter: blur() blurs the element itself, its text included. 8 to 12px is the common blur value. Blur doesn't rescue contrast. When a dark #0f172a photo passes behind the header (the top part of the page), the header's white background needs an alpha of at least 0.52 for its text to pass 4.5:1.

What you will learn