CSS filter Attribute
- Previous page empty-cells
- Next Page flex
Definition and usage
The filter property defines the visual effects of an element (usually <img>) such as blur and saturation.
See also:
CSS Tutorial:CSS Images
HTML DOM Reference Manual:filter property
Example
Convert all images to black and white (100% gray):
img { filter: grayscale(100%); }
Tip:More TIY examples can be found at the bottom of the page.
CSS syntax
filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();
Tip:If multiple filters are to be used, separate each filter with a space (see more examples below the page).
Technical details
Default value: | none |
---|---|
Inheritance: | No |
Animation production: | Supported. See:Animation-related properties. |
Version: | CSS3 |
JavaScript syntax: | object.style.filter="grayscale(100%)" |
Filter function
Note:Use percentage values (e.g., 75%) for filters, and also accept the value as a decimal (e.g., 0.75).
Filter | Description |
---|---|
none | Default value. Specifies no effect. |
blur(px) |
Apply a blur effect to the image. Larger values will produce more blur. If a specified value is used, use 0. |
brightness(%) |
Adjust the brightness of the image.
|
contrast(%) |
Adjust the contrast of the image.
|
drop-shadow(h-shadow v-shadow blur spread color) |
Apply a shadow effect to the image. Possible values:
blur - Optional. This is the third value, which must be in pixels. Adds a blur effect to the shadow. The larger the value, the more blur is created (the shadow will become larger and brighter). Negative values are not allowed. If a value is not specified, 0 will be used (the edge of the shadow will be very sharp). spread - Optional. This is the fourth value, which must be in pixels. A positive value will cause the shadow to expand and increase, while a negative value will cause the shadow to shrink. If a value is not specified, 0 will be used (the shadow will be the same size as the element). Note:Chrome, Safari, and Opera, and possibly other browsers, do not support the fourth length; if added, it will not be displayed. color - Optional. Adds a color to the shadow. If not specified, the color depends on the browser (usually black). This example creates a red shadow with a horizontal and vertical length of 8px and a blur effect of 10px: filter: drop-shadow(8px 8px 10px red); Tip:This filter is similar to the box-shadow attribute. |
grayscale(%) |
Convert the image to grayscale.
Note:Negative values are not allowed. |
hue-rotate(deg) |
Apply a hue rotation to the image. This value defines the degrees of the color wheel. The default value is 0deg, representing the original image. Note:The maximum value is 360deg. |
invert(%) |
Invert the samples in the image.
Note:Negative values are not allowed. |
opacity(%) |
Set the opacity level of the image. The opacity-level describes the opacity level, where:
Note:Negative values are not allowed. Tip:This filter is similar to the opacity attribute. |
saturate(%) |
Set the saturation of the image.
Note:Negative values are not allowed. |
sepia(%) |
Convert the image to sepia.
Note:Negative values are not allowed. |
url() |
The url() function accepts the location of the XML file that specifies the SVG filter and can include an anchor pointing to a specific filter element. Example: filter: url(svg-url#element-id) |
initial | Set this property to its default value. See initial. |
inherit | Inherit this property from its parent element. See inherit. |
More examples
Blur example
Add blur effect to the image:
img { filter: blur(5px); }
Blur example 2
Apply blurred background image:
img.background { filter: blur(35px); }
Brightness example
Adjust the brightness of the image:
img { filter: brightness(200%); }
Contrast example
Adjust the contrast of the image:
img { filter: contrast(200%); }
Shadow example
Apply shadow effect to the image:
img { filter: drop-shadow(8px 8px 10px gray); }
Grayscale example
Convert the image to grayscale:
img { filter: grayscale(50%); }
Hue rotation example
Apply a hue rotation to the image:
img { filter: hue-rotate(90deg); }
Invert example
Invert the samples in the image:
img { filter: invert(100%); }
Opacity example
Set the opacity level of the image:
img { filter: opacity(30%); }
Saturation example
Adjust the saturation of the image:
img { filter: saturate(800%); }
Sepia example
Convert the image to sepia:
img { filter: sepia(100%); }
Use multiple filters
To use multiple filters, separate each filter with a space. Note that the order is important (for example, using grayscale() after sepia() will produce a completely gray image):
img { filter: contrast(200%) brightness(150%); }
All filters
Demonstrate all filter functions:
.blur { filter: blur(4px); } .brightness { filter: brightness(0.30); } .contrast { filter: contrast(180%); } .grayscale { filter: grayscale(100%); } .huerotate { filter: hue-rotate(180deg); } .invert { filter: invert(100%); } .opacity { filter: opacity(50%); } .saturate { filter: saturate(7); } .sepia { filter: sepia(100%); } .shadow { filter: drop-shadow(8px 8px 10px green); }
Browser support
The numbers in the table indicate the first browser version that fully supports this attribute.
Numbers with -webkit- indicate the first version using the prefix.
Chrome | IE / Edge | Firefox | Safari | Opera |
---|---|---|---|---|
53.0 18.0 -webkit- |
13.0 | 35.0 | 9.1 6.0 -webkit- |
40.0 15.0 -webkit- |
Note:Older versions of Internet Explorer (4.0 to 8.0) support the deprecated non-standard "filter" attribute. It is mainly used for opacity when supporting IE8 and lower versions.
- Previous page empty-cells
- Next Page flex