簡體   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