渐变边框

问题描述 投票:252回答:15

我正在尝试将渐变应用到边框,我认为这样做很简单:

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

但这不起作用。

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

css css3 gradient
15个回答
177
投票

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


2
投票

来自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; 
}

1
投票

对于跨浏览器支持,您可以尝试使用:before:after伪元素模仿渐变边框,取决于您想要做什么。


1
投票

试试这个,它对我有用。

.yourDivClass {
    border: 1px solid transparent;
    border-image: linear-gradient(to top, #3a4ed5 0%, #f2f2f2 100%);
    border-image-slice: 1;
}

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


0
投票

试试这段代码

.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/


0
投票

这是一种很好的半跨浏览器方式,可以使渐变边框淡出一半。只需将颜色停止设置为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/en-US/docs/Web/CSS/linear-gradient


-1
投票

这里有一篇很好的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


105
投票

而不是边框​​,我会使用背景渐变和填充。相同的外观,但更容易,更支持。

一个简单的例子:

.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>

48
投票

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


46
投票

border-image-slice will extend a CSS border-image gradient

这(据我所知)可以防止将“图像”默认切片为部分 - 没有它,如果边框仅在一侧,则不会出现任何内容,如果它在整个元素周围,则每个角落会出现四个微小的渐变。

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

26
投票

试试这个,在网络工具包上运行正常

.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>

10
投票

这是一个黑客攻击,但是在某些情况下你可以通过使用background-image指定渐变然后用box-shadow遮盖实际背景来实现这种效果。例如:

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


3
投票

我同意szajmon。他和Quentin的答案唯一的问题是跨浏览器兼容性。

HTML:

<div class="g">
    <div>bla</div>
</div>

CSS:

.g {
background-image: -webkit-linear-gradient(300deg, white, black, white); /* webkit browsers (Chrome & Safari) */
background-image: -moz-linear-gradient(300deg, white, black, white); /* Mozilla browsers (Firefox) */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#000000', gradientType='1'); /* Internet Explorer */
background-image: -o-linear-gradient(300deg,rgb(255,255,255),rgb(0,0,0) 50%,rgb(255,255,255) 100%); /* Opera */
}

.g > div { background: #fff; }

2
投票

Webkit supports gradients in borders,现在接受Mozilla格式的渐变。

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

  1. Using border-image with border-image-source
  2. Using border-right-colors (right/left/top/bottom)

IE9没有支持。


2
投票

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

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

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

#theBox {
    background:
        url(/images/theBox-leftBorderGradient.png) left no-repeat,
        ...;
}

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

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

© www.soinside.com 2019 - 2024. All rights reserved.