Educational guide to filter
How to compose CSS filters
Understand which pixels filter changes, how function order affects the result, and when to use a shadow that follows transparency. Examples are independent of the playground.
Mental model
Think of an image of the already painted element: a chain of operations transforms its pixels before compositing them onto the page.
- Painted element
Content, background, border, and descendants.
- Function chain
Each operation receives the previous result.
- Visual result
Appearance changes without recalculating the box space.
Not inherited, but grouped
The initial value is none and the property is not inherited. Filtering a container still affects its children’s content: everything is painted as a group before applying the effect.
The effect does not reserve space
Blur and shadows may extend outside the box, but filter does not change layout. Leave enough space and check container overflow to avoid clipping.
filter changes the element’s own pixels, including text and borders. backdrop-filter acts behind the surface. To soften an illustration while keeping text readable, apply filter only to the illustration.
Filter the group or only the image
Filter on the container
This text should stay readable.
Filter only on the SVG
This text should stay readable.
Functions and values
Separate the list with spaces, not commas. These are the playground’s ten functions, grouped by the change they produce.
Light and contrast
brightness(110%) contrast(118%)100% equals 1 and leaves the result unchanged. brightness(0) turns pixels black and contrast(0) makes them gray. Both accept values above 100%; increasing contrast does not guarantee accessible text contrast.
Saturation and hue
saturate(150%) hue-rotate(90deg)saturate(100%) is neutral; 0% removes saturation and values above 100% increase it. hue-rotate() takes an angle, with 0deg as its neutral value. It uses a color matrix, not an HSL adjustment that exactly preserves lightness and saturation.
Grayscale, inversion, and sepia
grayscale(100%) invert(0%) sepia(0%)For these three functions, 0% is neutral and 100% applies the full conversion. Values above 100% are clamped to 100%; they add no intensity. Use grayscale to desaturate, invert to invert channels, and sepia for a warm finish.
Transparency
opacity(60%)opacity(100%) preserves alpha and 0% makes the result transparent; values above 100% are clamped to 100%. Inside filter, its position in the chain can affect a later shadow. The CSS opacity property acts separately; its effect combines with the filter.
Blur and shadow
blur(2px) drop-shadow(8px 8px 0 blue)blur() takes a nonnegative length, without percentages; 0px is neutral. drop-shadow() accepts two offsets, an optional nonnegative blur length, and a color. Offsets may be negative. It accepts neither inset nor spread.
Neutral values preserve appearance, but blur(0) is not equivalent to filter: none for stacking and positioning. Specify arguments: some empty functions, such as grayscale(), apply the full effect rather than the neutral value.
Order changes the result
CSS applies functions from left to right. The output of an operation, including a newly created shadow, enters the next one.
With grayscale → drop-shadow, the circle becomes gray first and then gains a blue shadow. Reversing the chain to drop-shadow → grayscale also turns the shadow gray. Both lists use the same values.
The same functions in reverse order
Reference: no filter
1. grayscale → 2. drop-shadow
1. drop-shadow → 2. grayscale
Two filter declarations do not add effects: the cascade winner replaces the other. Write the complete chain in one declaration. You can repeat a function; to change order here, edit the copied CSS. The playground serializes functions in a fixed order.
Practical example
A card with a monochrome illustration and sharp text: the filter belongs to the SVG, not the container.
The recipe combines contrast(118%) grayscale(100%) using the lab’s function generator. Replace the SVG with your illustration or an image with an appropriate alt, and keep text outside the filtered element.
Monochrome illustration, unfiltered text
A monochrome illustration accompanies this unfiltered text.
The examples’ SVG is decorative and hidden with aria-hidden. If an image conveys information, retain its text alternative. A visual filter alone cannot communicate a state or securely hide content.
drop-shadow() versus box-shadow
Choose what casts the shadow: the box or pixels with alpha. With an opaque rectangular image, the difference can be less obvious.
box-shadow follows the SVG’s rectangular box, including empty areas. drop-shadow() uses the painted result’s alpha: it follows the circle and preserves its hole. Adding an opaque background to the filtered element makes that background part of the silhouette too.
A transparent shape with a hole
box-shadow
drop-shadow()
drop-shadow(8px 8px 4px blue)The third drop-shadow() length specifies the standard deviation of Gaussian blur, whereas box-shadow uses a blur radius: the same number should not be expected to look identical. For multiple filter shadows, chain drop-shadow() calls; later calls receive the previous result, including its shadows.
Common mistakes
Identify what is painted, which declaration wins, and where the result is clipped.
Text gets blurred too
- Expected result
- Change the image while keeping text sharp.
- Cause
- The filter is on a container that includes both image and text.
- Solution
- Move filter to the visual element. To filter the background behind a panel, explore backdrop-filter.
An effect disappears when another is added
- Expected result
- Keep blur and grayscale together.
- Cause
- Two filter declarations compete in the cascade; they do not accumulate.
- Solution
- Use a complete list, such as filter: blur(2px) grayscale(100%), and check its order.
The chain does not apply
- Expected result
- See every effect from a valid declaration.
- Cause
- A comma between functions, a blur percentage, or an unsupported function invalidates the whole declaration.
- Solution
- Separate with spaces, check units, and simplify one function at a time. The browser keeps the valid cascade winner.
The shadow is still rectangular
- Expected result
- Follow the image’s transparent silhouette.
- Cause
- The image or filtered element’s background has opaque alpha across the whole box.
- Solution
- Use a transparent asset and apply drop-shadow to the appropriate element without an opaque background.
The shadow or blur is clipped
- Expected result
- See the full effect without covering nearby content.
- Cause
- The effect reserves no space and an ancestor may clip it with overflow.
- Solution
- Leave space and inspect clipping. Do not remove necessary overflow without checking the layout.
Support and compatibility
CSS filter functions are widely available. The card shows the feature’s Baseline status independently of the visible result in this browser.
Examples use native CSS functions and render without JavaScript. filter also accepts url() to reference SVG filters, outside these recipes’ scope; the overall status does not prove support for every resource, context, or SVG detail.
A computed value other than none, even blur(0), creates a stacking context. Except on the root element, it also establishes a containing block for absolute and fixed descendants. Filtering a large wrapper may change overlays and their relationship with z-index.
Cost depends on the painted area, radius, chain, and updates. Keep filters on small surfaces and measure on real devices before animating large areas. Avoid assuming free acceleration or adding permanent will-change. These recipes add no motion.