css設定背景圖片灰階的方法:可以利用濾鏡屬性filter來進行設置,如【filter:grayscale(100%)】,表示將影像完全轉換為灰階影像。
本文操作環境:windows10系統、css 3、thinkpad t480電腦。
css中有一個濾鏡屬性filter,該屬性定義了元素(通常是img)的視覺效果,如模糊、飽和度、灰階等。
屬性語法:
filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();
常用屬性值:
#none 預設值,沒有效果。
blur(px) 為影像設定高斯模糊。 "radius"一值設定高斯函數的標準差,或是螢幕上以多少像素融在一起, 所以值越大越模糊;如果沒有設定值,則預設為0;這個參數可設定css長度值,但不接受百分比值。
brightness(%) 給圖片塗上線性乘法,使其看起來更明亮或更暗。如果值是0%,影像會全黑。值是100%,則影像無變化。其他的值對應線性乘數效果。值超過100%也是可以的,影像會比原來更亮。如果沒有設定值,預設是1。
contrast(%) 調整影像的對比。數值是0%的話,影像會全黑。值是100%,影像不變。值可以超過100%,代表會運用更低的對比。若沒有設定值,預設是1。
grayscale(%)
#將影像轉換為灰階影像。值定義轉換的比例。值為100%則完全轉為灰階影像,數值為0%影像無變化。值在0%到100%之間,則是效果的線性乘子。若未設置,值預設為0;
範例1:
<!DOCTYPE html> <html> <head> <style> img { -webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */ filter: grayscale(100%); } </style> </head> <body> <p>图片转为黑白色:</p> <img src="pineapple.jpg" alt="Pineapple" width="300" style="max-width:90%"> <p><strong>注意:</strong> Internet Explorer 或 Safari 5.1 (及更早版本) 不支持该属性。</p> </body> </html>
執行結果:
<!DOCTYPE html> <html> <head> <style> img { -webkit-filter: blur(5px); /* Chrome, Safari, Opera */ filter: blur(5px); } </style> </head> <body> <p>图片使用高斯模糊效果:</p> <img src="pineapple.jpg" alt="Pineapple" width="300" style="max-width:90%"> <p><strong>注意:</strong> Internet Explorer 不支持 filter 属性。</p> </body> </html>
css影片教學)
範例3 :<!DOCTYPE html> <html> <head> <style> img { -webkit-filter: drop-shadow(8px 8px 10px red); /* Chrome, Safari, Opera */ filter: drop-shadow(8px 8px 10px red); } </style> </head> <body> <p>给图像设置一个阴影效果:</p> <img src="pineapple.jpg" alt="Pineapple" width="300" style="max-width:90%"> <p><strong>注意:</strong> Internet Explorer 不支持 filter 属性。</p> </body> </html>
以上是css如何設定背景圖片灰階的詳細內容。更多資訊請關注PHP中文網其他相關文章!