2024-02-03 07:00:02

梯度边界

我试图应用一个渐变的边界,我认为这是简单的这样做:

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

但这并不奏效。

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


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

border-image属性可以实现这一点。您还需要指定border-style和border-width。

border-image: linear-gradient(#f6b73c, #4d9f0c) 30;
border-width: 4px;
border-style: solid;

在MDN上阅读更多。

而不是边框,我会使用背景渐变和填充。同样的外观,但更简单,更有支撑。

举个简单的例子:

.g { Background-image: -webkit-gradient(线性,左下,左上,彩色停止(0.33,rgb(14,173,173)),彩色停止(0.67,rgb(0,255,255))); 背景-图像:-moz-linear-gradient(中底,rgb(14,173,173) 33%, rgb(0,255,255) 67%); 填充:2 px; } .g > div{背景:#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>

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

我同意szajmon的观点。他和昆汀回答的唯一问题是跨浏览器兼容性。

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

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

Firefox以两种方式支持渐变:

使用border-image和border-image-source 使用border-right-colors(右/左/上/下)

IE9不支持。

实现相同效果的另一种方法是利用多个背景图像,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,
        ...;
}

您可以通过更改背景速记属性的背景位置部分,将此技术应用于顶部、右侧和底部边界梯度。

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

试试这个,在web-kit上工作很好

.border { 宽度:400 px; 填充:20 px; border-top: 10px solid #FFFF00; border-bottom:10px solid #FF0000; 背景图片: 线性渐变(# FFFF00 # FF0000), 线性渐变(# FFFF00 # FF0000) ; background-size: 10 px 100%; 背景位置:0 0,100% 0; 平铺方式:不再重演; } < div class = "边境”>你好!< / 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

渐变边框来自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; 
}

border-image-slice将扩展CSS的border-image渐变

这(正如我所理解的)阻止了默认的“图像”切片-没有它,如果边界只在一侧,什么都不会出现,如果它围绕整个元素,每个角落会出现四个微小的梯度。

身体{ 边框:16px实体透明; 边界图像:线性渐变(45度,红,黄); border-image-slice: 1; 身高:120 px; border - radius: 10 px;/*将没有影响*/ }

试试这个,对我很管用。

div { border - radius: 20 px; 身高:70 vh; 溢出:隐藏; } {前div:: 内容:”; 显示:块; box-sizing: border-box; 高度:100%; 边框:1em实心透明; 边界图像:线性渐变(到顶部,红色0%,蓝色100%); border-image-slice: 1; } < div > < / div >

这是小提琴的链接 https://jsfiddle.net/yash009/kayjqve3/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;
}

这种方法的优点是:

它不受z指数的影响 它可以通过改变透明边框的宽度来轻松缩放

去https://codepen.io/AlexOverbeck/pen/axGQyv?editors=1100看看吧

渐变边框的例子

使用border-image css属性

credit to: border-image

.grad-border { 身高:1 px; 宽度:85%; 保证金:0自动; 显示:flex; } .left-border, .right-border { 宽度:50%; Border-bottom: 2px solid #695f52; 显示:inline-block; } .left-border { Border-image:线性梯度(270deg, #b3b3b3, #fff) 1; } .right-border { 边界图像:线性梯度(90度,#b3b3b3, #fff) 1; } < div class = " grad-border”> < div class = "左边框" > < / div > < div class = "右边界" > < / div > < / div >

试试下面的例子:

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

最直接的方法是使用border-image属性。你可以使用任何你想要的线性渐变或重复渐变。对于线性梯度,边界图像切片属性需要为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-pure-css

你可以在不使用background, background-clip和background-origin删除边界半径的情况下实现这一点:

<时尚> .border-gradient-rounded { /* Border */ 边框:4px实体透明; border - radius: 20 px; 背景: 线性渐变(向右,白色,白色), 线性梯度(右,红,蓝); 背景剪辑:填充盒、边框盒; 背景-起源:填充盒,边框盒; /*其他样式*/ 宽度:100 px; 高度:40像素; 填充:12 px; } > < /风格 < div class = " border-gradient-rounded”> 内容 < / div >

基本上,这将白色背景放置在渐变背景上,从内部边界剪辑白色背景,并从外部边界剪辑渐变背景。这就是为什么你需要将边框定义为纯透明的。

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

这是一个带有透明背景的渐变边界,使用边界半径

.gradient-border {
  border-radius: 999px;
  padding: 10px 3rem;
  display: inline-block;
  position: relative;
  background: transparent;
  border: none;
}

.gradient-border::before {
  content: "";
  position: absolute;
  inset: 0;
  padding: 2px;
  border-radius: 9999px;
  background: linear-gradient(87.36deg, blue 6.42%, red 84.24%),
linear-gradient(0deg, #FFFFFF, #FFFFFF);
  -webkit-mask: 
     linear-gradient(#fff 0 0) content-box, 
     linear-gradient(#fff 0 0);
          mask: 
     linear-gradient(#fff 0 0) content-box, 
     linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
          mask-composite: exclude;
}

现场演示:https://jsfiddle.net/jbernier/0eypxc74/1/