CSS3 Filters
The CSS filter property applies graphical effects such as blur, brightness changes or greyscale to an element as the browser draws it. Filters work on images, text, backgrounds and whole containers without editing the original files, which makes them handy for hover effects, disabled states and quick image adjustments.
The built-in filter functions
| Function | Typical value | Effect |
|---|---|---|
blur() | 4px | Softens the element; larger values blur more. |
brightness() | 0.7 or 130% | Below 1 darkens, above 1 lightens. |
contrast() | 1.4 | Increases or reduces difference between light and dark. |
grayscale() | 1 | Removes colour; 1 means fully grey. |
sepia() | 0.8 | Adds a warm brown, old-photo tone. |
saturate() | 2 | Boosts or reduces colour intensity. |
hue-rotate() | 90deg | Shifts every colour around the colour wheel. |
invert() | 1 | Flips colours to their opposites. |
opacity() | 0.5 | Makes the element see-through. |
drop-shadow() | 0 4px 6px #0006 | Shadow that follows the visible shape. |
Combining filters and animating them
List several functions separated by spaces. They are applied in order, left to right, and the order can change the result. Filters can also be transitioned, which gives smooth hover effects:
.gallery img {
filter: grayscale(1) brightness(0.9);
transition: filter 0.3s ease;
}
.gallery img:hover,
.gallery img:focus-visible {
filter: grayscale(0) brightness(1.05);
}
.button[disabled] {
filter: saturate(0.2) opacity(0.6);
cursor: not-allowed;
}
.logo {
/* shadow traces the transparent PNG outline */
filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.35));
}
For a transition to animate smoothly, keep the same functions in the same order in both states, changing only their values, as the gallery example does.
drop-shadow versus box-shadow
box-shadow always draws around the element's rectangular box. filter: drop-shadow() follows the actual visible pixels, so a transparent PNG icon or an SVG gets a shadow shaped like the icon rather than a square behind it. However, drop-shadow() has no spread value and no inset option, so box-shadow remains the better choice for cards and buttons.
Blurring what is behind an element
A related property, backdrop-filter, applies the same functions to whatever sits behind an element instead of the element itself. Combined with a semi-transparent background, it creates the frosted-glass look often used on sticky headers: background: rgba(255,255,255,0.6); backdrop-filter: blur(8px);. Without a partly transparent background you will not see any effect, because the element hides what lies behind it.
Gotchas worth knowing
A filter on a parent affects every child. If you blur a card, its text blurs too, and there is no way for a child to opt out. Apply the filter to a separate background layer, such as a pseudo-element, if only the background should change.
Filters also create a new stacking context and a containing block. That means a child with position: fixed inside a filtered element is positioned relative to that element, not the viewport, which can break sticky headers or modals placed inside it. They can also flatten 3D transforms. Finally, heavy blur values on large or animated areas cost the browser more work, so test scrolling on a low-end phone.
Main points
Use filter to adjust how an element looks without editing assets, chain functions in a deliberate order, transition them for smooth hover states, and reach for drop-shadow() when the shadow must follow a shape. Keep in mind that filters affect all children and change how fixed positioning behaves inside them.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.