在CSS中宣告備用顏色


備用顏色用於指定當瀏覽器不支援RGBA顏色的情況下的顏色。一些不支援備用顏色的瀏覽器,如Opera。在RGBA顏色前指定一個純色,這樣,如果瀏覽器不支援RGBA顏色,純色仍然有效。

設定備用顏色

以下為CSS中宣告備用顏色的程式碼 -

background-color: purple; /*fallback color*/

示例

我們來看一個例子 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body{
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      }
      div {
         padding:20px;
         font-size: 24px;
         background-color: purple; /*fallback color*/
         background-color: rgba(128, 0, 128, 0.527);
         color:white;
      }
   </style>
</head>
<body>
   <h1>Fallback color example</h1>
   <div>Some random text inside this div</div>
</body>
</html>

用rgb()設定備用顏色

示例

現代瀏覽器會先應用rgb(),然後轉到下一行並用rgba()覆蓋該顏色。我們來看一個例子 -

<!DOCTYPE html>
<html>
<head>
   <style>
      p {
         padding:20px;
         font-size: 24px;
         background-color: rgb(255, 99, 71); /*fallback color*/
         background-color: rgb(255, 99, 71, 0.6)
      }
   </style>
</head>
<body>
   <h1>Fallback color example</h1>
   <p>Some random text</p>
</body>
</html>

更新於:2023-11-01

5K+瀏覽量

啟動您的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.