CSS3 颜色-透明度

128 阅读1分钟
  • css2 - opacity
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .out {
      width: 400px;
      height: 400px;
      background-color: blueviolet;
      margin: 100px auto;
      border: 1px solid blue;
      /* opacity 可以给盒子设置半透明,但是会影响里面的子盒子,并且子盒子无法再改变透明度 */
      opacity: 0.3;
    }
    .in {
      width: 200px;
      height: 200px;
      background-color: red;
      margin: 100px auto;
      border: 1px solid blue;
    }
  </style>
</head>
<body>
<div class="out">
  <div class="in"></div>
</div>
</body>
</html>

  • css2 - transparent
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .out {
      width: 400px;
      height: 400px;
      margin: 100px auto;
      border: 1px solid blue;
      /* transparent 完全透明,无法更改 */
      background: transparent;
    }
    .in {
      width: 200px;
      height: 200px;
      background-color: red;
      margin: 100px auto;
      border: 1px solid blue;
    }
  </style>
</head>
<body>
<div class="out">
  <div class="in"></div>
</div>
</body>
</html>

  • css3 - RGBA red green blue (0-255) alpha: 透明度 (0-1)

  • css3 - HSLA H: 色调 (0-360) S: 饱和度 (0-100%) L: 亮度 (0-100%) A: alpha 透明度 (0-1)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .out {
      width: 400px;
      height: 400px;
      margin: 100px auto;
      border: 1px solid blue;
      background-color: hsla(32, 50%, 50%, 1.0);
    }
    .in {
      width: 200px;
      height: 200px;
      background-color: red;
      margin: 100px auto;
      border: 1px solid blue;
    }
  </style>
</head>
<body>
<div class="out">
  <div class="in"></div>
</div>
</body>
</html>