繁体   English   中英

渐变边框

[英]Gradient borders

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

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

但这不起作用。

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

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

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

一个简单的例子:

 .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%); top: -10px; left: -10px; bottom: -10px; right: -10px; position: absolute; z-index:-1; }
 <div class="circle"></div>

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

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

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

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

试试这个,在 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>

这是一个技巧,但在某些情况下,您可以通过使用 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

试试下面的例子:

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

Webkit 支持边框渐变,现在接受 Mozilla 格式的渐变。

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

  1. 使用 border-image 和 border-image-source
  2. 使用 border-right-colors (right/left/top/bottom)

IE9 不支持。

试试这个,它对我有用。

 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/希望这有帮助

最直接的方法是使用 border-image 属性。 你可以使用任何你想要的线性渐变或重复渐变。 对于线性渐变,border-image slice 属性需要为 1。

.gradient-border {
    border-style: solid;
    border-width: 2px;
    border-image: linear-gradient(45deg, red, blue) 1;
}

参考MDN - https://developer.mozilla.org/en-US/docs/Web/CSS/border-image-slice数字海洋 - https://www.digitalocean.com/community/tutorials/css-gradient-borders -纯css

我同意 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; }

实现相同效果的另一个技巧是利用多个背景图像,该功能在 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/

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

渐变边框示例

使用边框图像css 属性

致谢: Mozilla 中的边框图像

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

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

试试这个代码

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

这是一种不错的半跨浏览器方式,可以让渐变边框在一半向下淡出。 只需将颜色停止设置为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

您无需使用backgroundbackground-clipbackground-origin删除边框半径即可实现此目的:

 <style> .border-gradient-rounded { /* Border */ border: 4px solid transparent; border-radius: 20px; background: linear-gradient(to right, white, white), linear-gradient(to right, red , blue); background-clip: padding-box, border-box; background-origin: padding-box, border-box; /* Other styles */ width: 100px; height: 40px; padding: 12px; } </style> <div class="border-gradient-rounded"> Content </div>

基本上,这将白色背景定位在渐变背景上,从内边框剪裁白色背景,并从外边框剪裁渐变背景。 这就是为什么您需要将边框定义为solid transparent

归功于此 dev.to post 中的方法 2。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM