繁体   English   中英

多个元素边框上的 CSS 渐变

[英]CSS Gradient over multiple element's borders

我正在尝试实现这样的目标(颜色无关): 渐变边框

我几乎不知道如何实现这一目标。 我已经设法弄清楚如何通过 Multiple.js 对多个元素进行渐变,但我没有设法找出将其应用于边框的任何方法。 这是我到目前为止所拥有的:

.interestingElement {
    padding: 0.5em 1em 0.6em 1em;
    border: 1px solid black;
    border-radius: 5px;
    margin: 1em;
    font-size: 1.1em;
    background-image: linear-gradient(to right, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
}

我希望可能有一种方法可以做到这一点,而无需为每个元素采用单独的渐变。 任何帮助将不胜感激。 谢谢!

编辑 1:如果我的目标只是添加渐变边框,我肯定会使用 border-image 来代替。 有没有办法让它通过多个元素? 可能是这样的:

.interestingElement {
    padding: 0.5em 1em 0.6em 1em;
    border: 5px solid black;
    border-image: linear-gradient(to right, black 0%, white 100%) 30% stretch;
    border-radius: 5px;
    margin: 1em;
}

您有 2 种可能性来实现此效果。

两者都在基本元素上设置渐变,在子元素上留下某种透明度以使其显示。

一种可能性是使用边框颜色:透明,对背景使用一些特殊效果以将其限制为内容,并使用阴影来掩盖框周围的渐变。 (这很棘手。另一种可能更容易使用的方法是使用伪元素)

另一种可能性是使用混合模式。 这有额外的好处,可以实现文本的渐变。 但是浏览器支持较少,并且以某种方式限制了可以在嵌套元素中实现的颜色。

在代码片段中, element1 类显示了第一种方法, element2 显示了第二种方法

 .base { width: 300px; height: 100px; background: linear-gradient(to right, red, green); margin-top: 10px; overflow: hidden; } .element { display: inline-block; width: 100px; height: 30px; top: 25px; position: relative; border-radius: 10px; border: solid 10px black; background-color: white; font-size: 30px; } .element:nth-child(1) { margin-left: 10px; } .element:nth-child(2) { margin-left: 30px; } .element1 { border-color: transparent; background-clip: content-box; box-shadow: 0px 0px 0px 30px white, 10px -20px 0px 20px white, 10px 20px 0px 20px white ; } .element2 { border-color: black; mix-blend-mode: lighten; box-shadow: 0px 0px 0px 30px white, 10px -20px 0px 20px white, 10px 20px 0px 20px white ; }
 <div class="base base1"> <div class="element element1">ONE</div> <div class="element element1">TWO</div> </div> <div class="base base2"> <div class="element element2">ONE</div> <div class="element element2">TWO</div> </div>

您可以通过使用多个linear-gradients来创建此效果

 div { width: 200px; height: 80px; border-radius: 5px; background: linear-gradient(to right, orange, yellow), linear-gradient(to right, orange, yellow), linear-gradient(to right, orange, orange), linear-gradient(to right, yellow, yellow); background-size: 100% 3px, 100% 3px, 3px 100%, 3px 100%; background-position: 0 0, 0 100%, 0 0, 100% 0; background-repeat: no-repeat; }
 <div></div>

应用前缀以获得更多浏览器支持

 div { width: 200px; height: 80px; border-radius: 5px; background: -webkit-gradient(linear, left top, right top, from(orange), to(yellow)), -webkit-gradient(linear, left top, right top, from(orange), to(yellow)), -webkit-gradient(linear, left top, right top, from(orange), to(orange)), -webkit-gradient(linear, left top, right top, from(yellow), to(yellow)); background: -webkit-linear-gradient(left, orange, yellow), -webkit-linear-gradient(left, orange, yellow), -webkit-linear-gradient(left, orange, orange), -webkit-linear-gradient(left, yellow, yellow); background: -o-linear-gradient(left, orange, yellow), -o-linear-gradient(left, orange, yellow), -o-linear-gradient(left, orange, orange), -o-linear-gradient(left, yellow, yellow); background: linear-gradient(to right, orange, yellow), linear-gradient(to right, orange, yellow), linear-gradient(to right, orange, orange), linear-gradient(to right, yellow, yellow); background-size: 100% 3px, 100% 3px, 3px 100%, 3px 100%; background-position: 0 0, 0 100%, 0 0, 100% 0; background-repeat: no-repeat; }
 <div></div>

暂无
暂无

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

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