使用css实现渐变色边框

879 阅读2分钟

直角渐变色边框

border-image

代码:index.html HTML:

<div class="box"></div>

CSS:

.box {

      width100px;

      height100px;

      border20px solid;

      border-imagelinear-gradient(to bottom, red, blue) 1 1;

    }

结果: 20px 和 1px 对比

image.png

image.png

圆角渐变色边框

父级:overflow:hidden ,子级:border-image

代码:index2.html HTML:

<div class="parent">

    <div class="child box"></div>

  </div>

CSS:

   .parent {

      overflow: hidden;

      border-radius10px;

      width: fit-content;

      height: fit-content;

    }

   .box {

      width100px;

      height100px;

      border20px solid;

      border-imagelinear-gradient(to bottom, red, blue) 1 1;

    }

结果: 20px 和 1px 对比

image.png

image.png

使用伪类: background-image 、background-clip

代码:index3.html HTML:

<div class="box"></div>
.box {

      width100px;

      height100px;

      border20px solid transparent;  /*important*/

      border-radius10px;

      position: relative;

      background-color: yellow;

      background-clip: padding-box; /*important*/

    }

\


    .box::before {

      content'';

      position: absolute;

      left0;

      top0;

      right0;

      bottom0;

      border-radius: inherit; /*important*/

      background-imagelinear-gradient(to bottom, red, blue);

      margin: -20px/*important*/

      z-index: -1/*important*/

    }

结果: 20px 和 1px 对比

image.png

image.png

注意:box里面的border-color必须不能是有透明度的颜色(rgba),且必须得有颜色,不然无法遮挡住伪类的背景渐变色

background-clip + backgrond-image

代码:index4.html HTML:

<div class="box"></div>

CSS:

.box {

      width100px;

      height100px;

      border20px solid transparent;  /*important*/

      border-radius10px;

      background-imagelinear-gradient(to top, yellow, yellow), linear-gradient(to bottom, red, blue);

      background-clip: padding-box, border-box; /*important*/

      background-origin: padding-box, border-box; /*important*/

    }

结果: 20px 和 1px 对比

image.png

image.png

注意:box里面的border-color必须不能是有透明度的颜色(rgba),且必须得有颜色,不然无法遮挡住伪类的背景渐变色

父级:border + border-radius 子级:1-4条渐变色线定位(使用background实现线)

HTML:

  <div class="box" ></div>

  <div class="box2" style="margin-top:30px"></div>

CSS:

.box {

      width100px;

      height100px;

      border20px solid transparent;  /*important*/

      border-radius10px;

      position: relative;

    }

    .box::before {

      content'';

      position: absolute;

      left0;

      top0;

      bottom0;

      right:0;

      z-index: -1;

      background-imagelinear-gradient(to right, yellow, pink),linear-gradient(to right, yellow, pink),linear-gradient(to bottom, red, blue),  linear-gradient(to bottom, red, blue);

      background-size:  100% 20px100% 20px,20px 100%20px 100%;

      background-position:  center 0%, center 100%0% center, 100% center;

      background-repeat: no-repeat;

      border-radius: inherit;

      margin: -20px;

    }

\


    .box2 {

      width100px;

      height100px;

      border20px solid transparent;  /*important*/

      border-radius10px;

      position: relative;

    }

    .box2::before {

      content'';

      position: absolute;

      left0;

      top0;

      bottom0;

      right:0;

      z-index: -1;

      background-imagelinear-gradient(to right, red, blue),linear-gradient(to right, red, blue),linear-gradient(to bottom, red, red),  linear-gradient(to bottom, blue, blue);

      background-size:  100% 20px100% 20px,20px 100%20px 100%;

      background-position:  center 0%, center 100%0% center, 100% center;

      background-repeat: no-repeat;

      border-radius: inherit;

      margin: -20px;

    }

结果:4个边框,4个颜色渐变 - 20px 和 1px 对比

image.png

image.png