2016-05-31 50 views
2

我想要創建圓邊條矩形漸變條紋邊條紋如何使用漸變色條紋邊框通過CSS創建矩形?

我想用IMG標籤或DIV標籤,包括圖像和漸變條紋邊框。

這是怎麼了,需要的樣子: enter image description here

我嘗試搜索周圍,我發現(SCSS)例如:https://jsfiddle.net/rami7250/yujsz7wf/

和我得到這個SCSS代碼:

.module { 
    background: white; 
    border: 1px solid #ccc; 
    margin: 3%; 
    > h2 { 
    padding: 1rem; 
    margin: 0 0 0.5rem 0; 
    } 
    > p { 
    padding: 0 1rem; 
    } 
    /*animation: widen 10s linear alternate infinite;*/ 
} 

.stripe-1 { 
    color: white; 
    background: repeating-linear-gradient(
    45deg, 
    #606dbc, 
    #606dbc 10px, 
    #465298 10px, 
    #465298 20px 
); 
} 

正如您所看到的,該功能隻影響背景而不是邊界

如何作出這樣的矩形,以顯示他的邊框漸變彩色條紋通過CSS/SCSS?

+1

是內部區域(白某)透明? – Harry

+0

不。它內部有圖像。我將使用img或div標籤將圖像包含在我的頁面中。 – StackBuck

+0

圖像是否具有透明度,您是否需要完全相同類型的漸變或只是某種漸變? – Harry

回答

4

一種選擇是將linear-gradient以所需模式使用爲background-image,並將所需內容放在此背景之上。只要內容(無論是圖像還是任何文本或任何不需要透明的內容)都可以使用此方法。輸出也可以通過懸停在元素上來看到。

只需條:(白色層被放置在梯度的頂部,只顯示條)同內部圖像

div { 
 
    height: 250px; 
 
    width: 400px; 
 
    background: linear-gradient(white, white), 
 
       linear-gradient(60deg, orange 0%, orange 3.5%, white 3.5%, white 7%, hotpink 7%, hotpink 10.5%, black 10.5%, black 14%, brown 14%, brown 17.5%, aqua 17.5%, aqua 21%, green 21%, green 25%, orange 25%, orange 28.5%, white 28.5%, white 32%, hotpink 32%, hotpink 35.5%, black 35.5%, black 39%, brown 39%, brown 42.5%, aqua 42.5%, aqua 46%, green 46%, green 50%, orange 50%, orange 53.5%, white 53.5%, white 57%, hotpink 57%, hotpink 60.5%, black 60.5%, black 64%, brown 64%, brown 67.5%, aqua 67.5%, aqua 71%, green 71%, green 75%, orange 75%, orange 78.5%, white 78.5%, white 82%, hotpink 82%, hotpink 85.5%, black 85.5%, black 89%, brown 89%, brown 92.5%, aqua 92.5%, aqua 96%, green 96%, green 100%); 
 
    padding: 10px 20px; 
 
    border-radius: 20px; 
 
    background-origin: content-box, padding-box; 
 
    background-clip: content-box, padding-box; 
 
} 
 

 
/* just for demo */ 
 

 
div { 
 
    transition: all 1s; 
 
} 
 
div:hover { 
 
    height: 300px; 
 
    width: 500px; 
 
}
<div></div>


div

div { 
 
    height: 250px; 
 
    width: 400px; 
 
    background: linear-gradient(white, white), linear-gradient(60deg, orange 0%, orange 3.5%, white 3.5%, white 7%, hotpink 7%, hotpink 10.5%, black 10.5%, black 14%, brown 14%, brown 17.5%, aqua 17.5%, aqua 21%, green 21%, green 25%, orange 25%, orange 28.5%, white 28.5%, white 32%, hotpink 32%, hotpink 35.5%, black 35.5%, black 39%, brown 39%, brown 42.5%, aqua 42.5%, aqua 46%, green 46%, green 50%, orange 50%, orange 53.5%, white 53.5%, white 57%, hotpink 57%, hotpink 60.5%, black 60.5%, black 64%, brown 64%, brown 67.5%, aqua 67.5%, aqua 71%, green 71%, green 75%, orange 75%, orange 78.5%, white 78.5%, white 82%, hotpink 82%, hotpink 85.5%, black 85.5%, black 89%, brown 89%, brown 92.5%, aqua 92.5%, aqua 96%, green 96%, green 100%); 
 
    padding: 10px 20px; 
 
    border-radius: 20px; 
 
    background-origin: content-box, padding-box; 
 
    background-clip: content-box, padding-box; 
 
} 
 
img { 
 
    height: 250px; 
 
    width: 400px; 
 
} 
 
/* just for demo */ 
 

 
div, img { 
 
    transition: all 1s; 
 
} 
 
div:hover, div:hover img { 
 
    height: 300px; 
 
    width: 500px; 
 
}
<div> 
 
    <img src='http://lorempixel.com/400/250/nature/1' /> 
 
</div>


隨着重複線性梯度:(感謝vals用於創建演示)

div { 
 
    height: 250px; 
 
    width: 400px; 
 
    background: linear-gradient(white,white), repeating-linear-gradient(60deg, orange 0%, orange 3.5%, white 3.5%, white 7%, hotpink 7%, hotpink 10.5%, black 10.5%, black 14%, brown 14%, brown 17.5%, aqua 17.5%, aqua 21%, green 21%, green 25%); 
 
    background-attachment: padding-box, border-box; 
 
    padding: 10px 20px; 
 
    border-radius: 20px; 
 
    background-origin: content-box, padding-box; 
 
    background-clip: content-box, padding-box; 
 
} 
 

 
/* just for demo */ 
 

 
div { 
 
    transition: all 1s; 
 
} 
 
div:hover { 
 
    height: 300px; 
 
    width: 500px; 
 
}
<div> 
 
</div>

注:的jagg ed邊緣是使用傾斜漸變時難以避免的東西。它們可以通過調整顏色停止點來減少。

+0

這很棒,但我不想加載完整背景。只有條。 – StackBuck

+0

@StackBuck:一旦頂部圖像加載,背景將不會被看到。沒有其他辦法可以做到這一點。它可以用'border-image'來完成,但它不會給你彎曲的('border-radius')效果。 – Harry

+2

這個背景對我來說似乎沒問題:背景:重複線性漸變(60deg,橙色0%,橙色3.5%,白色3.5%,白色7%,熱水7%,熱水10.5%,黑色10.5%,黑色14%,棕色14%,棕色17.5%,水17.5%,水21%,綠21%,綠25%); – vals

0

發現可能是該設計的關鍵的東西。在這裏看到:https://jsfiddle.net/rami7250/jaca7sb4/

這裏是代碼的來源:http://codepen.io/SitePoint/pen/DKhkf

CSS:

div { 
    position: relative; 
    height: 100%; 

} 
div:before, div:after { 
    position: absolute; 
    content: ''; 
    height: 1em; 
    top: 0; 
    left: 0; 
    right: 0; 
} 
div:after { 
    top: auto; 
    bottom: 0; 
} 
div:before { 
    background: linear-gradient(90deg, #1abc9c 1%, #2ecc71 1%, #2ecc71 29%, #3498db 29%, #3498db 32%, #9b59b6 32%, #9b59b6 37%, #34495e 37%, #34495e 53%, #f1c40f 53%, #f1c40f 69%, #e67e22 69%, #e67e22 67%, #e74c3c 67%, #e74c3c 74%, #ecf0f1 74%, #ecf0f1 100%, #95a5a6 100%); 
} 
div:after { 
    background: linear-gradient(90deg, #1abc9c 10%, #2ecc71 10%, #2ecc71 12.5%, #3498db 12.5%, #3498db 28%, #9b59b6 28%, #9b59b6 35%, #34495e 35%, #34495e 60%, #f1c40f 60%, #f1c40f 69%, #e67e22 69%, #e67e22 83%, #e74c3c 83%, #e74c3c 88%, #ecf0f1 88%, #ecf0f1 96%, #95a5a6 96%); 
} 

div { 
    padding-top: 20px; 
    width: 450px; 
} 

但是,我不能將其設置在離開側的div。只有頂部和底部有效。

0

沒有必要爲這是如此令人費解,它需要CSS的兩行:

.module { 
    border: 20px solid; 
    border-image: repeating-linear-gradient(45deg,#606dbc,#606dbc 10px,#465298 10px,#465298 20px) 20; 
} 

叉的小提琴:https://jsfiddle.net/5k70jt5f/1/