Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS3 filter property

CSS3 filter property

Example

Change the color of all images to black and white (100% grayscale):

img

{

   

-webkit-filter:

grayscale(100%);

/* Chrome, Safari, Opera */

   

filter:

grayscale(100%);

}

Try it »


Definition and usage

The filter property defines the element's (usually) visual effects (for example: blurring and saturation).

Default value: none
Inherited: no
Animation support: Yes. For details, see CSS Animations
Version: CSS3
JavaScript syntax: object.style.WebkitFilter="grayscale(100%)" Try it »

Browser support

The numbers in the table indicate the first browser version that supports this method.

The -webkit- immediately following a number is the prefix for the specified browser.

property
filter 18.0 -webkit- 13.0 35.0 6.0 -webkit- 15.0 -webkit-

Note: The non-standard "filter" property supported by older versions of the Internet Explorer browser (4.0 to 8.0) has been deprecated. Browsers IE8 and earlier usually use opacity attribute.

CSS Syntax


filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();

Tip: Separate multiple filters with spaces.


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(values of the type defined in CSS3 backgrounds), except that the "inset" keyword is not allowed. This function is very similar to the existing box-shadow box-shadow property; the difference is that, through the filter, some browsers provide hardware acceleration for better performance.参数如下:

(必须) 这是设置阴影偏移量的两个 值. 设定水平方向距离. 负值会使阴影出现在元素左边. 设定垂直距离.负值会使阴影出现在元素上方。查看可能的单位. 如果两个值都是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 |


More examples

Blur example

Apply a Gaussian blur effect to the image:

img

{

   

-webkit-filter:

blur(5px);

/* Chrome, Safari, Opera */

   

filter:

blur(5px);

}

Try it »

Brightness function example

Brighten the image:

img

{

   

-webkit-filter:

brightness(200%);

/* Chrome, Safari, Opera */

   

filter:

brightness(200%);

}

Try it »

Contrast function example

Adjust the contrast of the image:

img

{

   

-webkit-filter:

contrast(200%);

/* Chrome, Safari, Opera */

   

filter:

contrast(200%);

}

Try it »

drop-shadow function example

Set a shadow effect on the image:

img

{

   

-webkit-filter:

drop-shadow(8px 8px 10px red);

/* Chrome, Safari, Opera */

   

filter:

drop-shadow(8px 8px 10px red);

}

Try it »

Grayscale function example

Convert the image to grayscale:

img

{

   

-webkit-filter:

grayscale(50%);

/* Chrome, Safari, Opera */

   

filter:

grayscale(50%);

}

Try it »

hue-rotate() function example

Apply a hue rotation to the image:

img

{

   

-webkit-filter:

hue-rotate(90deg);

/* Chrome, Safari, Opera */

   

filter:

hue-rotate(90deg);

}

Try it »

Invert function example

Invert the input image:

img

{

   

-webkit-filter:

invert(100%);

/* Chrome, Safari, Opera */

   

filter:

invert(100%);

}

Try it »

Opacity function example

Change the transparency level of the image:

img

{

   

-webkit-filter:

opacity(30%);

/* Chrome, Safari, Opera */

   

filter:

opacity(30%);

}

Try it »

Saturate function example

Change the saturation of the image:

img

{

   

-webkit-filter:

saturate(800%);

/* Chrome, Safari, Opera */

   

filter:

saturate(800%);

}

Try it »

Sepia function example

Convert the image to sepia:

img

{

   

-webkit-filter:

sepia(100%);

/* Chrome, Safari, Opera */

   

filter:

sepia(100%);

}

Try it »

Combining functions

Use multiple filters, separating each filter with a space.

Note: The order is very important (for example, using grayscale() before sepia() will produce a completely grayscale image).

img

{

   

-webkit-filter:

 contrast(200%) brightness(150%);

 

/* Chrome, Safari, Opera */

   

filter:

contrast(200%) brightness(150%);

}

Try it »

All filters example

The following example demonstrates how to use all the filters:

.blur

{

   

-webkit-filter:

blur(4px);

   

filter:

blur(4px);

}

.brightness

{

   

-webkit-filter:

brightness(0.30);

   

filter:

brightness(0.30);

}

.contrast

{

   

-webkit-filter:

contrast(180%);

   

filter:

contrast(180%);

}

.grayscale

{

   

-webkit-filter:

grayscale(100%);

   

filter:

grayscale(100%);

}

.huerotate

{

   

-webkit-filter:

hue-rotate(180deg);

   

filter:

hue-rotate(180deg);

}

.invert

{

   

-webkit-filter:

invert(100%);

   

filter:

invert(100%);

}

.opacity

{

   

-webkit-filter:

opacity(50%);

   

filter:

opacity(50%);

}

.saturate

{

   

-webkit-filter:

saturate(7);

   

filter:

saturate(7);

}

.sepia

{

   

-webkit-filter:

sepia(100%);

   

filter:

sepia(100%);

}

.shadow

{

   

-webkit-filter:

drop-shadow(8px 8px 10px green);

   

filter:

drop-shadow(8px 8px 10px green);

}

Try it »

Related Articles

HTML DOM Reference Manual: Style filter Property