一、橫向條紋
如下代碼:
1 background: linear-gradient(#fb3 20%, #58a 80%)
上面代碼表示整個圖片的上部分20%和下部分20%是對應的純色,只有中間的部分是漸變色。如果讓中間的部分逐漸縮小,當中間部分變為0即上下兩種顏色的七點和終點相同是,就沒有了漸變而變成了兩種顏色的色條:
1 background: linear-gradient(#fb3 50%, #58a 50%);
接下來可以通過設置背景的大小,讓背景高度變小並且背景默認為repeat,從而出現條紋狀
1 background: linear-gradient(#fb3 50%, #58a 50%); 2 background-size: 100% 30px;
設計塢https://www.wode007.com/sites/73738.html
我們可以不設定第二個顏色的起始位置,設置為0,則瀏覽器默認為接着上一個顏色開始:
1 background: linear-gradient(#fb3 30%, #58a 0); 2 background-size:100% 30px;
這樣就形成了一個黃色占30%藍色占70%的條紋狀背景
也可以設置多種顏色,下面設置了三種顏色的條紋:
1 background: linear-gradient(#fb3 33.3%, #58a 0, #58a 66.6%,yellowgreen 0); 2 background-size: 100% 45px;
二、豎向條紋
只需要在linear-gradient方法中加一個前綴即可。注意還需顛倒background-size長寬的設定
1 background: linear-gradient(to rightright, #fb3 50%, #58a 0); 2 background-size:30px 100%;
三、斜向條紋
可以通過修改background-size的值以及為linear-gradient添加角度來實現斜向的條紋:
1 background: linear-gradient(45deg, #fb3 50%, #58a 0); //讓背景的漸變帶有傾斜 2 background-size:30px 30px; //每一塊小組成部分固定寬度和高度
但這樣做的結果是只會形成一小塊一小塊的斜線,而不是整體div的斜線,我們需要以四個小div為一組繪制斜線,添加linear-gradient中的顏色分解:
1 background: linear-gradient(45deg, #fb3 25%, #58a 0, #58a50%, #fb3 0, #fb3 75%, #58a 0); 2 background-size:30px 30px;
四、使用repeat-linear-gradient
對於斜線的背景繪制,使用repeat-linear-gradient方法更有效。使用該方法的時候,設置的顏色變化會自動進行重復直到鋪滿整個div。實例代碼如下:
1 background: repeating-linear-gradient(45deg, #fb3, #58a 30px);
通過這種方式可以任意更改角度,而不會出現上中方法中的調節困難
1 background: repeating-linear-gradient(60deg, #fb3, #fb315px, #58a 0, #58a 30px);
(這種方法其實相當於將size的控制和gradient的控制合到了一起)
五、關於顏色的設定
有時我們希望條紋背景的顏色之間是相近的顏色,但是如果手動設定這個顏色的#很不方便,也很難發現要選擇什么樣的顏色。可以使用如下方法:
1 background: #58a; 2 background-image: repeating-linear-gradient(30deg, 3 hsla(0,0%,100%,.1), 4 hsla(0,0%,100%,.1)15px, 5 transparent 0,transparent 30px);
這種方法的原理是:指定背景中最深的顏色,條文中其他相近色用透明度進行調整
六、綜合實例
這里效果圖一起放上來了,與下面的樣式一一對應:
公用部分:
1 .stripes { 2 height: 250px; 3 width: 375px; 4 float: left; 5 margin: 10px; 6 -webkit-background-size: 50px 50px; 7 -moz-background-size: 50px 50px; 8 background-size: 50px 50px; /* 控制條紋的大小 */ 9 10 -moz-box-shadow: 1px 1px 8px gray; 11 -webkit-box-shadow: 1px 1px 8px gray; 12 box-shadow: 1px 1px 8px gray; 13 }
圖1:
1 .horizontal { 2 background-color: #0ae; 3 background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent)); 4 background-image: -moz-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent); 5 background-image: -o-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent); 6 background-image: linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent); 7 }
1 .vertical { 2 background-color: #f90; 3 background-image: -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent)); 4 background-image: -moz-linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent); 5 background-image: -o-linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent); 6 background-image: linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent); 7 }
圖3:
1 .picnic { 2 background-color:white; 3 background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5))), 4 -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5))); 5 background-image: -moz-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)), 6 -moz-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)); 7 background-image: -o-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)), 8 -o-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)); 9 background-image: linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)), 10 linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)); 11 }
圖4:
1 .picnic { 2 background-color:white; 3 background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5))), 4 -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5))); 5 background-image: -moz-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)), 6 -moz-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)); 7 background-image: -o-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)), 8 -o-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)); 9 background-image: linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)), 10 linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)); 11 }
圖5:
1 .angled-135 { 2 background-color: #c16; 3 background-image: -webkit-gradient(linear, 0 0, 100% 100%, 4 color-stop(.25, rgba(255, 255, 255, .2)), color-stop(.25, transparent), 5 color-stop(.5, transparent), color-stop(.5, rgba(255, 255, 255, .2)), 6 color-stop(.75, rgba(255, 255, 255, .2)), color-stop(.75, transparent), 7 to(transparent)); 8 background-image: -moz-linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%, 9 transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%, 10 transparent 75%, transparent); 11 background-image: -o-linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%, 12 transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%, 13 transparent 75%, transparent); 14 background-image: linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%, 15 transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%, 16 transparent 75%, transparent); 17 }
圖6:
1 .checkered { 2 background-image: -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, #555), color-stop(.25, transparent), to(transparent)), 3 -webkit-gradient(linear, 0 100%, 100% 0, color-stop(.25, #555), color-stop(.25, transparent), to(transparent)), 4 -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.75, transparent), color-stop(.75, #555)), 5 -webkit-gradient(linear, 0 100%, 100% 0, color-stop(.75, transparent), color-stop(.75, #555)); 6 background-image: -moz-linear-gradient(45deg, #555 25%, transparent 25%, transparent), 7 -moz-linear-gradient(-45deg, #555 25%, transparent 25%, transparent), 8 -moz-linear-gradient(45deg, transparent 75%, #555 75%), 9 -moz-linear-gradient(-45deg, transparent 75%, #555 75%); 10 background-image: -o-linear-gradient(45deg, #555 25%, transparent 25%, transparent), 11 -o-linear-gradient(-45deg, #555 25%, transparent 25%, transparent), 12 -o-linear-gradient(45deg, transparent 75%, #555 75%), 13 -o-linear-gradient(-45deg, transparent 75%, #555 75%); 14 background-image: linear-gradient(45deg, #555 25%, transparent 25%, transparent), 15 linear-gradient(-45deg, #555 25%, transparent 25%, transparent), 16 linear-gradient(45deg, transparent 75%, #555 75%), 17 linear-gradient(-45deg, transparent 75%, #555 75%); 18 }
HTML代碼:
1 <div class="horizontal stripes"></div> 2 <div class="vertical stripes"></div> 3 <div class="picnic stripes"></div> 4 <div class="angled stripes"></div> 5 <div class="angled-135 stripes"></div> 6 <div class="checkered stripes"></div>