css3属性border-radius与border-image不兼容解决办法

一、border-radius和border-image简介

border-radius:允许您为元素添加圆角边框!

border-image:属性来构造漂亮的可伸缩按钮!(例:渐变图片)

  // 渐变
 border-image: linear-gradient(
      135deg,
      rgba(183, 40, 255, 1),
      rgba(40, 112, 255, 1)
    )
    2 2;

  // 圆角
  border-radius: 10px;

渐变和圆角

image.png

渐变和圆角都可以实现,但是一起用于渐变圆角边框会失效
原因查看官方规范 W3C 解释如下:
A box's backgrounds, but not its border-image, are clipped to the appropriate curve (as determined by ‘background-clip’). Other effects that clip to the border or padding edge (such as ‘overflow’ other than ‘visible’) also must clip to the curve. The content of replaced elements is always trimmed to the content edge curve. Also, the area outside the curve of the border edge does not accept mouse events on behalf of the element。

二、解决方法

  1. 两层元素:外层渐变背景+圆角+内边距,里层圆角+白色背景
  2. 利用伪元素after或before:元素overflow+相对定位+内边距+圆角, 伪元素绝对定位+渐变边框
  3. clip-path:使用裁剪方式创建元素的可显示区域。
  4. background-clip:指定背景绘制区域。


    效果如图
//html
   <div class="gradient_radius1 box out">
      <div class="in">111gradient_radius<br />两层元素</div>
    </div>
    <div class="gradient_radius2 box">222gradient_radius<br />伪类</div>
    <div class="gradient_radius3 box">333gradient_radius<br />clip-path</div>
    <div class="gradient_radius4 box">
      444gradient_radius<br />background-origin、 background-clip
    </div>
//css
.box {
  width: 200px;
  height: 130px;
  text-align: center;
  display: inline-grid;
}
// 方法1
.gradient_radius1 {
  &.out {
    border-radius: 10px;
    padding: 4px;
    background: linear-gradient(
      135deg,
      rgba(183, 40, 255, 1),
      rgba(40, 112, 255, 1)
    );
  }
  .in {
    width: 100%;
    height: 100%;
    background: #fff;
    border-radius: 10px;
  }
}
// 方法2
.gradient_radius2 {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  padding: 4px;
  &::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 4px solid;
    border-image: linear-gradient(
        135deg,
        rgba(183, 40, 255, 1),
        rgba(40, 112, 255, 1)
      )
      1;
  }
}
// 方法3
.gradient_radius3 {
  position: relative;
  border: 4px solid;
  border-image: linear-gradient(
      135deg,
      rgba(183, 40, 255, 1),
      rgba(40, 112, 255, 1)
    )
    1;
  clip-path: inset(0 round 10px);
}
// 方法4
.gradient_radius4 {
  border: solid 4px transparent;
  border-radius: 10px;
  background-image: linear-gradient(#fff, #fff),
    linear-gradient(135deg, rgba(183, 40, 255, 1), rgba(40, 112, 255, 1));
  background-origin: border-box;
  background-clip: content-box, border-box;
}

三、总结

虽说十分不易,但是视觉效果还是挺好看的

缺点:

  1. 需要两个元素(1,2)
  2. 内容背景不可以透明(1,4)
  3. 里边框四角不是圆角(2,3)
  4. 不适用于圆形(2,3)
图例

图例
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容

  • 如何实现下面这个渐变的边框效果: 这个问题本身不难,实现的方法也有一些,主要是有一些细节需要注意。 border-...
    反复练习的阿离很笨吧阅读 5,248评论 0 0
  • 选择器 CSS3中新添加了很多选择器,解决了很多之前需要用javascript才能解决的布局问题。· elemen...
    lovelydong阅读 3,369评论 0 2
  • 关注我的个人博客:https://pinbolei.cn,获取更多内容 1、CSS3的概念和优势 A、CSS3的概...
    pinbolei阅读 4,147评论 0 3
  • CSS3 是 CSS 的修订版本。CSS3 的开发是朝着模块化发展的。 简介 CSS3 被拆分为“模块”。旧规范已...
    神齐阅读 4,743评论 0 2
  • 1.CSS3 边框 border-radius CSS属性用来设置边框圆角。当使用一个半径时确定一个圆形;当使用两...
    garble阅读 3,928评论 0 0