Style filter Property
Example
Modify the image color to black and white (100% grayscale):
// Chrome, Safari and Opera browsers
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 is used to define the visual effect of an element (usually ) visual effect.
**Note:** If the element is not a flex item, the flexGrow property has no effect.
Browser support
The numbers in the table indicate the first browser version that supports this method.
| 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
Returns the filter property:
object.style.filter
Sets the filter property:
object.style.filter="none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia()"
Filter Functions
Note: Filters are usually specified using 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 screen blend together, so a larger value produces more blur; |
如果没有设定值,则默认是0;这个参数可设置css长度值,但不接受百分比值。 |
| brightness(%) | Applies a linear multiplication to the image to make it appear brighter or darker. If the value is 0%, the image becomes completely black. A value of 100% leaves the image unchanged. Other values apply a linear multiplier effect. Values above 100% are also allowed, making the image brighter than the original. If no value is set, the default is 1. | | contrast(%) | Adjusts the contrast of the image. If the value is 0%, the image becomes completely black. A value of 100% leaves the image unchanged. Values may exceed 100%, meaning lower contrast is 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 a composited, offset version of the mask drawn underneath the image, with optional blur and a specific color. The function accepts
(必须) 这是设置阴影偏移量的两个 值. 设定水平方向距离. 负值会使阴影出现在元素左边. 设定垂直距离.负值会使阴影出现在元素上方。查看可能的单位. 如果两个值都是0, 则阴影出现在元素正后面 (如果设置了 and/or ,会有模糊效果). (可选) 这是第三个code>值. 值越大,越模糊,则阴影会变得更大更淡.不允许负值 若未设定,默认是0 (则阴影的边界很锐利). (可选) 这是第四个 值. 正值会使阴影扩张和变大,负值会是阴影缩小.若未设定,默认是0 (阴影会与元素一样大小). 注意: Webkit, 以及一些其他浏览器 不支持第四个长度,如果加了也不会渲染。 (可选) 查看 该值可能的关键字和标记。若未设定,颜色值基于浏览器。在Gecko (Firefox), Presto (Opera)和Trident (Internet Explorer)中, 会应用colorcolor属性的值。另外, 如果颜色值省略,WebKit中阴影是透明的。 | | grayscale(%) |
Converts the image to grayscale. The value defines the proportion of the conversion. A value of 100% results in a fully grayscale image, while a value of 0% leaves the image unchanged. Values between 0% and 100% are linear multipliers of the effect. If not set, the default value is 0;
| | hue-rotate(deg) |Applies a hue rotation to the image. The "angle" value sets the angle on the color wheel by which the image is 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 above 360deg are equivalent to going around the wheel once more.
| | invert(%) |Inverts the input image. The value defines the proportion of the conversion. A value of 100% means full inversion. 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(%) |Adjusts the opacity of the image. The value defines the proportion of the conversion. A value of 0% makes it fully transparent, while 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 a number. 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 the filter, some browsers provide hardware acceleration for better performance.
| | saturate(%) |Adjusts the saturation of the image. The value defines the proportion of the conversion. A value of 0% results in complete desaturation, while a value of 100% leaves the image unchanged. Other values are linear multipliers of the effect. Values above 100% are allowed, producing 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% results in a fully sepia image, while a value of 0% leaves the image unchanged. Values between 0% and 100% are linear multipliers of the effect. If not set, the default value is 0;
| | url() |The URL function accepts an XML file that sets an SVG filter and may contain an anchor to specify a particular filter element.
For example:
filter: url(svg-url#element-id)| | initial |
Sets the property to its default value. See: CSS initial keyword
| | inherit | Inherits the property from the parent element. See:CSS inherit keyword |Technical Details
| CSS version | CSS3 |
|---|
Related Articles
CSS Reference Manual: filter Property
The Style object