Latest web development tutorials
×

CSS 參考手冊

CSS 參考手冊 CSS 選擇器 CSS 語音參考 CSS Web 安全字體 CSS 動畫 CSS 單位 CSS 顏色 CSS 合法顏色值 CSS 顏色名稱 CSS 顏色十六進制值 CSS 瀏覽器支持

CSS 屬性

align-content align-items align-self all animation animation-delay animation-direction animation-duration animation-fill-mode animation-iteration-count animation-name animation-play-state animation-timing-function appearance backface-visibility background background-attachment background-blend-mode background-clip background-color background-image background-origin background-position background-repeat background-size border border-bottom border-bottom-color border-bottom-left-radius border-bottom-right-radius border-bottom-style border-bottom-width border-collapse border-color border-image border-image-outset border-image-repeat border-image-slice border-image-source border-image-width border-left border-left-color border-left-style border-left-width border-radius border-right border-right-color border-right-style border-right-width border-spacing border-style border-top border-top-color border-top-left-radius border-top-right-radius border-top-style border-top-width border-width bottom box-align box-direction box-flex box-flex-group box-lines box-ordinal-group box-orient box-pack box-shadow box-sizing caption-side clear clip color column-count column-fill column-gap column-rule column-rule-color column-rule-style column-rule-width column-span column-width columns content counter-increment counter-reset cursor direction display empty-cells filter flex flex-basis flex-direction flex-flow flex-grow flex-shrink flex-wrap float font @font-face font-family font-size font-size-adjust font-stretch font-style font-variant font-weight grid-columns grid-rows hanging-punctuation height icon justify-content @keyframes left letter-spacing line-height list-style list-style-image list-style-position list-style-type margin margin-bottom margin-left margin-right margin-top max-height max-width @media min-height min-width nav-down nav-index nav-left nav-right nav-up opacity order outline outline-color outline-offset outline-style outline-width overflow overflow-x overflow-y padding padding-bottom padding-left padding-right padding-top page-break-after page-break-before page-break-inside perspective perspective-origin position punctuation-trim quotes resize right rotation tab-size table-layout target target-name target-new target-position text-align text-align-last text-decoration text-decoration-color text-decoration-line text-decoration-style text-indent text-justify text-outline text-overflow text-shadow text-transform text-wrap top transform transform-origin transform-style transition transition-delay transition-duration transition-property transition-timing-function unicode-bidi vertical-align visibility white-space width word-break word-spacing word-wrap z-index

CSS3 filter(濾鏡) 屬性

實例

修改所有圖片的顏色為黑白(100% 灰度):

img {
-webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */
filter: grayscale(100%);
}

嘗試一下»

定義和使用

filter 屬性定義了元素(通常是<img>)的可視效果(例如:模糊與飽和度)。

默認值: none
繼承: no
動畫支持: 是。 詳細可查閱CSS動畫
版本: CSS3
JavaScript 語法: object.style.WebkitFilter="grayscale(100%)" 嘗試一下»

瀏覽器支持

表格中的數字表示支持該方法的第一個瀏覽器的版本號。

緊跟在數字後面的-webkit- 為指定瀏覽器的前綴。

屬性
filter 18.0 -webkit- 不支持 35.0 6.0 -webkit- 15.0 -webkit-

注意:舊版Internet Explorer瀏覽器(4.0 to 8.0)支持的非標準"filter"屬性已被廢棄。 IE8及更低版本瀏覽器通常使用opacity屬性。

CSS 語法

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

提示:使用空格分隔多個濾鏡。


Filter 函數

注意:濾鏡通常使用百分比(如:75%),當然也可以使用小數來表示(如:0.75)。

Filter 描述
none 默認值,沒有效果。
blur( px ) 給圖像設置高斯模糊。 "radius"一值設定高斯函數的標準差,或者是屏幕上以多少像素融在一起, 所以值越大越模糊;

如果沒有設定值,則默認是0;這個參數可設置css長度值,但不接受百分比值。
brightness( % ) 給圖片應用一種線性乘法,使其看起來更亮或更暗。 如果值是0%,圖像會全黑。 值是100%,則圖像無變化。 其他的值對應線性乘數效果。 值超過100%也是可以的,圖像會比原來更亮。 如果沒有設定值,默認是1。
contrast( % ) 調整圖像的對比度。 值是0%的話,圖像會全黑。 值是100%,圖像不變。 值可以超過100%,意味著會運用更低的對比。 若沒有設置值,默認是1。
drop-shadow( h-shadow v-shadow blur spread color )

給圖像設置一個陰影效果。 陰影是合成在圖像下面,可以有模糊度的,可以以特定顏色畫出的遮罩圖的偏移版本。 函數接受<shadow>(在CSS3背景中定義)類型的值,除了"inset"關鍵字是不允許的。 該函數與已有的box-shadow box-shadow屬性很相似;不同之處在於,通過濾鏡,一些瀏覽器為了更好的性能會提供硬件加速。 <shadow>参数如下:

<offset-x> <offset-y> (必須)
這是設置陰影偏移量的兩個<length>值. <offset-x>設定水平方向距離.負值會使陰影出現在元素左邊. <offset-y>設定垂直距離.負值會使陰影出現在元素上方。 查看<length>可能的單位.
如果兩個值都是 0 ,則陰影出現在元素正後面(如果設置了   <blur-radius> and/or <spread-radius>, 会有模糊效果 ).
<blur-radius> (可选)这是第三个code><length>值. 值越大,越模糊,则阴影会变得更大更淡.不允许负值若未设定,默认是0 (则阴影的边界很锐利). <spread-radius> (可选)这是第四个<length>值. 正值会使阴影扩张和变大,负值会是阴影缩小.若未设定,默认是0 (阴影会与元素一样大小).
注意: Webkit, 以及一些其他浏览器不支持第四个长度,如果加了也不会渲染。 <color> (可选)查看<color>该值可能的关键字和标记。若未设定,颜色值基于浏览器。在Gecko (Firefox), Presto (Opera)和Trident (Internet Explorer)中, 会应用color color属性的值。另外, 如果颜色值省略,WebKit中阴影是透明的。
<blur-radius> (可选)这是第三个code><length>值. 值越大,越模糊,则阴影会变得更大更淡.不允许负值若未设定,默认是0 (则阴影的边界很锐利). <spread-radius> (可选)这是第四个<length>值. 正值会使阴影扩张和变大,负值会是阴影缩小.若未设定,默认是0 (阴影会与元素一样大小).
注意: Webkit, 以及一些其他浏览器不支持第四个长度,如果加了也不会渲染。 <color> (可选)查看<color>该值可能的关键字和标记。若未设定,颜色值基于浏览器。在Gecko (Firefox), Presto (Opera)和Trident (Internet Explorer)中, 会应用color color属性的值。另外, 如果颜色值省略,WebKit中阴影是透明的。
<blur-radius> (可选)这是第三个code><length>值. 值越大,越模糊,则阴影会变得更大更淡.不允许负值若未设定,默认是0 (则阴影的边界很锐利). <spread-radius> (可选)这是第四个<length>值. 正值会使阴影扩张和变大,负值会是阴影缩小.若未设定,默认是0 (阴影会与元素一样大小).
注意: Webkit, 以及一些其他浏览器不支持第四个长度,如果加了也不会渲染。 <color> (可选)查看<color>该值可能的关键字和标记。若未设定,颜色值基于浏览器。在Gecko (Firefox), Presto (Opera)和Trident (Internet Explorer)中, 会应用color color属性的值。另外, 如果颜色值省略,WebKit中阴影是透明的。
<blur-radius> (可选)这是第三个code><length>值. 值越大,越模糊,则阴影会变得更大更淡.不允许负值若未设定,默认是0 (则阴影的边界很锐利). <spread-radius> (可选)这是第四个<length>值. 正值会使阴影扩张和变大,负值会是阴影缩小.若未设定,默认是0 (阴影会与元素一样大小).
注意: Webkit, 以及一些其他浏览器不支持第四个长度,如果加了也不会渲染。 <color> (可选)查看<color>该值可能的关键字和标记。若未设定,颜色值基于浏览器。在Gecko (Firefox), Presto (Opera)和Trident (Internet Explorer)中, 会应用color color属性的值。另外, 如果颜色值省略,WebKit中阴影是透明的。
<blur-radius> (可选)这是第三个code><length>值. 值越大,越模糊,则阴影会变得更大更淡.不允许负值若未设定,默认是0 (则阴影的边界很锐利). <spread-radius> (可选)这是第四个<length>值. 正值会使阴影扩张和变大,负值会是阴影缩小.若未设定,默认是0 (阴影会与元素一样大小).
注意: Webkit, 以及一些其他浏览器不支持第四个长度,如果加了也不会渲染。 <color> (可选)查看<color>该值可能的关键字和标记。若未设定,颜色值基于浏览器。在Gecko (Firefox), Presto (Opera)和Trident (Internet Explorer)中, 会应用color color属性的值。另外, 如果颜色值省略,WebKit中阴影是透明的。
<blur-radius> (可选)这是第三个code><length>值. 值越大,越模糊,则阴影会变得更大更淡.不允许负值若未设定,默认是0 (则阴影的边界很锐利). <spread-radius> (可选)这是第四个<length>值. 正值会使阴影扩张和变大,负值会是阴影缩小.若未设定,默认是0 (阴影会与元素一样大小).
注意: Webkit, 以及一些其他浏览器不支持第四个长度,如果加了也不会渲染。 <color> (可选)查看<color>该值可能的关键字和标记。若未设定,颜色值基于浏览器。在Gecko (Firefox), Presto (Opera)和Trident (Internet Explorer)中, 会应用color color属性的值。另外, 如果颜色值省略,WebKit中阴影是透明的。
grayscale( % )

將圖像轉換為灰度圖像。 值定義轉換的比例。 值為100%則完全轉為灰度圖像,值為0%圖像無變化。 值在0%到100%之間,則是效果的線性乘子。 若未設置,值默認是0;

hue-rotate( deg )

給圖像應用色相旋轉。 "angle"一值設定圖像會被調整的色環角度值。 值為0deg,則圖像無變化。 若值未設置,默認值是0deg。 該值雖然沒有最大值,超過360deg的值相當於又繞一圈。

invert( % )

反轉輸入圖像。 值定義轉換的比例。 100%的價值是完全反轉。 值為0%則圖像無變化。 值在0%和100%之間,則是效果的線性乘子。 若值未設置,值默認是0。

opacity( % )

轉化圖像的透明程度。 值定義轉換的比例。 值為0%則是完全透明,值為100%則圖像無變化。 值在0%和100%之間,則是效果的線性乘子,也相當於圖像樣本乘以數量。 若值未設置,值默認是1。 該函數與已有的opacity屬性很相似,不同之處在於通過filter,一些瀏覽器為了提升性能會提供硬件加速。

saturate( % )

轉換圖像飽和度。 值定義轉換的比例。 值為0%則是完全不飽和,值為100%則圖像無變化。 其他值,則是效果的線性乘子。 超過100%的值是允許的,則有更高的飽和度。 若值未設置,值默認是1。

sepia( % )

將圖像轉換為深褐色。 值定義轉換的比例。 值為100%則完全是深褐色的,值為0%圖像無變化。 值在0%到100%之間,則是效果的線性乘子。 若未設置,值默認是0;

url()

URL函數接受一個XML文件,該文件設置了一個SVG濾鏡,且可以包含一個錨點來指定一個具體的濾鏡元素。

例如:

filter: url(svg-url#element-id)
initial

設置屬性為默認值,可參閱: CSS initial關鍵字

inherit 從父元素繼承該屬性,可參閱: CSS inherit關鍵字

Examples

更多實例

模糊實例

圖片使用高斯模糊效果:

img {
-webkit-filter: blur(5px); /* Chrome, Safari, Opera */
filter: blur(5px);
}

嘗試一下»

Brightness 函數實例

使圖片變亮:

img {
-webkit-filter: brightness(200%); /* Chrome, Safari, Opera */
filter: brightness(200%);
}

嘗試一下»

Contrast 函數實例

調整圖像的對比度:

img {
-webkit-filter: contrast(200%); /* Chrome, Safari, Opera */
filter: contrast(200%);
}

嘗試一下»

drop-shadow 函數實例

給圖像設置一個陰影效果:

img {
-webkit-filter: drop-shadow(8px 8px 10px red); /* Chrome, Safari, Opera */
filter: drop-shadow(8px 8px 10px red);
}

嘗試一下»

Grayscale 函數實例

將圖像轉換為灰度圖像:

img {
-webkit-filter: grayscale(50%); /* Chrome, Safari, Opera */
filter: grayscale(50%);
}

嘗試一下»

hue-rotate() 函數實例

給圖像應用色相旋轉:

img {
-webkit-filter: hue-rotate(90deg); /* Chrome, Safari, Opera */
filter: hue-rotate(90deg);
}

嘗試一下»

Invert 函數實例

反轉輸入圖像:

img {
-webkit-filter: invert(100%); /* Chrome, Safari, Opera */
filter: invert(100%);
}

嘗試一下»

Opacity 函數實例

轉化圖像的透明程度:

img {
-webkit-filter: opacity(30%); /* Chrome, Safari, Opera */
filter: opacity(30%);
}

嘗試一下»

Saturate 函數實例

轉換圖像飽和度:

img {
-webkit-filter: saturate(800%); /* Chrome, Safari, Opera */
filter: saturate(800%);
}

嘗試一下»

Sepia 函數實例

將圖像轉換為深褐色:

img {
-webkit-filter: sepia(100%); /* Chrome, Safari, Opera */
filter: sepia(100%);
}

嘗試一下»

複合函數

使用多個濾鏡,每個濾鏡使用空格分隔。

注意:順序是非常重要的(例如使用grayscale()後再使用sepia()將產生一個完整的灰度圖片)。

img {
-webkit-filter: contrast(200%) brightness(150%); /* Chrome, Safari, Opera */
filter: contrast(200%) brightness(150%);
}

嘗試一下»

所有濾鏡實例

以下實例演示了所有濾鏡的使用方法:

.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);
}

嘗試一下»

相關文章

HTML DOM參考手冊: Style filter屬性