2024-02-03 07:00:02

梯度边界

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

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

但这并不奏效。

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


当前回答

最直接的方法是使用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

其他回答

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

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

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

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

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

Firefox以两种方式支持渐变:

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

IE9不支持。

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

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

举个简单的例子:

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