Blur an element
filter: blur() takes a length, the blur radius. The larger it is, the blurrier the element:
filter works on any element, not only images: a blurred div blurs its text, border and children too. Notice the soft edges on the blurred images: the blur spreads the picture outward and mixes in the transparent area around it. The fix is below, under "The blurry edge problem".
Blur the background: backdrop-filter
To blur what is behind an element (a frosted glass panel over a photo or a page), use backdrop-filter. The element's own content stays sharp. It needs a see-through background to show the blur:
The stripes and circles are sharp around the panel and blurred behind it, while the panel's text is untouched. Keep the text readable: a lighter or darker tint in the background color helps on busy images.
Every filter function, live
Move any slider. The line under the image is the exact filter value, ready to copy:
Only functions you moved away from their neutral value are written out, which keeps the value short. brightness(1) or blur(0px) change nothing, so there is no reason to list them.
Filter functions
| Function | Neutral value | What it does | Example |
|---|---|---|---|
blur(r) | 0 | Gaussian blur with radius r | blur(4px) |
brightness(n) | 1 | darker below 1, brighter above | brightness(0.7) |
contrast(n) | 1 | flatter below 1, punchier above | contrast(1.3) |
grayscale(n) | 0 | 1 (or 100%) is fully gray | grayscale(100%) |
sepia(n) | 0 | warm brown tone | sepia(0.6) |
saturate(n) | 1 | 0 is gray, above 1 more vivid | saturate(1.5) |
hue-rotate(a) | 0deg | shifts every hue around the color wheel | hue-rotate(90deg) |
invert(n) | 0 | 1 is a full color negative | invert(1) |
opacity(n) | 1 | like the opacity property | opacity(0.5) |
drop-shadow(x y blur color) | a shadow that follows the shape | drop-shadow(2px 4px 6px rgb(0 0 0 / 0.3)) |
Numbers can also be percentages (grayscale(100%) equals grayscale(1)). Several functions go in one value, separated by spaces, and are applied in order: filter: grayscale(1) sepia(1) gives a brown tint, while sepia(1) grayscale(1) ends up plain gray.
drop-shadow follows the shape
box-shadow is always a rectangle (with rounded corners at most). drop-shadow() is drawn from the visible pixels, so a transparent icon gets a shadow with the icon's outline:
The first star casts a square shadow from its invisible bounding box. The second casts a star-shaped one. Unlike box-shadow, drop-shadow() has no spread value and no inset; the box-shadow page covers those.
The blurry edge problem
A blurred image fades out at its edges, because the blur pulls in the transparent pixels around it. To keep a hard edge (for a blurred hero background, for example), put the image in a container with overflow: hidden and scale the image up slightly so the soft edge is cut off:
.hero { overflow: hidden; }
.hero img { filter: blur(8px); transform: scale(1.1); }
Hover effects with filter
filter can be transitioned, so a gray-to-color logo wall or a dim-until-hovered card is two rules:
Hover (or tab to) a tile and its color fades in.
Good to know
- Performance. Large blur radii on large areas cost the most, especially
backdrop-filteron elements that move or scroll. Test on a phone. - Readability. Never blur text people need to read, and check contrast on text over a frosted panel.
- Stacking. Any
filterother thannonecreates a new stacking context and becomes the containing block forposition: fixeddescendants, liketransform.
Common mistakes
filter: blur(5). The radius needs a unit:blur(5px).- Using
filter: blur()for a frosted panel. It blurs the panel's own text. Usebackdrop-filterwith a transparent background. - A backdrop-filter that shows nothing. The background of the element must be at least partly transparent, and there must be something behind it.
- Two
filterdeclarations. The second replaces the first. Put all functions in one value. - Box-shaped shadows on transparent icons. Use
filter: drop-shadow()instead ofbox-shadow.
Frequently Asked Questions
How do I blur an element in CSS?
Use filter: blur(5px);. The length is the blur radius: 0 is sharp and larger values are blurrier. It blurs the element and everything inside it, including its text and images.
How do I blur the background behind an element?
Use backdrop-filter: blur(10px); on the element, with a partly transparent background such as rgb(255 255 255 / 0.5). The element's own content stays sharp and whatever is behind it is blurred, the frosted glass effect.
What is the difference between drop-shadow() and box-shadow?
box-shadow always follows the element's rectangular box. filter: drop-shadow() follows the visible shape, including transparent areas of a PNG or SVG, so an icon's shadow has the icon's outline.
How do I make an image black and white in CSS?
Use filter: grayscale(100%); (or grayscale(1)). Add a transition and filter: grayscale(0) on hover to bring the color back.
Can I use several filters at once?
Yes, list them separated by spaces: filter: grayscale(1) brightness(1.2) blur(1px);. They are applied in order, so the order can change the result. A second filter declaration replaces the first.