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>

实现效果 

CSS中,当使用线性渐变色作为边框时,`border-radius` 属性可能不会按预期工作,导致圆角效果不明显或完全无效。这种情况通常发生在尝试将渐变直接应用于 `border` 属性时。 ### 问题原因 当使用 `border` 属性并尝试通过 `linear-gradient` 设置边框颜色时,`border-radius` 的效果可能被忽略,因为渐变背景的绘制方式与普通颜色不同。标准的 `border` 属性并不直接支持渐变色,而是通过 `background-clip` 和 `background-image` 等属性间接实现。 ### 解决方案 为了解决这个问题,可以使用伪元素(`::before` 或 `::after`)来创建渐变边框,并通过 `border-radius` 应用于这些伪元素。以下是一个示例代码: ```css .gradient-border { position: relative; padding: 20px; background-color: white; border-radius: 10px; } .gradient-border::before { content: &#39;&#39;; position: absolute; top: -2px; left: -2px; right: -2px; bottom: -2px; background: linear-gradient(to right, red, blue); border-radius: 10px; z-index: -1; } ``` 在这个示例中,`.gradient-border` 是一个包含内容的容器,而 `::before` 伪元素则用于创建渐变边框。通过设置 `border-radius` 属性,可以确保圆角效果正常应用。 ### 替代方案 另一种方法是使用 `background-image` 和 `background-clip` 属性来实现渐变边框效果: ```css .gradient-border { padding: 20px; background: linear-gradient(to right, red, blue) no-repeat; background-clip: padding-box; border-radius: 10px; border: 2px solid transparent; } ``` 在这个示例中,`background-clip: padding-box;` 确保渐变仅应用于内容区域,而 `border` 属性设置为 `transparent` 以避免覆盖渐变效果。 ### 注意事项 - **兼容性**:某些旧版本的浏览器可能不完全支持 `linear-gradient` 和 `background-clip` 属性,因此需要测试不同浏览器中的表现。 - **性能**:使用伪元素可能会增页面的复杂性,影响渲染性能,尤其是在大量使用时。 通过上述方法,可以在使用线性渐变色作为边框时,确保 `border-radius` 属性正常工作,从而实现所需的视觉效果。[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值