理解 CSS3 濾鏡函式


濾鏡函式用於設定元素的視覺效果,例如對比度、亮度、色相旋轉、不透明度以及影像等。讓我們看看一些濾鏡函式。

語法

以下是 CSS 中 filter 的語法:

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

如上所示,使用filter屬性,我們可以設定以下效果:模糊、對比度、投影、模糊、亮度、灰度、色相旋轉、反色、不透明度、飽和度、褐色調、url。

反色

invert()用於反轉影像中的樣本。這裡,0% (0) 是實際影像,而 100% 的值將完全反轉影像。要在 CSS3 中反轉影像,請對filter屬性使用invert()

示例

讓我們來看一個使用反轉為影像新增視覺效果的示例:

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <style>
      img{
         margin: 20px;
      }
      img.normal{
         width: 300px;
         height: 300px;
      }
      img.filter {
         width: 300px;
         height: 300px;
         filter: invert(100%);
      }
   </style>
</head>
<body>
   <h1>CSS Image effects example</h1>
   <img class="normal" src="https://tutorialspoint.tw/python/images/python-mini-logo.jpg">
   <img class="filter" src="https://tutorialspoint.tw/python/images/python-mini-logo.jpg">
</body>
</html>

對比度

要在 CSS 中設定影像對比度,請使用 filter contrast(%)。請記住,值為 0 會使影像變黑,100% 用於原始影像和預設影像。其餘的,您可以設定任何您選擇的值,但超過 100% 的值會使影像對比度更高。

示例

現在讓我們來看一個使用 CSS3 調整影像對比度的示例:

<!DOCTYPE html>
<html>
<head>
   <style>
      img.demo {
         filter: brightness(120%);
         filter: contrast(120%);
      }
   </style>
</head>
<body>
   <h1>Learn MySQL</h1>
   <img src="https://tutorialspoint.tw/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
   <h1>Learn MySQL</h1>
   <p>Below image is brighter and has more contrast than the original image above.</p>
   <img class="demo" src="https://tutorialspoint.tw/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
</body>
</html>

模糊

filter屬性用於設定視覺效果,例如模糊、投影、對比度、亮度、飽和度、陰影等 CSS 中的影像。我們將看到如何建立一個模糊影像。blur用於模糊影像。要在 CSS3 中設定模糊效果,請對filter屬性使用blur值。值越大,模糊程度越高。

示例

以下是使用 CSS 建立模糊背景影像的程式碼:

<!DOCTYPE html>
<html>
<head>
   <style>
      body,
      html {
         height: 100vh;
         margin: 0;
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      * {
         box-sizing: border-box;
      }
      .backgroundImage {
         background-image: url("https://images.pexels.com/photos/1172207/pexels-photo-1172207.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500");
         filter: blur(10px);
         height: 100%;
         background-position: center;
         background-repeat: no-repeat;
         background-size: cover;
      }
      .transparentText {
         background-color: rgba(0, 0, 0, 0.4);
         color: white;
         border: 3px solid #f1f1f1;
         position: absolute;
         top: 40%;
         left: 30%;
         width: 50%;
         padding: 20px;
         text-align: center;
      }
   </style>
</head>
<body>
   <div class="backgroundImage"></div>
   <div class="transparentText">
      <h1>I am Amit</h1>
      <h1>I am an Entrepreneur</h1>
   </div>
</body>
</html>

投影

要在 CSS3 中為影像新增投影,請對 filter 屬性使用 drop-shadow 值。它具有以下值:

  • h-shadow - 指定水平陰影的畫素值。

  • v-shadow - 指定垂直陰影的畫素值。負值將陰影放在影像上方。

  • blur - 為陰影新增模糊效果。

  • spread - 正值會導致陰影擴大,負值會導致陰影縮小。

  • color - 為陰影新增顏色

示例

現在讓我們來看一個示例:

<!DOCTYPE html>
<html>
<head>
   <style>
      img.demo {
         filter: brightness(120%);
         filter: contrast(120%);
         filter: drop-shadow(10px 10px 10px green);
      }
   </style>
</head>
<body>
   <h1>Learn MySQL</h1>
   <img src="https://tutorialspoint.tw/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
   <h1>Learn MySQL</h1>
   <img class="demo" src="https://tutorialspoint.tw/mysql/images/mysql-mini-logo.jpg" alt="MySQL" width="160" height="150">
</body>
</html>

褐色調效果

要使用 CSS3 將褐色調效果應用於影像,請對 filter 屬性使用 sepia 值。程式碼如下:

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      img.demo {
         filter: sepia(100%);
      }
   </style>
</head>
<body>
   <h1>Learn Spring Framework</h1>
   <img src="https://tutorialspoint.tw/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
   <h1>Learn Spring Framework</h1>
   <img class="demo" src="https://tutorialspoint.tw/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
</body>
</html>

色相旋轉

色相旋轉調整即增加或減少影像色相值。要在 CSS3 中設定影像的色相旋轉,請對filter屬性使用hue-rotate值。色相旋轉以度數設定,即

  • 實際影像 - 0 度,即預設值

  • 旋轉 30 度 - 30 度

  • 旋轉 90 度 - 90 度,等等。

  • 正色相旋轉 - 增加色相值

  • 負色相旋轉 - 減少色相值

示例

現在讓我們來看一個使用 CSS3 應用色相旋轉的示例。我們將使用色相旋轉設定 45 度:

<!DOCTYPE html>
<html>
<head>
   <style>
      img.demo {
         filter: hue-rotate(45deg);
      }
   </style>
</head>
<body>
   <h1>Learn Spring Framework</h1>
   <img src="https://tutorialspoint.tw/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
   <h1>Learn Spring Framework</h1>
   <img class="demo" src="https://tutorialspoint.tw/spring/images/spring-mini-logo.jpg" alt="Spring Framework" width="160" height="150">
</body>
</html>

更新於:2023-12-27

95 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.