[CSS] CSS @property

Browser support: https://caniuse.com/?search=%40property

Normal CSS variable doesn't support gradient animation

<!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>
      :root {
        --end-color: black;
      }
      .box {
        width: 200px;
        height: 200px;
        background: linear-gradient(skyblue, var(--end-color));
        transition: --end-color 1s;   /* 普通的CSS变量不支持渐变动画 */
      }
      .box:hover {
        --end-color: pink;
      }
    </style>
  </head>
  <body>
    <h2>CSS @property (CSS自定义属性)</h2>
    <div class="box"></div>
  </body>
</html>

 

Use @property to resolve the issue:

<!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>
    @property --end-color {
      syntax: "<color>";
      inherits: false;
      initial-value: black;
    }
    .box {
      width: 200px;
      height: 200px;
      background: linear-gradient(skyblue, var(--end-color));
      transition: --end-color 1s;
    }
    .box:hover {
      --end-color: pink;
    }
  </style>
</head>
<body>
  <h2>CSS @property (CSS自定义属性)</h2>
  <div class="box"></div>
</body>
</html>

 

More: 

posted @ 2025-05-13 01:09  Zhentiw  阅读(12)  评论(0)    收藏  举报