🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Filter

AI & DATA SCIENCE // filter

The filter property applies graphical effects like blur, brightness, and grayscale directly to an element, without needing external image editing or additional markup.

Syntax

filter: blur(px) | brightness(%) | grayscale(%) | contrast(%) | drop-shadow(...) | ...;

Deep Dive Course

Each individual filter function, like blur(5px) or grayscale(100%), applies its specific graphical effect to the element and everything rendered inside it, and multiple filter functions can be chained together in a single space-separated declaration, applying all of them in sequence. filter also creates a new stacking context for the element it's applied to, the same way transform and opacity below 1 do, which is worth remembering when troubleshooting unexpected z-index stacking behavior involving a filtered element or its descendants.

1Understanding Filter

Each individual filter function, like blur(5px) or grayscale(100%), applies its specific graphical effect to the element and everything rendered inside it, and multiple filter functions can be chained together in a single space-separated declaration, applying all of them in sequence. filter also creates a new stacking context for the element it's applied to, the same way transform and opacity below 1 do, which is worth remembering when troubleshooting unexpected z-index stacking behavior involving a filtered element or its descendants.

💡

Applying filter to an element creates a new stacking context, the same as transform or opacity below 1 — if a descendant's z-index seems to be behaving unexpectedly, check whether an ancestor's filter property might be the actual, easy-to-overlook cause.

editor.html
.preview-image {
  filter: grayscale(100%);
  transition: filter 0.3s ease;
}

.preview-image:hover {
  filter: grayscale(0%);
}
localhost:3000

2Practical Example

Here is a real-world application of Filter showing how it is used in production CSS code.

editor.html
.card {
  filter: drop-shadow(0 4px 6px rgba(0,0,0,0.3)) brightness(1.1);
}
localhost:3000

3Best Practices

Follow these guidelines when working with Filter:

1. Chain multiple filter functions together in a single declaration, like filter: blur(2px) brightness(0.8), to apply several graphical effects at once

2. Use filter: grayscale(100%) transitioning to grayscale(0%) on hover as a common, simple technique for a color-on-hover image effect

3. Remember filter creates a new stacking context, the same as transform and opacity below 1, which can affect z-index behavior for that element's descendants

⚠️

Tip: Applying filter to an element creates a new stacking context, the same as transform or opacity below 1 — if a descendant's z-index seems to be behaving unexpectedly, check whether an ancestor's filter property might be the actual, easy-to-overlook cause.

editor.html
.preview-image {
  filter: grayscale(100%);
  transition: filter 0.3s ease;
}

.preview-image:hover {
  filter: grayscale(0%);
}
localhost:3000

Examples

Example 01Basic Usage
.preview-image {
  filter: grayscale(100%);
  transition: filter 0.3s ease;
}

.preview-image:hover {
  filter: grayscale(0%);
}
Example 02Advanced Example
.card {
  filter: drop-shadow(0 4px 6px rgba(0,0,0,0.3)) brightness(1.1);
}

Best Practices

  • Chain multiple filter functions together in a single declaration, like filter: blur(2px) brightness(0.8), to apply several graphical effects at once
  • Use filter: grayscale(100%) transitioning to grayscale(0%) on hover as a common, simple technique for a color-on-hover image effect
  • Remember filter creates a new stacking context, the same as transform and opacity below 1, which can affect z-index behavior for that element's descendants

Interview Question

Why does drop-shadow(), one of the filter functions, more accurately follow an element's actual visible, non-rectangular shape compared to the separate box-shadow property, which always produces a rectangular (or rounded-rectangle) shadow?

Hint: Think about what each shadow technique's shadow shape is actually calculated from — the element's rectangular box boundaries, or its actual rendered, potentially irregular pixel content.

box-shadow calculates and renders its shadow shape based purely on the element's own rectangular box model, its width, height, and border-radius if rounded, completely independent of what's actually visually rendered inside that box, which means an element containing a transparent PNG image with an irregular, non-rectangular visible shape, like a star or an object silhouette, still produces a plain rectangular shadow that doesn't match the image's actual visible silhouette at all. filter's drop-shadow() function, by contrast, calculates its shadow based on the element's actual rendered alpha channel, its real visible pixel shape after accounting for any transparency, meaning a transparent PNG's shadow correctly follows that irregular silhouette's true outline rather than the underlying rectangular box's boundaries. This distinction, box-boundary-based versus actual-rendered-shape-based shadow calculation, is exactly why drop-shadow() is the appropriate, more visually accurate choice specifically for adding a shadow to non-rectangular content like a transparent-background image, while box-shadow remains perfectly appropriate, and generally more performant, for straightforward rectangular or rounded-rectangle elements.

Exercises

MediumPractice using Filter in a real scenario.
View Solution
.preview-image {
  filter: grayscale(100%);
  transition: filter 0.3s ease;
}

.preview-image:hover {
  filter: grayscale(0%);
}

Frequently Asked Questions

Why does drop-shadow(), one of the filter functions, more accurately follow an element's actual visible, non-rectangular shape compared to the separate box-shadow property, which always produces a rectangular (or rounded-rectangle) shadow?

box-shadow calculates and renders its shadow shape based purely on the element's own rectangular box model, its width, height, and border-radius if rounded, completely independent of what's actually visually rendered inside that box, which means an element containing a transparent PNG image with an irregular, non-rectangular visible shape, like a star or an object silhouette, still produces a plain rectangular shadow that doesn't match the image's actual visible silhouette at all. filter's drop-shadow() function, by contrast, calculates its shadow based on the element's actual rendered alpha channel, its real visible pixel shape after accounting for any transparency, meaning a transparent PNG's shadow correctly follows that irregular silhouette's true outline rather than the underlying rectangular box's boundaries. This distinction, box-boundary-based versus actual-rendered-shape-based shadow calculation, is exactly why drop-shadow() is the appropriate, more visually accurate choice specifically for adding a shadow to non-rectangular content like a transparent-background image, while box-shadow remains perfectly appropriate, and generally more performant, for straightforward rectangular or rounded-rectangle elements.

Related Functions

OpacityClip-pathText-shadow