使用 RGBA 的 CSS 透明度


使用 RGBA() 值表示 CSS 透明度。设置 alpha 通道参数可指定颜色的不透明度 −

.transparent {
   background-color: rgba(0, 0, 255, 0.582);
}

RGBA 颜色值包括 rgba(红色,绿色,蓝色,alpha)。此处,将 alpha 设置为透明度,即 −

  • 0.0:完全透明

  • 1.0:完全不透明

使用 RGBA 实现透明度

以下为使用 RGBA 实现 CSS 透明度的代码。此处,我们将 alpha 参数设置为 0.582 以表示透明度 −

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      div {
         width: 200px;
         height: 200px;
         background-color: rgb(0, 0, 255);
         color: white;
         display: inline-block;
      }
      .transparent {
         background-color: rgba(0, 0, 255, 0.582);
      }
   </style>
</head>
<body>
   <h1>Transparency using RGBA example</h1>
   <div class="transparent">
      This is a demo text.
   </div>
   <div>
      This is another demo text.
   </div>
</body>
</html>

使用 RGBA 实现完全透明

以下为使用 RGBA 实现 CSS 透明度的代码。此处,我们将 alpha 参数设置为 0.0 以表示完全透明 −

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      div {
         width: 200px;
         height: 200px;
         background-color: rgb(0, 0, 255);
         color: white;
         display: inline-block;
      }
      .transparent {
         background-color: rgba(0, 0, 255, 0.0);
      }
   </style>
</head>
<body>
   <h1>Transparency using RGBA example</h1>
   <div class="transparent">
      This is a demo text.
   </div>
   <div>
      This is another demo text.
   </div>
</body>
</html>

使用 RGBA 实现完全不透明

以下为使用 RGBA 实现 CSS 透明度的代码。此处,我们将 alpha 参数设置为 1.0 以表示完全不透明 −

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      div {
         width: 200px;
         height: 200px;
         background-color: rgb(0, 0, 255);
         color: white;
         display: inline-block;
      }
      .transparent {
         background-color: rgba(0, 0, 255, 1.0);
      }
   </style>
</head>
<body>
   <h1>Transparency using RGBA example</h1>
   <div class="transparent">
      This is a demo text.
   </div>
   <div>
      This is another demo text.
   </div>
</body>
</html>

更新于: 2023-10-31

1K+ 浏览量

开启职业生涯 之路

完成课程后,即可获得认证

开始学习
广告
© . All rights reserved.