文本颜色适配背景色(附带完整代码)

文本颜色适配背景色

使用mix-blend-mode实现文本颜色随着背景颜色自动变化
chrome-capture.gif

关键知识点

关键代码

mix-blend-mode: difference;

完整代码

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta
      http-equiv="X-UA-Compatible"
      content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width,initial-scale=1.0" />
    <title>文本颜色适配背景色</title>
    <meta
      name="keywords"
      content="demo,js,css,animate,技术方" />
    <style>
      body {
        background: #eee;
        margin: 0;
        padding: 8px;
      }
      .outbox {
        width: 400px;
        max-width: 90vw;
        margin: 0 auto;
      }
      .container {
        width: 100%;
        padding-bottom: 100%;
        position: relative;
      }
      @property --direct {
        syntax: '<angle>';
        initial-value: 0deg;
        inherits: false;
      }
      .card {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        --border-width: 1rem;
        border-radius: 0.25rem;
        padding: var(--border-width);
        --direct: 0deg;
        background: linear-gradient(var(--direct), black 50%, white 50%);
        animation: rotate 2s linear infinite;
      }
      @keyframes rotate {
        to {
          --direct: 360deg;
        }
      }

      .inner {
        color: #fff;
        mix-blend-mode: difference;
        font-size: 32px;
        text-align: center;
      }
    </style>
  </head>

  <body>
    <div class="outbox">
      <div class="container">
        <div class="card">
          <div class="inner">文本颜色适配背景色</div>
        </div>
      </div>
    </div>
  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值