日本語

Creating a Frosted Glass Background with backdrop-filter in CSS

​​CSS​HTML,Playwright

A background that blurs and shows through what is behind it, like frosted glass, can be made by setting the element's background color to semi-transparent and blurring what is behind it with backdrop-filter: blur(). If the background color is opaque, the blur is not visible. Also, if you put a frosted glass menu inside a frosted glass header, only the menu stops blurring. Moving the header's blur to ::before fixes this.

Note

The test environment is Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page made only of HTML and CSS with Playwright, took a screenshot of the inside of each element, and calculated the standard deviation of pixel brightness and the saturation. The smaller the variation, the more the pattern behind has been blurred. I did not test Firefox or Safari.

Three screens stacked vertically, each with a card over a background of vertical stripes and light blue and pink circles. The top uses only semi-transparent white, so the stripes and text behind show through and clash with the card's text, making it hard to read. The middle adds blur(12px) to the semi-transparent white, so the background is blurred into a grayish surface and the card's text is readable. The bottom uses opaque white with blur(12px), which is just a plain white card
Three kinds of cards placed over the same background. Only the middle one looks like frosted glass

Combining a Semi-Transparent Background Color with backdrop-filter

The CSS for the frosted glass card is as follows.

css
.glass {
  background-color: rgb(255 255 255 / 0.3); /* semi-transparent white */
  -webkit-backdrop-filter: blur(12px); /* for Safari 17 and earlier */
  backdrop-filter: blur(12px); /* blurs what is behind */
  border: 1px solid rgb(255 255 255 / 0.5);
  border-radius: 16px;
}

backdrop-filter is a property that applies blur or color changes to what is behind an element. It changes the part of the background visible through the element, not the element itself. For the blur, use blur().

I placed this card over a background with fine vertical stripes and colored circles and measured it. The standard deviation of the brightness inside the card was 61.8 with only semi-transparent white, and 17.6 when blur(12px) was added. The contrast between the white and black stripes was evened out, and the area looks like a blurred surface.

Even without a background color, applying only backdrop-filter still produced a blur (standard deviation 25.0). However, the average brightness was 165.9, darker than with semi-transparent white (192.2). The semi-transparent white plays the role of making the background look brighter.

With an Opaque Background Color, the Blur Is Not Visible

I applied blur(12px) to a card with an opaque white background (#fff) and compared the pixels with a card without it. None of the 104,000 values inside the card (width × height × 4 RGBA channels) differed.

The MDN page for backdrop-filter also says the element, or its background, must be transparent or semi-transparent. If the background does not blur, first check the transparency of the background color.

Choose the blur Value by the Size of the Pattern Behind

I changed the blur() value from 0px to 32px and measured how much of the stripes remained inside the card. I used only stripes behind the card and tested two patterns: fine stripes (repeating every 12px) and coarse stripes (every 48px).

A line graph with the blur() value on the horizontal axis and the standard deviation of brightness inside the glass on the vertical axis. The fine-stripe line (gray) drops to nearly 0 at 4px. The coarse-stripe line (light blue) falls slowly, reaching 22.4 at blur(12px) (pink point) and 6.2 at 32px
The blur() value and how much stripe contrast remains inside the glass. Fine stripes almost disappear at 4px, while coarse stripes still remain at 12px

For the fine stripes, blur(4px) lowered the standard deviation to 4.1, and they were almost invisible. For the coarse stripes, the value was 22.4 at blur(12px), 13.5 at 16px, and 8.0 at 24px. The larger the pattern, the larger the blur value needed to smooth it out. I only tested stripes, but based on these results, a larger value seems right when the background has large photos or thick text, and a smaller value seems right when it has only fine patterns.

To Make Colors Vivid, Add saturate()

Blurring tends to mix the background colors and make them look dull. On a background with colored circles, the average saturation inside the card was 0.170 with only semi-transparent white, and dropped to 0.161 when blur(12px) was added. To keep the colors, chain saturate() after the blur, separated by a space.

css
backdrop-filter: blur(12px) saturate(180%);

Adding saturate(180%) raised the average saturation to 0.226.

If a Menu Inside a Frosted Glass Header Doesn't Blur, Move the Blur to ::before

If you place a frosted glass menu that extends downward inside a frosted glass header, the area behind the menu is not blurred.

css
.bar {
  position: relative;
  height: 48px;
  background-color: rgb(255 255 255 / 0.3);
  backdrop-filter: blur(12px);
}

.menu {
  position: absolute;
  top: 100%;
  left: 40px;
  width: 200px;
  height: 120px;
  background-color: rgb(255 255 255 / 0.3);
  backdrop-filter: blur(12px);
  border-radius: 0 0 12px 12px;
}
Two screens stacked vertically, each showing a header and a menu extending below it over a background of stripes and circles. The top has backdrop-filter on the header, and the stripes and light blue circle behind the menu show through clearly. The bottom has the blur moved to ::before, and the area behind the menu is also blurred
With backdrop-filter on the header, the area behind the menu is not blurred (top). With the blur moved to ::before, it is blurred (bottom)

When I measured the part of the menu that extends below the header, the standard deviation was 44.3. With backdrop-filter removed from the menu, it was still 44.3, and the average brightness and saturation were the same too. The blur had no effect at all.

According to the "Backdrop root" section on the English backdrop-filter MDN page, backdrop-filter blurs only up to the nearest backdrop root ancestor. An element with backdrop-filter, filter, an opacity less than 1, mask, clip-path, mix-blend-mode, and similar properties becomes a backdrop root. The header has backdrop-filter, so it becomes a backdrop root. Therefore, the page background behind the header is visible through the menu but is not blurred.

When I gave the parent opacity: 0.99 or filter: brightness(1), the standard deviations of the nested frosted glass card were 62.2 and 61.7. These are almost the same as the semi-transparent white without blur (61.8). Even values that barely change the look stop the blur from working when applied to the parent.

To fix this, move the header's blur to ::before and do not put backdrop-filter on the header itself.

css
.bar-fixed {
  position: relative;
  isolation: isolate; /* to place ::before below the header text and menu */
  height: 48px;
}

.bar-fixed::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background-color: rgb(255 255 255 / 0.3);
  backdrop-filter: blur(12px);
}

.menu stays as in the previous code. With this, the part of the menu below the header had a standard deviation of 5.3, meaning the background was blurred. The part of the header without the menu was 3.6, the same as before the fix, so the header's frosted glass is kept as well.

Because ::before is placed below the text with z-index: -1, the header gets isolation: isolate to create a new stacking context. When I removed it, the header's frosted glass disappeared, and the stripes in the header area were visible without blur (standard deviation 101.3). isolation is not among the backdrop root conditions listed on MDN.

Also Write the -webkit- Prefix for Safari

According to MDN's compatibility data (@mdn/browser-compat-data 8.1.8), backdrop-filter is available from Chrome 76 and Firefox 103. In Safari, the unprefixed version is available from 18, and -webkit-backdrop-filter from 9. If you also need to support Safari 17 and earlier, write both, as in the first code block.

In the test environment's Chromium 153, CSS.supports('-webkit-backdrop-filter', 'blur(12px)') returned false, and CSS.supports('backdrop-filter', 'blur(12px)') returned true. Chromium uses only the unprefixed line, and writing both still blurs correctly.

​Posted by 86