Style filterProperty
Examples
Change the image color to black and white (100% grayscale):
document.getElementById("myImg").style.WebkitFilter = "grayscale(100%)";
// Standard syntax (not supported by other major browsers)
document.getElementById("myImg").style.filter = "grayscale(100%)";
Try it »
Definition and Usage
The filter property is used to define the visual effects of an element (usually <img>).
Note:If the element is not a flexible item, the flexGrow property has no effect.
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
| Property | |||||
|---|---|---|---|---|---|
| filter | 18.0 Webkit | Not supported | Not supported | 6.0 Webkit | 15.0 Webkit |
Note:Chrome, Safari and Opera use the WebkitFilter property instead.
Syntax
Return the filter property:
object.style.filter
Set the filter property:
object.style.filter="none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia()"
Filter Functions
Note:Filters usually use percentages (e.g., 75%), but decimals can also be used (e.g., 0.75).
| Filter | Description |
|---|---|
| none | Default value; no effect. |
| blur(px) | Applies a Gaussian blur to the image. The "radius" value sets the standard deviation of the Gaussian function, or how many pixels on the screen blend together, so the larger the value, the blurrier the image; If no value is set, it defaults to 0; this parameter can accept CSS length values, but not percentage values. |
| brightness(%) | Applies a linear multiplier to the image, making it appear brighter or darker. If the value is 0%, the image will be completely black. A value of 100% leaves the image unchanged. Other values correspond to linear multiplier effects. Values over 100% are also allowed, making the image brighter than before. If no value is set, the default is 1. |
| contrast(%) | Adjusts the contrast of the image. A value of 0% makes the image completely black. A value of 100% leaves the image unchanged. Values over 100% are allowed, meaning lower contrast will be applied. If no value is set, the default is 1. |
| drop-shadow(h-shadow v-shadow blur spread color) | Applies a shadow effect to the image. The shadow is composited underneath the image, and can be a blurred offset version of a mask drawn in a specific color. The function accepts a value of type <shadow> (defined in CSS3 backgrounds), except that the "inset" keyword is not allowed. This function is very similar to the existing box-shadow property; the difference is that through filters, some browsers provide hardware acceleration for better performance.
|
| grayscale(%) | Converts the image to grayscale. The value defines the proportion of the conversion. A value of 100% completely converts the image to grayscale, and a value of 0% leaves the image unchanged. Values between 0% and 100% are linear multipliers of the effect. If not set, the value defaults to 0. |
| hue-rotate(deg) | Applies a hue rotation to the image. The "angle" value sets the hue angle by which the image will be adjusted. A value of 0deg leaves the image unchanged. If the value is not set, the default is 0deg. Although there is no maximum value, values over 360deg are equivalent to going around the wheel again. |
| invert(%) | Inverts the input image. The value defines the proportion of the conversion. A value of 100% is completely inverted. A value of 0% leaves the image unchanged. Values between 0% and 100% are linear multipliers of the effect. If the value is not set, the default is 0. |
| opacity(%) | Converts the transparency of the image. The value defines the proportion of the conversion. A value of 0% is completely transparent, and a value of 100% leaves the image unchanged. Values between 0% and 100% are linear multipliers of the effect, equivalent to multiplying the image samples by the amount. If the value is not set, the default is 1. This function is very similar to the existing opacity property; the difference is that through filter, some browsers provide hardware acceleration to improve performance. |
| saturate(%) | Converts the saturation of the image. The value defines the proportion of the conversion. A value of 0% is completely unsaturated, and a value of 100% leaves the image unchanged. Other values are linear multipliers of the effect. Values over 100% are allowed, providing higher saturation. If the value is not set, the default is 1. |
| sepia(%) | Converts the image to sepia. The value defines the proportion of the conversion. A value of 100% is completely sepia, and a value of 0% leaves the image unchanged. Values between 0% and 100% are linear multipliers of the effect. If not set, the value defaults to 0. |
| url() | The URL function accepts an XML file that sets an SVG filter, and can include an anchor to specify a specific filter element. For example: filter: url(svg-url#element-id) |
| initial | To set the property to its default value, see:CSS initial keyword |
| inherit | To inherit the property from its parent element, see:CSS inherit keyword |
Technical Details
| CSS Version | CSS3 |
|---|
Related Articles
CSS Reference:filter Property
Other Extensions
Style Object