filter
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
The filter
CSS property applies graphical effects like blur or color shift to an element. Filters are commonly used to adjust the rendering of images, backgrounds, and borders.
Several functions, such as blur()
and contrast()
, are available to help you achieve predefined effects.
Try it
Syntax
/* <filter-function> values */
filter: blur(5px);
filter: brightness(0.4);
filter: contrast(200%);
filter: drop-shadow(16px 16px 20px blue);
filter: grayscale(50%);
filter: hue-rotate(90deg);
filter: invert(75%);
filter: opacity(25%);
filter: saturate(30%);
filter: sepia(60%);
/* URL */
filter: url("filters.svg#filter-id");
/* Multiple filters */
filter: contrast(175%) brightness(3%);
filter: drop-shadow(3px 3px red) sepia(100%) drop-shadow(-3px -3px blue);
/* Use no filter */
filter: none;
/* Global values */
filter: inherit;
filter: initial;
filter: revert;
filter: revert-layer;
filter: unset;
With a function, use the following:
filter: <filter-function> [<filter-function>]* | none;
You can use url()
to reference an SVG filter element. For a reference to an SVG <filter>
element, use the following syntax:
filter: url(file.svg#filter-element-id);
Functions
The filter
property is specified as none
or one or more of the functions listed below. If the parameter for any function is invalid, the function returns none
. Except where noted, the functions that take a value expressed with a percent sign (as in 34%
) also accept the value expressed as decimal (as in 0.34
).
When the filter
property values contains multiple functions, the filters are applied in order.
blur()
-
Applies a Gaussian blur to the input image.
cssfilter: blur(5px);
brightness()
-
Applies a linear multiplier to the input image, making it appear more or less bright. Values are linear multipliers on the effect, with
0%
creating a completely black image,100%
having no effect, and values over100%
brightening the image.cssfilter: brightness(2);
contrast()
-
Adjusts the contrast of the input image. A value of
0%
makes the image grey,100%
has no effect, and values over100%
create a contrast.cssfilter: contrast(200%);
drop-shadow()
-
Applies the parameter
<shadow>
as a drop shadow, following the contours of the image. The shadow syntax is similar to<box-shadow>
(defined in the CSS backgrounds and borders module), with the exception that theinset
keyword andspread
parameter are not allowed. As with allfilter
property values, any filters after thedrop-shadow()
are applied to the shadow.cssfilter: drop-shadow(16px 16px 10px black);
grayscale()
-
Converts the image to grayscale. A value of
100%
is completely grayscale. The initial value of0%
leaves the input unchanged. Values between0%
and100%
produce linear multipliers on the effect.cssfilter: grayscale(100%);
hue-rotate()
-
Applies a hue rotation. The
<angle>
value defines the number of degrees around the hue color circle at which the input samples will be adjusted. A value of0deg
leaves the input unchanged.cssfilter: hue-rotate(90deg);
invert()
-
Inverts the samples in the input image. A value of
100%
completely inverts the image. A value of0%
leaves the input unchanged. Values between0%
and100%
have linear multipliers on the effect.cssfilter: invert(100%);
opacity()
-
Applies transparency.
0%
makes the image completely transparent and100%
leaves the image unchanged.cssfilter: opacity(50%);
saturate()
-
Saturates the image, with
0%
being completely unsaturated,100%
leaving the image unchanged, and values of over100%
increasing saturation.cssfilter: saturate(200%);
sepia()
-
Converts the image to sepia, with a value of
100%
making the image completely sepia and0%
making no change.cssfilter: sepia(100%);
Combining functions
You may combine any number of functions to manipulate the rendering. The filters are applied in the order declared. The following example enhances the contrast and brightness of the image:
filter: contrast(175%) brightness(103%);
Interpolation
When animated, if both the beginning and end filters have a function list of the same length without <url>
in the same order, each of their filter functions is interpolated according to the filter function's specific rules.
If the filter lists are of different lengths, the missing equivalent filter functions from the longer list are added to the end of the shorter list. The added functions use their initial, no filter modification values. All the filters listed are then interpolated according to the filter function's specific rules. Otherwise, discrete interpolation is used.
Formal definition
Initial value | none |
---|---|
Applies to | all elements; In SVG, it applies to container elements excluding the <defs> element and all graphics elements |
Inherited | no |
Computed value | as specified |
Animation type | a filter function list |
Formal syntax
filter =
none |
<filter-value-list>
<filter-value-list> =
[ <filter-function> | <url> ]+
<filter-function> =
<blur()> |
<brightness()> |
<contrast()> |
<drop-shadow()> |
<grayscale()> |
<hue-rotate()> |
<invert()> |
<opacity()> |
<sepia()> |
<saturate()>
<url> =
<url()> |
<src()>
<blur()> =
blur( <length>? )
<brightness()> =
brightness( [ <number> | <percentage> ]? )
<contrast()> =
contrast( [ <number> | <percentage> ]? )
<drop-shadow()> =
drop-shadow( [ <color>? && <length>{2,3} ] )
<grayscale()> =
grayscale( [ <number> | <percentage> ]? )
<hue-rotate()> =
hue-rotate( [ <angle> | <zero> ]? )
<invert()> =
invert( [ <number> | <percentage> ]? )
<opacity()> =
opacity( [ <number> | <percentage> ]? )
<sepia()> =
sepia( [ <number> | <percentage> ]? )
<saturate()> =
saturate( [ <number> | <percentage> ]? )
<url()> =
url( <string> <url-modifier>* ) |
<url-token>
<src()> =
src( <string> <url-modifier>* )
Examples
Applying filter functions
The filter
property is applied to the second image, greying and blurring both the image and its border.
img {
border: 5px solid yellow;
}
/* Gray the second image by 40% and blur by 5px */
img:nth-of-type(2) {
filter: grayscale(0.4) blur(5px);
}
<img src="pencil.jpg" alt="Original image is sharp" />
<img src="pencil.jpg" alt="The image and border are blurred and muted" />
Repeating filter functions
Filter functions are applied in order of appearance. The same filter function can be repeated.
#MDN-logo {
border: 1px solid blue;
filter: drop-shadow(5px 5px 0 red) hue-rotate(180deg)
drop-shadow(5px 5px 0 red);
}
The filters are applied in order. This is why the drop shadows are not the same color: the first drop shadow's hue is altered by the hue-rotate()
function but the second one is not.
Specifications
Specification |
---|
Filter Effects Module Level 1 # FilterProperty |
Browser compatibility
BCD tables only load in the browser
See also
- CSS
backdrop-filter
property - CSS compositing and blending module, including the CSS
background-blend-mode
andmix-blend-mode
properties. - The CSS
mask
property - SVG, including the SVG
<filter>
element and SVGfilter
attribute. - Applying SVG effects to HTML content