Detailed Explanation of the CSS Filter Property
Description
The CSS filter property allows you to apply graphical effects such as blur, brightness adjustment, contrast adjustment, etc., to an element (typically an image). Based on SVG filter primitives, it provides a way to achieve visual effects directly through CSS without the need for image processing software.
Core Concept
The filter property accepts one or more filter functions, each corresponding to a visual effect. Multiple filters can be chained and are applied in order. Filters affect the element's content (including child elements) and background.
Common Filter Functions and Usage
-
Blur (blur)
- Function: Applies a Gaussian blur effect to the element's content.
- Syntax:
blur(radius)– the larger the radius value, the more blurred the effect. - Example:
.element { filter: blur(5px); /* Blur radius of 5 pixels */ } - Note: A radius of 0 has no effect; percentage values are not supported.
-
Brightness (brightness)
- Function: Adjusts the brightness of the element.
- Syntax:
brightness(multiplier). A value of 1 keeps the original; values less than 1 darken, and values greater than 1 brighten. - Example:
.element { filter: brightness(0.5); /* Halves the brightness */ }
-
Contrast (contrast)
- Function: Adjusts the contrast of the element.
- Syntax:
contrast(multiplier). A value of 1 keeps the original; values less than 1 reduce contrast, and values greater than 1 increase contrast. - Example:
.element { filter: contrast(200%); /* Doubles the contrast */ }
-
Grayscale (grayscale)
- Function: Converts the element to grayscale.
- Syntax:
grayscale(amount). A value of 0 retains full color; 1 is fully grayscale. - Example:
.element { filter: grayscale(1); /* Fully grayscale */ }
-
Other Common Filters
sepia(amount): Converts to a sepia tone (0-1).hue-rotate(angle): Rotates the hue (e.g.,90deg).saturate(multiplier): Adjusts saturation (1 is original).opacity(amount): Adjusts opacity (similar to theopacityproperty, but filters can be combined).drop-shadow(offset-x offset-y blur-radius color): Adds a drop shadow effect (differs frombox-shadowas it conforms to the element's shape).
Multiple Filter Combinations
Multiple filters are separated by spaces and applied in sequence:
.element {
filter: brightness(1.2) contrast(0.8) blur(2px);
}
/* First adjusts brightness, then contrast, and finally blur */
Dynamic Effects and Transitions
Filters can be combined with the transition property for smooth animations:
.button {
filter: grayscale(0);
transition: filter 0.3s;
}
.button:hover {
filter: grayscale(1); /* Becomes grayscale on hover */
}
Performance Considerations
- Filters impact performance, especially when applied to many elements or with complex filters (like
blur). - Avoid applying filters frequently to large elements or animations; consider using
will-change: filterfor optimization.
Browser Compatibility
The filter property is supported by all major browsers, but older versions may require prefixes (e.g., -webkit-filter). Use tools like Autoprefixer for automatic handling.
By flexibly combining filter functions, you can achieve rich visual effects without modifying image assets, enhancing page interactivity.