CSS 动态边框、渐变色边框 北京透明效果

一、背景透明+ 流动边框效果

使用 clip-path 

<body>
    <div class="box">hello</div>
  </body>
  <style>
    @keyframes clippath {
      0%,
      100% {
        clip-path: inset(0 0 95% 0);
      }
      25% {
        clip-path: inset(0 95% 0 0);
      }
      50% {
        clip-path: inset(95% 0 0 0);
      }
      75% {
        clip-path: inset(0 0 0 95%);
      }
    }
    .box {
      width: 100px;
      height: 100px;
      background-color: rgba(0, 0, 0, 0.2);
      text-align: center;
      line-height: 100px;
      position: relative;
      border-radius: 4px;
    }
    .box::after {
      content: "";
      display: block;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      border: 1px solid red;
      transition: all 0.5s;
      border-radius: 4px;
      animation: clippath 3s infinite linear;
    }
  </style>

最终效果:边框逆时针动画,且内容部分可设置背景透明

 

二、背景透明+渐变色边框效果

使用 mask 和mask-composite 实现 ,需注意浏览器兼容问题,新版 谷歌、火狐可使用

<body>
  <div class="box">hello</div>
</body>
<style>
  .box {
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    position: relative;
    border-radius: 4px;
  }
  .box::after {
    content: "";
    display: block;
    width: 100%;
    height: 100%;
    border-radius: 4px;
    background: linear-gradient(180deg, #ffe70b, #bfff7e, #75fb03);
    padding: 3px;
    mask: linear-gradient(#fff 0 100%) content-box, linear-gradient(#fff 0 100%);
    mask-composite: exclude;
    position: absolute;
    top: -3px;
    left: -3px;
  }
</style>

实现效果 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值