Weird gradient border effect

前端 未结 7 545
旧时难觅i
旧时难觅i 2020-11-30 05:33

When applying a transparent border over an element with a linear-gradient as the background, I get a weird effect.

7条回答
  •  感情败类
    2020-11-30 05:43

    The default value of background-origin property is padding-box which means the background is positioned and sized relative to the padding box.

    The background also extends below border since the background-clip property defaults to border-box; it simply repeats itself below the border. This is why you see the right side of background below left border and vice versa.

    So, just change the origin:

    .colors {
      width: 100px;
      border: 10px solid rgba(0, 0, 0, 0.2);
      height: 50px;
      background: linear-gradient(to right, #78C5D6, #459BA8, #79C267, #C5D647, #F5D63D, #F08B33, #E868A2, #BE61A5);
      background-origin: border-box;
    }

    Alternately you can play with background size and position: add 20px to the background size and position it at -10px -10px:

    .colors {
      width: 100px;
      border: 10px solid rgba(0, 0, 0, 0.2);
      height: 50px;
      background: linear-gradient(to right, #78C5D6, #459BA8, #79C267, #C5D647, #F5D63D, #F08B33, #E868A2, #BE61A5);
      background-position: -10px -10px;
      background-size: calc(100% + 20px) calc(100% + 20px);
    }

提交回复
热议问题