每个边界都有完全不同的渐变?

时间:2015-02-17 06:35:24

标签: css

我希望为边框的4个不同边中的每一边指定不同的线性渐变。我只关心Chrome。我不想使用黑客或背景。我发誓我在一个旧的项目中做到了这一点,使用了以下非常粗糙的例子:

border-top:linear-gradient (top to bottom, 0%, red, 100%, blue),
   -webkit:etc...,
   -moz:etc...;
border-bottom:etc...
border-left:etc...
border-right:etc...

我希望这是一个重复的问题,但我到处寻找(包括:Create a border gradient for each of the 4 borders)并且没有找到我正在寻找的答案。

1 个答案:

答案 0 :(得分:2)

正如@Harry所提到的,我非常怀疑这在以前的项目中很容易实现(在之前的链接答案中看到,这非常像'必须破解'的情况) - 我自己回答了这两个答案和哈利。

我没有遇到过这样的情况(人们为什么会问这样的问题),以及为什么我们不给出“简单”的答案。

这就是为什么我包含以下代码段的原因:

div {
    height:300px;
    width:400px;
    position:absolute;
}
.one, .two {
    position:absolute;
}
.one:before, .one:after{
    content:"";
    position:absolute;
    height:10px;
    width:400px;
    left:0px;
    top:0px;
background: rgb(242,246,248); /* Old browsers */
background: -moz-linear-gradient(left,  rgba(242,246,248,1) 0%, rgba(216,225,231,1) 50%, rgba(181,198,208,1) 51%, rgba(224,239,249,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(242,246,248,1)), color-stop(50%,rgba(216,225,231,1)), color-stop(51%,rgba(181,198,208,1)), color-stop(100%,rgba(224,239,249,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left,  rgba(242,246,248,1) 0%,rgba(216,225,231,1) 50%,rgba(181,198,208,1) 51%,rgba(224,239,249,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left,  rgba(242,246,248,1) 0%,rgba(216,225,231,1) 50%,rgba(181,198,208,1) 51%,rgba(224,239,249,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left,  rgba(242,246,248,1) 0%,rgba(216,225,231,1) 50%,rgba(181,198,208,1) 51%,rgba(224,239,249,1) 100%); /* IE10+ */
background: linear-gradient(to right,  rgba(242,246,248,1) 0%,rgba(216,225,231,1) 50%,rgba(181,198,208,1) 51%,rgba(224,239,249,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f2f6f8', endColorstr='#e0eff9',GradientType=1 ); /* IE6-9 */

}
.one:after{
    top:auto;
    bottom:0;
    background: rgb(180,221,180); /* Old browsers */
background: -moz-linear-gradient(left,  rgba(180,221,180,1) 0%, rgba(131,199,131,1) 17%, rgba(82,177,82,1) 33%, rgba(0,138,0,1) 67%, rgba(0,87,0,1) 83%, rgba(0,36,0,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(180,221,180,1)), color-stop(17%,rgba(131,199,131,1)), color-stop(33%,rgba(82,177,82,1)), color-stop(67%,rgba(0,138,0,1)), color-stop(83%,rgba(0,87,0,1)), color-stop(100%,rgba(0,36,0,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left,  rgba(180,221,180,1) 0%,rgba(131,199,131,1) 17%,rgba(82,177,82,1) 33%,rgba(0,138,0,1) 67%,rgba(0,87,0,1) 83%,rgba(0,36,0,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left,  rgba(180,221,180,1) 0%,rgba(131,199,131,1) 17%,rgba(82,177,82,1) 33%,rgba(0,138,0,1) 67%,rgba(0,87,0,1) 83%,rgba(0,36,0,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left,  rgba(180,221,180,1) 0%,rgba(131,199,131,1) 17%,rgba(82,177,82,1) 33%,rgba(0,138,0,1) 67%,rgba(0,87,0,1) 83%,rgba(0,36,0,1) 100%); /* IE10+ */
background: linear-gradient(to right,  rgba(180,221,180,1) 0%,rgba(131,199,131,1) 17%,rgba(82,177,82,1) 33%,rgba(0,138,0,1) 67%,rgba(0,87,0,1) 83%,rgba(0,36,0,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b4ddb4', endColorstr='#002400',GradientType=1 ); /* IE6-9 */

}
.two {
    height:280px;
    width:380px;
    top:10px;
    left:10px;
    z-index:8;
}
.two:before, .two:after {
    content:"";
    position:absolute;
    height:300px;
    width:10px;
    left:-10px;
    top:-10px;
    background: rgb(30,87,153); /* Old browsers */
background: -moz-linear-gradient(top,  rgba(30,87,153,1) 0%, rgba(41,137,216,1) 50%, rgba(32,124,202,1) 51%, rgba(125,185,232,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(50%,rgba(41,137,216,1)), color-stop(51%,rgba(32,124,202,1)), color-stop(100%,rgba(125,185,232,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* IE10+ */
background: linear-gradient(to bottom,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-9 */

}
.two:after{
    background: rgb(169,3,41); /* Old browsers */
background: -moz-linear-gradient(top,  rgba(169,3,41,1) 0%, rgba(143,2,34,1) 44%, rgba(109,0,25,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(169,3,41,1)), color-stop(44%,rgba(143,2,34,1)), color-stop(100%,rgba(109,0,25,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  rgba(169,3,41,1) 0%,rgba(143,2,34,1) 44%,rgba(109,0,25,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  rgba(169,3,41,1) 0%,rgba(143,2,34,1) 44%,rgba(109,0,25,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  rgba(169,3,41,1) 0%,rgba(143,2,34,1) 44%,rgba(109,0,25,1) 100%); /* IE10+ */
background: linear-gradient(to bottom,  rgba(169,3,41,1) 0%,rgba(143,2,34,1) 44%,rgba(109,0,25,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a90329', endColorstr='#6d0019',GradientType=0 ); /* IE6-9 */

    left:auto;
    right:-10px;
}
<div class="one">
    <div class="two">Four gradients!</div>
</div>

替代

备注,如评论和链接问题的答案中所提到的,将使用多个背景渐变并将它们放置在角落。 - 就像{{3}一样}指出了

.border-image {
  height: 200px;
  width: 200px;
  background: -webkit-linear-gradient(0deg, transparent 10%, white 10%, black 50%, white 90%, transparent 90%), -webkit-linear-gradient(0deg, transparent 10%, white 10%, red 50%, white 90%, transparent 90%), -webkit-linear-gradient(90deg, transparent 10%, white 10%, blue 50%, white 90%, transparent 90%), -webkit-linear-gradient(90deg, transparent 10%, white 10%, green 50%, white 90%, transparent 90%);
  background: -moz-linear-gradient(0deg, transparent 10%, white 10%, black 50%, white 90%, transparent 90%), -moz-linear-gradient(0deg, transparent 10%, white 10%, red 50%, white 90%, transparent 90%), -moz-linear-gradient(90deg, transparent 10%, white 10%, blue 50%, white 90%, transparent 90%), -moz-linear-gradient(90deg, transparent 10%, white 10%, green 50%, white 90%, transparent 90%);
  background: linear-gradient(90deg, transparent 10%, white 10%, black 50%, white 90%, transparent 90%), linear-gradient(90deg, transparent 10%, white 10%, red 50%, white 90%, transparent 90%), linear-gradient(0deg, transparent 10%, white 10%, blue 50%, white 90%, transparent 90%), linear-gradient(0deg, transparent 10%, white 10%, green 50%, white 90%, transparent 90%);
  
  
  background-repeat: no-repeat, no-repeat, no-repeat, no-repeat;
  background-size: 100% 20px, 100% 20px, 20px 100%, 20px 100%;
  background-position: 0px 0px, 0px 100%, 0px 0px, 100% 0px;
  padding: 20px;
}
<div class="border-image"></div>


但是,说实话,我不相信(实现此功能)的“固定方式”(主要是因为它不太可能相当受欢迎) - 因此使用创造性的解决方案。 / p>

相关问题