https://www.joshwcomeau.com/css/backdrop-filter/ JoshWComeau * categories * courses * goodies * About Next-level frosted glass with backdrop-filter Filed under CSS on in December 2nd, 2024. Dec 2024. Last updated on in December 7th, 2024. Dec 2024. Table of Contents IntroductionCSS filtersThe IssuePointer eventsFlickering topThicker glassBrowser supportGlassy edgeThe final codeContinue learning Introduction One of my all-time favourite CSS tricks is using backdrop-filter: blur() to create a frosted glass effect. I use it in just about every project I work on, including this blog! Here's a quick demo, to show what I'm talking about: Some Example Website https://www.joshwcomeau.com/example-website Some Website [cupcake] This is an example website showing how I typically use backdrop-filter to create glassy headers. Notice that as the cupcake moves behind the header, it appears blurry, as it would if it was passing behind frosted glass. Play/Pause Animation This effect helps us add depth and realism to our projects. It's lovely. But when I see this effect in the wild, it's almost always missing some crucial optimizations. A couple of small changes can make our frosted glass so much more lush and realistic! In this post, you'll learn how to make the slickest frosted glass ever . We'll also learn quite a bit about CSS filters along the way! Credit where credit is due I learned about this effect from Artur Bien's(opens in new tab) incredible demos. He credits Jamie Gray(opens in new tab) for the original concept. Link to this headingCSS filters To briefly explain the underlying concept: CSS gives us quick and easy access to SVG filters via the filter property. For example, we can give elements a Gaussian blur with filter: blur (): My 3D mascot smiling with his tongue out. It's very blurry, but dragging the slider makes it clearer Blur radius:16px [16 ] There are lots of fun filter options, the sorts of things you'd find in image-editing software. Like, rotating the hue of all the colors: My 3D mascot making a puzzled face. Dragging the slider causes him to become unnatural shades of green and pink. Hue rotation:0deg [0 ] In these examples, I'm applying the filters to an tag, but we can apply them to standard DOM nodes as well: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. Blur radius:0px [0 ] Sepia percentage:0% [0 ] Pretty neat, right? Things get even cooler with backdrop-filter. This property lets us apply these same filters to the stuff behind a given element. For example: [tokyo] Sepia:50% [50 ] Brightness:130% [130 ] In this demo, the .magic-ring element sits in front of a photo ( source(opens in new tab)). It uses the backdrop-filter property to apply some filtering to everything behind it, which can be used for some pretty artistic effects. In practice, I pretty much only use backdrop-filter for one use case: blurring everything behind an element, usually a header, to create the "frosted glass" effect I mentioned earlier: Some Example Website https://www.joshwcomeau.com/example-website Some Website [cupcake] This is an example website showing how I typically use backdrop-filter to create glassy headers. Notice that as the cupcake moves behind the header, it appears blurry, as it would if it was passing behind frosted glass. Play/Pause Animation Alright. Let's talk about the thing most developers miss. Link to this headingThe Issue Here's the problem: The backdrop-filter algorithm only considers the pixels that are directly behind the element. For filters like brightness or hue-rotate, that makes perfect sense. With blur, though, we actually want to consider pixels that are near the element too. This is one of those things where a demo is worth a thousand words. Check out the difference: DefaultTweaked Play/Pause Animation By default, the gaussian blur algorithm is applied to all of the pixels behind the element. This means that if a big colorful element is near the element, it won't have any effect. That's not really how frosted glass works in real life though. Light bounces off of objects and then goes through the glass. It looks so much better when the blurring algorithm includes nearby content. Unfortunately, this isn't something we can configure directly. Instead, we need to be a bit crafty. Here's the code: Code Playground Format code using Prettier Reset Code HTMLCSS Focus the editor. This will trap focus until you press Escape.Code editor:
[
[
[
[ [
[

Try to select this text

[
Experiment with other filters! In the example above, I increased the brightness of everything behind the glassy edge by 20% with a secondary filter, brightness(1.2). And in my original cupcake example, I tweaked both the brightness and saturation: Copy to clipboard .backdrop { backdrop-filter: blur(8px) brightness(90%) saturate(140%); } .backdrop-edge { backdrop-filter: blur(6px) brightness(110%) saturate(120%); } I've heard that increased saturation can help reduce the muddiness that comes from blurring, but really, this is more art than science. I recommend experimenting with various filter effects to come up with something that works for your particular use case! Link to this headingThe final code Phew! We covered a lot of ground in this one. Here's the final code, with all of the optimizations we've discussed. I've also included feature queries, to make sure that our website remains legible on older browsers. Feel free to copy this code, and make it your own! This is intended to be a starting point, not a complete solution. For example, you may wish to tweak the size of the backdrop's overlap for your particular circumstances. Code Playground Format code using Prettier Reset Code HTMLCSS Focus the editor. This will trap focus until you press Escape.Code editor:
[