渐变边框

CSS

斯丁前端

2020-03-24

我试图将渐变应用于边框,我认为这样做很简单:

border-color: -moz-linear-gradient(top, #555555, #111111);

但这是行不通的。

有人知道做边界渐变的正确方法是什么吗?

第3315篇《渐变边框》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

16个回答
番长樱梅 2020.03.24

这里有关于此的不错的CSS技巧文章:https : //css-tricks.com/gradient-borders-in-css/

使用多个背景和background-origin属性,我能够提出一个非常简单的元素解决方案。

.wrapper {
  background: linear-gradient(#222, #222), 
              linear-gradient(to right, red, purple);
  background-origin: padding-box, border-box;
  background-repeat: no-repeat; /* this is important */
  border: 5px solid transparent;
}

这种方法的优点是:

  1. 不受z-index影响
  2. 只需更改透明边框的宽度即可轻松缩放

签出: https : //codepen.io/AlexOverbeck/pen/axGQyv?editors=1100

Tony凯 2020.03.24

这是一种不错的半跨浏览器方式,可以使渐变边框逐渐降低一半。只需将色标设置rgba(0, 0, 0, 0)

.fade-out-borders {
min-height: 200px; /* for example */

-webkit-border-image: -webkit-gradient(linear, 0 0, 0 50%, from(black), to(rgba(0, 0, 0, 0))) 1 100%;
-webkit-border-image: -webkit-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%;
-moz-border-image: -moz-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%;
-o-border-image: -o-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%;
border-image: linear-gradient(to bottom, black, rgba(0, 0, 0, 0) 50%) 1 100%;
}

<div class="fade-out-border"></div>

用法说明:

Formal grammar: linear-gradient(  [ <angle> | to <side-or-corner> ,]? <color-stop> [, <color-stop>]+ )
                              \---------------------------------/ \----------------------------/
                                Definition of the gradient line         List of color stops  

此处更多内容:https : //developer.mozilla.org/zh-CN/docs/Web/CSS/linear-gradient

Gil伽罗小宇宙 2020.03.24

渐变边框示例

使用border-image CSS属性

致谢:Mozilla中的border-image

.grad-border {
  height: 1px;
  width: 85%;
  margin: 0 auto;
  display: flex;
}
.left-border, .right-border {
  width: 50%;
  border-bottom: 2px solid #695f52;
  display: inline-block;
}
.left-border {
  border-image: linear-gradient(270deg, #b3b3b3, #fff) 1;
}
.right-border {
  border-image: linear-gradient(90deg, #b3b3b3, #fff) 1;
}
<div class="grad-border">
  <div class="left-border"></div>
  <div class="right-border"></div>
</div>

Stafan阿飞 2020.03.24

试试这个代码

.gradientBoxesWithOuterShadows { 
height: 200px;
width: 400px; 
padding: 20px;
background-color: white; 

/* outer shadows  (note the rgba is red, green, blue, alpha) */
-webkit-box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.4); 
-moz-box-shadow: 0px 1px 6px rgba(23, 69, 88, .5);

/* rounded corners */
-webkit-border-radius: 12px;
-moz-border-radius: 7px; 
border-radius: 7px;

/* gradients */
background: -webkit-gradient(linear, left top, left bottom, 
color-stop(0%, white), color-stop(15%, white), color-stop(100%, #D7E9F5)); 
background: -moz-linear-gradient(top, white 0%, white 55%, #D5E4F3 130%); 
}

或参考这个小提琴:http : //jsfiddle.net/necolas/vqnk9/

前端猪猪GO 2020.03.24

对于跨浏览器支持,您也可以尝试模仿带有:before:after伪元素的渐变边框,具体取决于您要执行的操作。

凯西里 2020.03.24

实现相同效果的另一种方法是利用多个背景图像,IE9 +,最新的Firefox和大多数基于WebKit的浏览器均支持该功能:http : //caniuse.com/#feat=multibackgrounds

在IE6-8中,还有一些使用多个背景的选项:http : //www.beyondhyper.com/css3-multiple-backgrounds-in-non-supportive-browsers/

例如,假设您想要一个5像素宽的左边框,该边框是从蓝色到白色的线性渐变。将渐变创建为图像并导出到PNG。在左边界渐变之后列出所有其他CSS背景

#theBox {
    背景:
        url(/images/theBox-leftBorderGradient.png)不重复,
        ...;
}

您可以通过更改background简写属性的背景位置部分,使此技术适应顶部,右侧和底部边界渐变

这是给定示例的jsFiddle:http : //jsfiddle.net/jLnDt/

2020.03.24

请尝试以下示例:

.border-gradient {
      border-width: 5px 5px 5px 5px;
      border-image: linear-gradient(45deg, rgba(100,57,242,1) 0%, rgba(242,55,55,1) 100%);
      border-image-slice: 9;
      border-style: solid;
}
Sam猿泡芙 2020.03.24

试试这个,对我有用。

div{
  border-radius: 20px;
  height: 70vh;
  overflow: hidden;
}

div::before{
  content: '';
  display: block;
  box-sizing: border-box;
  height: 100%;

  border: 1em solid transparent;
  border-image: linear-gradient(to top, red 0%, blue 100%);
  border-image-slice: 1;
}
<div></div>

链接到小提琴https://jsfiddle.net/yash009/kayjqve3/1/希望这会有所 帮助

LGil 2020.03.24

Webkit支持border中的渐变,并且现在接受Mozilla格式的渐变。

Firefox声称以两种方式支持渐变:

  1. 将border-image与border-image-source一起使用
  2. 使用右边框颜色(右/左/上/下)

IE9不支持。

番长猴子 2020.03.24

Css-Tricks的渐变边框:http : //css-tricks.com/examples/GradientBorder/

.multbg-top-to-bottom {
  border-top: 3px solid black;
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#000), to(transparent));
  background-image: -webkit-linear-gradient(#000, transparent);
  background-image:
      -moz-linear-gradient(#000, transparent),
      -moz-linear-gradient(#000, transparent);
  background-image:
      -o-linear-gradient(#000, transparent),
      -o-linear-gradient(#000, transparent);
  background-image: 
      linear-gradient(#000, transparent),
      linear-gradient(#000, transparent);
  -moz-background-size: 3px 100%;
  background-size: 3px 100%;
  background-position: 0 0, 100% 0;
  background-repeat: no-repeat; 
}
乐Davaid 2020.03.24

尝试一下,在Web-kit上运行良好

.border { 
    width: 400px;
    padding: 20px;
    border-top: 10px solid #FFFF00;
    border-bottom:10px solid #FF0000;
    background-image: 
        linear-gradient(#FFFF00, #FF0000),
        linear-gradient(#FFFF00, #FF0000)
    ;
    background-size:10px 100%;
    background-position:0 0, 100% 0;
    background-repeat:no-repeat;
}
<div class="border">Hello!</div>

米亚Tony 2020.03.24

而不是边框​​,我将使用背景渐变和填充。外观相同,但更加轻松,获得更多支持。

一个简单的例子:

.g {
background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.33, rgb(14,173,173)), color-stop(0.67, rgb(0,255,255)));
background-image: -moz-linear-gradient(center bottom, rgb(14,173,173) 33%, rgb(0,255,255) 67% );
padding: 2px;
}

.g > div { background: #fff; }
<div class="g">
	<div>bla</div>
</div>


编辑:您也可以利用:before选择器,如@WalterSchwarz所指出的:

body {
    padding: 20px;
}
.circle {
    width: 100%;
    height: 200px;
    background: linear-gradient(to top, #3acfd5 0%, #3a4ed5 100%);
    border-radius: 100%;
    position: relative;
    text-align: center;
    padding: 20px;
    box-sizing: border-box;
}
.circle::before {
    border-radius: 100%;
    content: '';
    background-image: linear-gradient(to bottom, #3acfd5 0%, #3a4ed5 100%);
    padding: 10px;
    width: 100%;
    height:100%;
    top: -10px;
    left: -10px;
    position:absolute;
    z-index:-1;
}
<div class="circle">Test</div>

泡芙 2020.03.24

这是一个技巧,但是在某些情况下,您可以通过使用background-image指定渐变,然后用盒形阴影遮罩实际背景来实现此效果。例如:

p {
  display: inline-block;
  width: 50px;
  height: 50px;
  /* The background is used to specify the border background */
  background: -moz-linear-gradient(45deg, #f00, #ff0);
  background: -webkit-linear-gradient(45deg, #f00, #ff0);
  /* Background origin is the padding box by default.
  Override to make the background cover the border as well. */
  -moz-background-origin: border;
  background-origin: border-box;
  /* A transparent border determines the width */
  border: 4px solid transparent;
  border-radius: 8px;
  box-shadow:
    inset 0 0 12px #0cc, /* Inset shadow */
    0 0 12px #0cc, /* Outset shadow */
    inset -999px 0 0 #fff; /* The background color */
}

来自:http : //blog.nateps.com/the-elusive-css-border-gradient

番长 2020.03.24

border-image-slice 将扩展CSS边框图像渐变

(据我所知)这可以防止将“图像”默认分割成多个部分-如果没有,则仅当边框在一侧时什么也不会出现,并且如果边框在整个元素周围,则每个角上会出现四个微小的渐变。

  border-bottom: 6px solid transparent;
  border-image: linear-gradient(to right, red , yellow);
  border-image-slice: 1;
JinJin逆天 2020.03.24

WebKit现在(至少Chrome 12)支持渐变作为边框图像:

-webkit-border-image: -webkit-gradient(linear, left top, left bottom, from(#00abeb), to(#fff), color-stop(0.5, #fff), color-stop(0.5, #66cc00)) 21 30 30 21 repeat repeat;

Prooflink- http
: //www.webkit.org/blog/1424/css3-gradients/浏览器支持:http : //caniuse.com/#search=border-image

JinJin 2020.03.24

Mozilla当前仅支持CSS渐变作为background-image属性以及速记背景的值。

https://developer.mozilla.org/en/CSS/-moz-linear-gradient

Example 3 - Gradient Borders

border: 8px solid #000;
-moz-border-bottom-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
-moz-border-top-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
-moz-border-left-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
-moz-border-right-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
padding: 5px 5px 5px 15px; 

http://www.cssportal.com/css3-preview/borders.htm

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android