在线文档教程

过滤器 | filter

filter

filterCSS属性允许您对元素应用图形效果,如模糊、锐化或颜色转移。过滤器通常用于调整图像、背景和边框的渲染。

CSS标准中包含了几个实现预定义效果的功能。还可以将带有URL的SVG筛选器引用到SVG滤波元件...

/* Function values */ filter: url("filters.svg#filter-id" 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% /* Apply multiple filters */ filter: contrast(175%) brightness(3% /* Global values */ filter: inherit; filter: initial; filter: unset;

初始值没有
适用于所有元素; 在SVG中,它适用于不包含<defs>元素和所有图形元素的容器元素
遗传没有
媒体视觉
计算值作为指定
动画类型一个过滤函数列表
规范的顺序形式语法定义的独特的非模糊顺序

句法

对于函数,请使用以下内容:

filter: <filter-function> [<filter-function>]* | none

对于SVG的引用<filter>元素,请使用以下内容:

filter: url(file.svg#filter-element-id)

插值

如果两个筛选器的函数列表具有相同的长度而没有 <url>, 则它们的每个筛选器函数都根据其特定规则进行插值。如果它们具有不同的长度, 则从较长列表中丢失的等效筛选器函数将使用其空隙值添加到较短列表的末尾, 然后根据它们的特定规则对所有筛选函数进行内插。如果一个筛选器为 "none", 则使用筛选器函数默认值将其替换为另一个过滤器函数列表, 然后根据它们的特定规则对所有筛选函数进行插值。否则, 使用离散插值。

形式语法

none | <filter-function-list>where <filter-function-list> = [ <filter-function> | <url> ]+ where <filter-function> = <blur()> | <brightness()> | <contrast()> | <drop-shadow()> | <grayscale()> | <hue-rotate()> | <invert()> | <opacity()> | <sepia()> | <saturate()> where <blur()> = blur( <length> ) <brightness()> = brightness( <number-percentage> ) <contrast()> = contrast( [ <number-percentage> ] ) <drop-shadow()> = drop-shadow( <length>{2,3} <color>? ) <grayscale()> = grayscale( <number-percentage> ) <hue-rotate()> = hue-rotate( <angle> ) <invert()> = invert( <number-percentage> ) <opacity()> = opacity( [ <number-percentage> ] ) <sepia()> = sepia( <number-percentage> ) <saturate()> = saturate( <number-percentage> ) where <number-percentage> = <number> | <percentage> <color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color> where <rgb()> = rgb( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] ) <rgba()> = rgba( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] ) <hsl()> = hsl( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] ) <hsla()> = hsla( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] ) where <alpha-value> = <number> | <percentage> <hue> = <number> | <angle>

实例

下面显示了使用预定义函数的示例。参见每个函数的特定示例。

.mydiv { filter: grayscale(50%) } /* gray all images by 50% and blur by 10px */ img {   filter: grayscale(0.5) blur(10px }

下面显示了在SVG资源中使用URL函数的示例。

.target { filter: url(#c1 } .mydiv { filter: url(commonfilters.xml#large-blur) }

函数

若要使用 "CSS 筛选器" 属性, 请为下列函数之一指定一个值。如果该值无效, 则该函数返回 "none"。除注意到的情况外, 采用以百分号表示的值 (如 34%) 的函数也接受以十进制表示的值 (如0.34 所示)。

url()

url()函数获取指定SVG过滤器的XML文件的位置,并且可能包含特定过滤器元素的锚点。

filter: url(resources.svg#c1)

blur()

将高斯模糊应用于输入图像。“半径”的值定义了高斯函数的标准偏差值,或者屏幕上有多少像素相互混合,所以较大的值会产生更多的模糊。插值的空缺值是0。该参数被指定为CSS长度,但不接受百分比值。

filter: blur(5px)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form.jpg" id="img1" class="internal default" src="/files/3710/Test_Form_2.jpg" style="width: 100%;" /></td> <td><img alt="Test_Form.jpg" id="img2" class="internal default" src="/files/3710/Test_Form_2.jpg" style="width: 100%;" /></td> <td> <div class="svg-container"> <svg id="img3" overflow="visible" viewbox="0 0 212 161" color-interpolation-filters="sRGB"> <filter id="svgBlur" x="-5%" y="-5%" width="110%" height="110%"> <feGaussianBlur in="SourceGraphic" stdDeviation="3.5"/> </filter> <image xlink:href="/files/3710/Test_Form_2.jpeg" filter="url(#svgBlur)" width="212px" height="161px"/> </svg> </div> </td> <td><img alt="Test_Form_s.jpg" id="img4" class="internal default" src="/files/3711/Test_Form_2_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande", "Lucida Sans Unicode", "DejaVu Sans", Lucida, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -webkit-filter:blur(5px -ms-filter:blur(5px filter:blur(5px } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0; margin: 0 0 1.286em; height: 100%; width: 85%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

<svg style="position: absolute; top: -99999px" xmlns="http://www.w3.org/2000/svg"> <filter id="svgBlur" x="-5%" y="-5%" width="110%" height="110%"> <feGaussianBlur in="SourceGraphic" stdDeviation="5"/> </filter> </svg>

brightness()

将线性乘法器应用于输入图像,使其看起来或多或少明亮。值0%将创建一个完全黑色的图像。值100%保持输入不变。其他数值则是线性乘数对效应的影响。超过某数额的值100%是允许的,提供更好的结果。插值的空白值是1.

filter: brightness(0.5)

<svg style="position: absolute; top: -99999px" xmlns="http://www.w3.org/2000/svg"> <filter id="brightness"> <feComponentTransfer> <feFuncR type="linear" slope="[amount]"/> <feFuncG type="linear" slope="[amount]"/> <feFuncB type="linear" slope="[amount]"/> </feComponentTransfer> </filter> </svg>

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form.jpg" id="img1" class="internal default" src="/files/3708/Test_Form.jpg" style="width: 100%;" /></td> <td><img alt="Test_Form.jpg" id="img2" class="internal default" src="/files/3708/Test_Form.jpg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 286 217" color-interpolation-filters="sRGB"> <filter id="brightness"> <feComponentTransfer> <feFuncR type="linear" slope="2"/> <feFuncG type="linear" slope="2"/> <feFuncB type="linear" slope="2"/> </feComponentTransfer> </filter>   <image xlink:href="/files/3708/Test_Form.jpg" filter="url(#brightness)" width="286px" height="217px" /> </svg><div></td> <td><img alt="Test_Form_s.jpg" id="img4" class="internal default" src="/files/3709/Test_Form_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter:brightness(2 -webkit-filter:brightness(2 -o-filter:brightness(2 -ms-filter:brightness(2 filter:brightness(2 } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; height:100%; width: 85%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

contrast()

调整输入的对比度。值0%将创建一个完全灰色的图像。值为100%输入时不会被改变。超过100%的值也是允许的,提供更多对比的结果。插值的空白值是1.

filter: contrast(200%)

<svg style="position: absolute; top: -99999px" xmlns="http://www.w3.org/2000/svg"> <filter id="contrast"> <feComponentTransfer> <feFuncR type="linear" slope="[amount]" intercept="-(0.5 * [amount]) + 0.5"/> <feFuncG type="linear" slope="[amount]" intercept="-(0.5 * [amount]) + 0.5"/> <feFuncB type="linear" slope="[amount]" intercept="-(0.5 * [amount]) + 0.5"/> </feComponentTransfer> </filter> </svg>

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_3.jpeg" id="img1" class="internal default" src="/files/3712/Test_Form_3.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_3.jpg" id="img2" class="internal default" src="/files/3712/Test_Form_3.jpeg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 240 151" color-interpolation-filters="sRGB"> <filter id="contrast"> <feComponentTransfer> <feFuncR type="linear" slope="2" intercept="-0.5"/> <feFuncG type="linear" slope="2" intercept="-0.5"/> <feFuncB type="linear" slope="2" intercept="-0.5"/> </feComponentTransfer> </filter>   <image xlink:href="/files/3712/Test_Form_3.jpeg" filter="url(#contrast)" width="240px" height="151px" /> </svg><div></td> <td><img alt="Test_Form_s.jpg" id="img4" class="internal default" src="/files/3713/Test_Form_3_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter:contrast(200% -webkit-filter:contrast(200% -o-filter:contrast(200% -ms-filter:contrast(200% filter:contrast(200% } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

drop-shadow()

将投影效果应用于输入图像。投影实际上是一个模糊的, 偏移版本的输入。图像的 alpha 掩码由一个特定的颜色绘制,在图像下方合成。该函数接受一个类型为 <shadow> (在 CSS3 背景中定义) 的参数, 但不允许使用 "inset" 关键字。此函数类似于box-shadow属性;不同的是, 对于过滤器, 一些浏览器提供硬件加速以获得更好的性能。<shadow> 参数的参数如下所示。

<offset-x> <offset-y> (必需) 这些是两个 <length> 值, 用于设置阴影偏移量。<offset-x> 指定水平距离。负值将阴影放在元素的左侧。<offset-y> 指定垂直距离。负值将阴影置于元素上方。其他可能的元素, 请参见 <length>。

如果两个值都为0,则将阴影放在元素后面(如果设置了<blur-radius>和/或<spread-radius>, 则可能产生模糊效果).<blur-radius>(可选的)这是第三个 <length> 值。这个值越大, 模糊就越大, 所以阴影变得越来越轻。如果没有指定,不允许使用负值。 它将是 0 (阴影的边缘是尖锐的)。<spread-radius> (可选) 这是第四 <length> 值。正值会导致阴影扩展和增大, 负值会导致阴影缩小。如果未指定, 则为 0 (阴影将与元素的大小相同)。

注: Webkit, 也许其他浏览器, 不支持这第四长度;如果添加, 则不会呈现。<color> (可选) 请参见 <color> 可能的关键字和符号的值。如果未指定, 则颜色取决于浏览器。在火狐、Opera和ie中, 使用了颜色属性的值。另一方面, WebKit 的阴影是透明的, 因此如果省略 <color>, 则无用。

filter: drop-shadow(16px 16px 10px black)

<svg style="position: absolute; top: -999999px" xmlns="http://www.w3.org/2000/svg"> <filter id="drop-shadow"> <feGaussianBlur in="SourceAlpha" stdDeviation="[radius]"/> <feOffset dx="[offset-x]" dy="[offset-y]" result="offsetblur"/> <feFlood flood-color="[color]"/> <feComposite in2="offsetblur" operator="in"/> <feMerge> <feMergeNode/> <feMergeNode in="SourceGraphic"/> </feMerge> </filter> </svg>

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_4.jpeg" id="img1" class="internal default" src="/files/3714/Test_Form_4.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_4.jpg" id="img2" class="internal default" src="/files/3714/Test_Form_4.jpeg" style="width: 100%;" /></td> <td> <div class="svg-container"> <svg xmlns="http://www.w3.org/2000/svg" id="img3" overflow="visible" viewbox="0 0 213 161" color-interpolation-filters="sRGB"> <defs> <image id="MyImage" xlink:href="/files/3714/Test_Form_4.jpeg" width="213px" height="161px"/> </defs> <filter id="drop-shadow" x="-50%" y="-50%" width="200%" height="200%"> <feOffset dx="9" dy="9" in="SourceAlpha"/> <feGaussianBlur stdDeviation="5"/> </filter> <use xlink:href="#MyImage" filter="url(#drop-shadow)"/> <use xlink:href="#MyImage"/> </svg> </div> </td> <td><img alt="Test_Form_4_s.jpg" id="img4" class="internal default" src="/files/3715/Test_Form_4_s.jpg" style="width: 100%;" /></td> </tr> <tr>       <td><img alt="Test_Form_4 distorded border - Original image" id="img11" class="internal default" src="/files/8467/Test_Form_4_irregular-shape_opacity-gradient.png" style="width: 100%;" /></td>       <td><img alt="Test_Form_4 distorded border - Live example" id="img12" class="internal default" src="/files/8467/Test_Form_4_irregular-shape_opacity-gradient.png" style="width: 100%;" /></td> <td> <div class="svg-container"> <svg xmlns="http://www.w3.org/2000/svg" id="img13" overflow="visible" viewbox="0 0 213 161" color-interpolation-filters="sRGB"> <defs> <image id="MyImage2" xlink:href="/files/8467/Test_Form_4_irregular-shape_opacity-gradient.png" width="213px" height="161px"/> </defs> <filter id="drop-shadow2" x="-50%" y="-50%" width="200%" height="200%"> <feOffset dx="5" dy="5.5" in="SourceAlpha"/> <feGaussianBlur stdDeviation="2.5"/> <feComponentTransfer> <feFuncA type="table" tableValues="0 0.8"/> </feComponentTransfer> </filter> <use xlink:href="#MyImage2" filter="url(#drop-shadow2)"/> <use xlink:href="#MyImage2"/> </svg> </div> </td>       <td><img alt="Test_Form_4 distorded border drop shadow - Static example" id="img14" class="internal default" src="/files/8469/Test_Form_4_irregular-shape_opacity-gradient_drop-shadow.png" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter: drop-shadow(16px 16px 10px black -webkit-filter: drop-shadow(16px 16px 10px black -o-filter: drop-shadow(16px 16px 10px black -ms-filter: drop-shadow(16px 16px 10px black filter: drop-shadow(16px 16px 10px black } #img12 { width:100%; height:auto; -moz-filter: drop-shadow(8px 9px 5px rgba(0,0,0,.8) -webkit-filter: drop-shadow(8px 9px 5px rgba(0,0,0,.8) -o-filter: drop-shadow(8px 9px 5px rgba(0,0,0,.8) -ms-filter: drop-shadow(8px 9px 5px rgba(0,0,0,.8) filter: drop-shadow(8px 9px 5px rgba(0,0,0,.8) } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } #irregular-shape { width: 64%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3, #img13 { width:100%; height:auto; }

grayscale()

将图像转换为灰度图像。值定义转换的比例。值为100%则完全转为灰度图像,值为0%图像无变化。值在0%到100%之间,则是效果的线性乘子。若未设置,值默认是0。

filter: grayscale(100%)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_5.jpeg" id="img1" class="internal default" src="/files/3716/Test_Form_5.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_5.jpg" id="img2" class="internal default" src="/files/3716/Test_Form_5.jpeg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 276 184" color-interpolation-filters="sRGB"> <filter id="grayscale"> <feColorMatrix type="matrix" values="0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0 0 0 1 0"/> </filter>   <image xlink:href="/files/3716/Test_Form_5.jpeg" filter="url(#grayscale)" width="276px" height="184px" /> </svg><div></td> <td><img alt="Test_Form_5_s.jpg" id="img4" class="internal default" src="/files/3717/Test_Form_5_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter:grayscale(100% -webkit-filter:grayscale(100% -o-filter:grayscale(100% -ms-filter:grayscale(100% filter:grayscale(100% } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

hue-rotate()

给图像应用色相旋转。“angle”一值设定图像会被调整的色环角度值。值为0deg,则图像无变化。若值未设置,默认值是0deg。该值虽然没有最大值,超过360deg的值相当于又绕一圈。

filter: hue-rotate(90deg)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_6.jpeg" id="img1" class="internal default" src="/files/3718/Test_Form_6.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_6.jpg" id="img2" class="internal default" src="/files/3718/Test_Form_6.jpeg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 266 190" color-interpolation-filters="sRGB"> <filter id="hue-rotate"> <feColorMatrix type="hueRotate" values="90"/> </filter>   <image xlink:href="/files/3718/Test_Form_6.jpeg" filter="url(#hue-rotate)" width="266px" height="190px" /> </svg><div></td> <td><img alt="Test_Form_6_s.jpg" id="img4" class="internal default" src="/files/3719/Test_Form_6_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter:hue-rotate(90deg -webkit-filter:hue-rotate(90deg -o-filter:hue-rotate(90deg -ms-filter:hue-rotate(90deg filter:hue-rotate(90deg } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

<svg style="position: absolute; top: -999999px" xmlns="http://www.w3.org/2000/svg"> <filter id="svgHueRotate" > <feColorMatrix type="hueRotate" values="[angle]" /> <filter /> </svg>

invert()

反转输入图像。值定义转换的比例。100%的价值是完全反转。值为0%则图像无变化。值在0%和100%之间,则是效果的线性乘子。 若值未设置,值默认是0。

filter: invert(100%)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_7.jpeg" id="img1" class="internal default" src="/files/3720/Test_Form_7.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_7.jpg" id="img2" class="internal default" src="/files/3720/Test_Form_7.jpeg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 183 276" color-interpolation-filters="sRGB"> <filter id="invert"> <feComponentTransfer> <feFuncR type="table" tableValues="1 0"/> <feFuncG type="table" tableValues="1 0"/> <feFuncB type="table" tableValues="1 0"/> </feComponentTransfer> </filter> <image xlink:href="/files/3720/Test_Form_7.jpeg" filter="url(#invert)" width="183px" height="276px" /> </svg><div></td> <td><img alt="Test_Form_7_s.jpg" id="img4" class="internal default" src="/files/3721/Test_Form_7_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter: invert(100% -webkit-filter: invert(100% -o-filter: invert(100% -ms-filter: invert(100% filter: invert(100% } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

opacity()

转化图像的透明程度。值定义转换的比例。值为0%则是完全透明,值为100%则图像无变化。值在0%和100%之间,则是效果的线性乘子,也相当于图像样本乘以数量。 若值未设置,值默认是1。该函数与已有的opacity属性很相似,不同之处在于通过filter,一些浏览器为了提升性能会提供硬件加速。

filter: opacity(50%)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_14.jpeg" id="img1" class="internal default" src="/files/3725/Test_Form_14.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_14.jpg" id="img2" class="internal default" src="/files/3725/Test_Form_14.jpeg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 276 183" color-interpolation-filters="sRGB"> <filter id="opacity"> <feComponentTransfer> <feFuncA type="table" tableValues="0 0.5"> </feComponentTransfer> </filter> <image xlink:href="/files/3725/Test_Form_14.jpeg" filter="url(#opacity)" width="276px" height="183px" /> </svg><div></td> <td><img alt="Test_Form_14_s.jpg" id="img4" class="internal default" src="/files/3726/Test_Form_14_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter: opacity(50% -webkit-filter: opacity(50% -o-filter: opacity(50% -ms-filter: opacity(50% filter: opacity(50% } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

saturate()

转换图像饱和度。值定义转换的比例。值为0%则是完全不饱和,值为100%则图像无变化。其他值,则是效果的线性乘子。超过100%的值是允许的,则有更高的饱和度。 若值未设置,值默认是1。

filter: saturate(200%)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_9.jpeg" id="img1" class="internal default" src="/files/3722/Test_Form_9.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_9.jpg" id="img2" class="internal default" src="/files/3722/Test_Form_9.jpeg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 201 239" color-interpolation-filters="sRGB"> <filter id="saturate"> <feColorMatrix type="saturate" values="2"/> </filter> <image xlink:href="/files/3722/Test_Form_9.jpeg" filter="url(#saturate)" width="201px" height="239px" /> </svg><div></td> <td><img alt="Test_Form_9_s.jpg" id="img4" class="internal default" src="/files/3724/Test_Form_9_s.jpeg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter: saturate(200% -webkit-filter: saturate(200% -o-filter: saturate(200% -ms-filter: saturate(200% filter: saturate(200% } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

sepia()

将图像转换为深褐色。值定义转换的比例。值为100%则完全是深褐色的,值为0%图像无变化。值在0%到100%之间,则是效果的线性乘子。若未设置,值默认是0。

filter: sepia(100%)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">SVG Equivalent</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_12.jpeg" id="img1" class="internal default" src="/files/3727/Test_Form_12.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_12.jpg" id="img2" class="internal default" src="/files/3727/Test_Form_12.jpeg" style="width: 100%;" /></td> <td><div class="svg-container"><svg xmlns="http://www.w3.org/2000/svg" id="img3" viewbox="0 0 259 194" color-interpolation-filters="sRGB"> <filter id="sepia"> <feColorMatrix type="matrix" values="0.393 0.769 0.189 0 0 0.349 0.686 0.168 0 0 0.272 0.534 0.131 0 0 0 0 0 1 0"/> </filter> <image xlink:href="/files/3727/Test_Form_12.jpeg" filter="url(#sepia)" width="259px" height="194px" /> </svg><div></td> <td><img alt="Test_Form_12_s.jpg" id="img4" class="internal default" src="/files/3728/Test_Form_12_s.jpg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter: sepia(100% -webkit-filter: sepia(100% -o-filter: sepia(100% -ms-filter: sepia(100% filter: sepia(100% } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

组合功能

你可以组合任意数量的函数来控制渲染。下面的例子可以增强图像的对比度和亮度。

filter: contrast(175%) brightness(103%)

<table class="standard-table"> <thead> <tr> <th align="left" scope="col">Original image</th> <th align="left" scope="col">Live example</th> <th align="left" scope="col">Static example</th> </tr> </thead> <tbody> <tr> <td><img alt="Test_Form_8.jpeg" id="img1" class="internal default" src="/files/3729/Test_Form_8.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_8.jpg" id="img2" class="internal default" src="/files/3729/Test_Form_8.jpeg" style="width: 100%;" /></td> <td><img alt="Test_Form_8_s.jpg" id="img4" class="internal default" src="/files/3730/Test_Form_8_s.jpeg" style="width: 100%;" /></td> </tr> </tbody> </table>

html { height:100%; } body { font: 14px/1.286 "Lucida Grande","Lucida Sans Unicode","DejaVu Sans",Lucida,Arial,Helvetica,sans-serif; color: rgb(51, 51, 51 height:100%; overflow:hidden; } #img2 { width:100%; height:auto; -moz-filter: contrast(175%) brightness(103% -webkit-filter: contrast(175%) brightness(103% -o-filter: contrast(175%) brightness(103% -ms-filter: contrast(175%) brightness(103% filter: contrast(175%) brightness(103% } table.standard-table { border: 1px solid rgb(187, 187, 187 border-collapse: collapse; border-spacing: 0px; margin: 0px 0px 1.286em; width: 85%; height: 100%; } table.standard-table th { border: 1px solid rgb(187, 187, 187 padding: 0px 5px; background: none repeat scroll 0% 0% rgb(238, 238, 238 text-align: left; font-weight: bold; } table.standard-table td { padding: 5px; border: 1px solid rgb(204, 204, 204 text-align: left; vertical-align: top; width:25%; height:auto; } #img3 { height:100%; }

规范

SpecificationStatusComment
Filter Effects Module Level 1The definition of 'filter' in that specification.Working DraftInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support18.0-webkit1 53.0 Unprefixed(Yes)-webkit (Yes)35 (35)2No support315.0-webkit 40.0 Unprefixed6.0-webkit
On SVG ElementsNo support4(Yes)35 (35)No supportNo supportNo support

FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support4.4-webkit53.0 Unprefixed(Yes)-webkit (Yes)35 (35)2No support22.0-webkit 40.0 Unprefixed6.0 (Yes)-webkit53.0 Unprefixed
On SVG ElementsNo support4?(Yes)35 (35)No supportNo supportNo support?

在Chrome 18至19中,saturate()函数仅采用整数而不是小数或百分比值。此错误在Chrome 20及更高版本中得到修复。

在Firefox 34之前,Gecko只实现了url(),所以只url()允许一个url()(或者当layout.css.filters.enabledpref设置为false)。

filter的函数值由layout.css.filters.enabledpref 控制并且使用,但在Firefox 34中默认禁用。

除了无前缀的支持外,Gecko 46.0(Firefox 46.0 / Thunderbird 46.0 / SeaMonkey 2.43)还增加了对该属性的-webkit前缀版本的支持,以满足Web兼容性,因为 layout.css.prefixes.webkit的默认值false。从Gecko 49.0开始(Firefox 49.0 / Thunderbird 49.0 / SeaMonkey 2.46),该选项首选项默认为true

Internet Explorer 4.0到9.0实现了非标准filter属性。语法与此完全不同,这里没有记录。

目前没有实现此功能。查看错误109224

另见