Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 139 ยท Backgrounds and Effects

Filters

Apply CSS filter functions like blur, brightness, grayscale and drop-shadow, chain them together, and build frosted-glass panels with backdrop-filter.

Photo editors have sliders for brightness, contrast, saturation and blur. CSS has the same tools, built in. The filter property applies graphical effects to an element after it has been drawn, so it affects everything inside the element: text, images, borders, backgrounds and child elements alike.

A second property, backdrop-filter, applies the same effects to whatever is behind an element instead. That is how you create the frosted-glass panels you see in modern operating systems. In this lesson you will meet every filter function, learn how to combine them, and build a frosted card.

The filter property

filter takes one or more filter functions. Here is the complete list:

Function Example Effect
blur() blur(4px) Gaussian blur; the length is the blur radius.
brightness() brightness(1.3) 1 is unchanged, 0 is black, above 1 is brighter.
contrast() contrast(150%) 1 is unchanged, 0 is flat gray.
saturate() saturate(2) 0 removes all color, above 1 is more vivid.
grayscale() grayscale(1) 1 is fully gray; 0 is unchanged.
sepia() sepia(0.8) Warm, old-photo brown tones.
hue-rotate() hue-rotate(90deg) Turns every color around the color wheel.
invert() invert(1) Swaps colors for their opposites.
opacity() opacity(0.5) Like the opacity property.
drop-shadow() drop-shadow(0 4px 6px #0006) A shadow that follows the element's shape.

Numbers and percentages are interchangeable: 0.5 and 50% mean the same thing. If you leave the argument out, you get the full effect, so grayscale() means grayscale(1).

One filter per image

Edit the numbers and watch the result. The empty alt="" is deliberate here: these copies are decorative repeats of the same picture, so screen readers can skip them.

Chaining filters

To apply several effects, list them separated by spaces (not commas):

CSS
.vintage {
  filter: sepia(0.6) contrast(1.1) brightness(0.95) saturate(1.2);
}

The functions run in order, each one working on the result of the previous one. Order often matters. grayscale(1) sepia(1) gives a brown tint, because sepia is applied to the gray image. sepia(1) grayscale(1) ends up plain gray, because grayscale removes the brown that sepia added.

Like every property, writing filter again replaces the whole list. If one rule sets filter: blur(2px) and a hover rule sets filter: brightness(1.2), the hovered element is bright but no longer blurred. Repeat the functions you want to keep.

drop-shadow() versus box-shadow

box-shadow, which you met in Box Shadows, always draws the shadow of the element's rectangle (rounded by border-radius). drop-shadow() draws the shadow of the visible pixels. For a transparent PNG, an SVG icon or text, it follows the actual outline:

drop-shadow follows the shape

The first star has a square shadow, because box-shadow only knows about the box. The second one's shadow is star-shaped. The speech bubble shows another advantage: the shadow covers the pseudo-element tail too, because the filter applies to the whole rendered element. drop-shadow() has no spread value and no inset option, though.

Filters for interaction and state

Filters animate smoothly with transitions, which makes them great for hover effects and "disabled" looks:

CSS
.logo-wall img {
  filter: grayscale(1) opacity(0.7);
  transition: filter 0.3s;
}
.logo-wall img:hover,
.logo-wall img:focus-visible {
  filter: none;
}

Some other patterns you will meet:

  • A quick dark-mode image fix: filter: invert(1) hue-rotate(180deg) inverts brightness but turns hues back around, so diagrams with black lines on white become white lines on black without the colors going strange. Use it only for simple diagrams, never for photos.
  • Recoloring a one-color icon with a chain of sepia, saturate and hue-rotate is possible but fiddly. A better option for icons is usually masks or SVG with currentColor.

Filters change the stacking. Any filter value other than none creates a new stacking context and a containing block for position: fixed descendants. A fixed header inside a filtered element suddenly scrolls with it. You will learn why in z-index and Stacking Contexts.

backdrop-filter: frosted glass

backdrop-filter takes exactly the same functions, but applies them to the area behind the element. You only see the effect if the element's own background is at least partly transparent:

CSS
.glass {
  background: rgb(255 255 255 / 0.25);
  backdrop-filter: blur(12px) saturate(1.5);
}

The semi-transparent white tints the result, and the blur softens whatever passes underneath. Add a thin light border and a soft shadow, and the panel looks like a sheet of frosted glass:

A frosted-glass card

Remove the backdrop-filter lines and the circles show sharply through the card. Change blur(14px) to blur(3px) for a lighter frost.

backdrop-filter works in all current major browsers. Safari supported it for years only with the -webkit- prefix and gained the unprefixed name in version 18 (2024), so keep both lines for a while. Where it is unsupported, the panel simply shows its translucent background, so make sure your text is still readable without the blur. A slightly more opaque background color is a safe fallback.

Filters cost performance. Large blurs are expensive, especially backdrop-filter, which has to be recalculated whenever anything behind the element moves. A frosted header that sits over scrolling content is redrawn on every scroll step. Keep blurred areas small, and test on a modest phone.

Filtering with SVG

filter also accepts url() pointing to an SVG <filter> element, such as filter: url(#noise). SVG filters can do things the built-in functions cannot, like adding grain, displacing pixels or creating gooey blobs. They are powerful but verbose; the built-in functions cover nearly every everyday need.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you apply both a blur and a grayscale effect with one filter declaration?

    Filter functions are separated by spaces. A second filter declaration would replace the first one.
  2. 2. What is the main advantage of drop-shadow() over box-shadow?

    drop-shadow() uses the rendered shape, including transparency, while box-shadow only follows the box. It has no spread or inset.
  3. 3. Why might backdrop-filter: blur(10px) seem to do nothing?

    The filter is applied to the content behind the element. An opaque background covers it, so you need a transparent or semi-transparent background.
  4. 4. Does filter: sepia(1) grayscale(1) look the same as filter: grayscale(1) sepia(1)?

    Each function processes the result of the previous one, so the final step wins here.

Summary

  • filter applies effects such as blur(), brightness(), contrast(), grayscale(), hue-rotate() and drop-shadow() to an element and everything inside it.
  • Chain functions with spaces; they run in order, and a new filter declaration replaces the whole list.
  • drop-shadow() follows the visible shape, unlike box-shadow.
  • Filters animate well, but they create stacking contexts and can be expensive.
  • backdrop-filter filters what is behind a semi-transparent element: the basis of frosted glass. Include the -webkit- prefix and a readable fallback.

Next: Blend Modes โ€” mix colors between layers and elements like a graphics editor.