CSS 文字渐变效果

关键点

  • 背景色绘制区域 background-clip:text
  • 浏览器兼容

预览

在这里插入图片描述

完整代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      @import url(https://fonts.googleapis.com/css?family=Lato);
      body {
        display: flex;
        height: 100vh;
        justify-content: center;
        align-items: center;
        text-align: center;
        background: black;
      }

      .shining {
        font-size: 2em;
        font-family: Lato, sans-serif;
        letter-spacing: 4px;
        text-transform: uppercase;
        background: linear-gradient(90deg, black 0%, white 50%, black 100%);
        background-size: 80%;
        background-repeat: no-repeat;
        color: transparent;
        -webkit-background-clip: text;
        background-clip: text;
        -webkit-animation: shining 3s linear infinite;
        animation: shining 3s linear infinite;
      }

      @-webkit-keyframes shining {
        from {
          background-position: -500%;
        }
        to {
          background-position: 500%;
        }
      }

      @keyframes shining {
        from {
          background-position: -500%;
        }
        to {
          background-position: 500%;
        }
      }
    </style>
  </head>
  <body>
    <p class="shining">This is handsome word</p>
  </body>
</html>

升级版(彩虹渐变)

HSl

预览

在这里插入图片描述

完整代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      @import url("https://fonts.googleapis.com/css?family=Lato:300");
      body {
        display: flex;
        height: 100vh;
        justify-content: center;
        align-items: center;
        text-align: center;
        background: #222;
      }

      .rainbow {
        letter-spacing: 0.2rem;
        font-size: 1.2rem;
        font-family: Lato, serif;
        text-transform: uppercase;
      }
      .rainbow span {
        animation: rainbow 50s alternate infinite forwards;
      }

      @keyframes rainbow {
        0% {
          color: #ef8fba;
        }
        5% {
          color: #a4ef8f;
        }
        10% {
          color: #d98fef;
        }
        15% {
          color: #efa48f;
        }
        20% {
          color: #efe78f;
        }
        25% {
          color: #8fefbf;
        }
        30% {
          color: #8fefe2;
        }
        35% {
          color: #8f96ef;
        }
        40% {
          color: #d1ef8f;
        }
        45% {
          color: #8fa4ef;
        }
        50% {
          color: #efd78f;
        }
        55% {
          color: #8fefc4;
        }
        60% {
          color: #efc98f;
        }
        65% {
          color: #8fefd2;
        }
        70% {
          color: #ef8fea;
        }
        75% {
          color: #8fefb7;
        }
        80% {
          color: #ef8fba;
        }
        85% {
          color: #efe18f;
        }
        90% {
          color: #dcef8f;
        }
        95% {
          color: #ccef8f;
        }
        100% {
          color: #ed8fef;
        }
      }
    </style>
  </head>
  <body>
    <p class="rainbow">This is handsome word</p>
  </body>
  <script>
    let rainbowText = document.querySelector(".rainbow");
    let letters = rainbowText.textContent.split("");
    rainbowText.textContent = "";
    letters.forEach((letter, i) => {
      let span = document.createElement("span");
      span.textContent = letter;
      span.style.animationDelay = `${-20 + i * 0.2}s`;
      rainbowText.append(span);
    });
  </script>
</html>

升级版(打字机效果)

text-shadow

预览

在这里插入图片描述

完整代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      @import url("https://fonts.googleapis.com/css?family=Lora:400,400i,700");
      body {
        display: flex;
        flex-direction: column;
        height: 100vh;
        justify-content: center;
        align-items: center;
        background-image: linear-gradient(
            rgba(16, 16, 16, 0.8),
            rgba(16, 16, 16, 0.8)
          ),
          url(https://i.loli.net/2019/11/03/RtVq2wxQYySDb8L.jpg);
        background-size: cover;
      }

      p {
        margin: 0em 5em 4em 5em;
      }

      .glowIn {
        text-align: left;
        width: 400px;
        line-height: 1.8;
        color: white;
        font-family: Lora, serif;
      }
      .glowIn span {
        animation: glowIn 0.8s ease both;
      }

      @keyframes glowIn {
        from {
          opacity: 0;
        }
        65% {
          opacity: 1;
          text-shadow: 0 0 25px white;
        }
        75% {
          opacity: 1;
        }
        to {
          opacity: 0.7;
        }
      }
    </style>
  </head>
  <body>
    <h1 class="glowIn">Hello World</h1>
    <p class="glowIn">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
      tempor incididunt ut labore et dolore magna aliqua. Mattis pellentesque id
      nibh tortor. Suspendisse ultrices gravida dictum fusce ut placerat orci
      nulla. A lacus vestibulum sed arcu.
    </p>
  </body>
  <script>
    let glowInTexts = document.querySelectorAll(".glowIn");
    glowInTexts.forEach(glowInText => {
      let letters = glowInText.textContent.split("");
      glowInText.textContent = "";
      letters.forEach((letter, i) => {
        let span = document.createElement("span");
        span.textContent = letter;
        span.style.animationDelay = `${i * 0.05}s`;
        glowInText.append(span);
      });
    });
  </script>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值