利用 CSS 繪製圖形是一種有利有弊的做法:好處是不必畫圖、能節省流量;壞處是要寫長串的樣式,而且有時流量未必真的減少,用不用則視情況而定。
以下是我參考多個網站後整理的一些圖形示例,並做簡單的注解與歸納,並未涉及太複雜的 CSS 畫圖技巧。由於使用了 CSS3.0 的一些屬性,請用支援 CSS3.0 的瀏覽器查看本文!
先看正方形、矩形、梯形與平行四邊形,這幾個相對簡單:
/*正方形*/ .Square { width: 100px; height: 100px; background: #669; } /*矩形*/ .rectangle { width: 200px; height: 100px; background: #669; } /*梯形*/ .trapezoid { border-bottom: 100px solid #669; border-left: 50px solid transparent; border-right: 50px solid transparent; height: 0; width: 100px; } /*平行四邊形*/ .parallelogram { width: 150px; height: 100px; -webkit-transform: skew(-20deg); -moz-transform: skew(-20deg); -o-transform: skew(-20deg); background: #669;margin-left:20px; }
接著是各種三角形。原理是利用相鄰兩個邊框接壤處的分配原則——若沒有寬高,本應是四個三角形接成的矩形,只要把不需要顯示的邊框設為透明即可:
/*三角形*/ .triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid #669; } .triangle-down { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-top: 100px solid #669; } .triangle-left { width: 0; height: 0; border-top: 50px solid transparent; border-right: 100px solid #669; border-bottom: 50px solid transparent; } .triangle-right { width: 0; height: 0; border-top: 50px solid transparent; border-left: 100px solid #669; border-bottom: 50px solid transparent; } .triangle-topleft { width: 0; height: 0; border-top: 100px solid #669; border-right: 100px solid transparent; } .triangle-topright { width: 0; height: 0; border-top: 100px solid #669; border-left: 100px solid transparent; } .triangle-bottomleft { width: 0; height: 0; border-bottom: 100px solid #669; border-right: 100px solid transparent; } .triangle-bottomright { width: 0; height: 0; border-bottom: 100px solid #669; border-left: 100px solid transparent; }
下面這個例子清楚標明了四邊的情況,上面的三角形都是利用這個原理,把不想顯示的邊框設為 transparent:
<div style="border-left:50px solid #060;border-right:50px solid #600;border-bottom:50px solid #f00;border-top:50px solid #00f;width:0px;height:0px;"></div>
再看圓形系列。它與畫三角形的原理相似,主要利用 CSS3.0 的圓角屬性 border-radius,適當調整大小會有不同效果,結合 border-width 還能得到更多圖形:
/*圓形*/ .circle { width: 100px; height: 100px; background: #669; -moz-border-radius: 50px; -webkit-border-radius: 50px; border-radius: 50px; } .circle-circle { width: 100px; height: 100px; border:20px solid #669;background: #fff; -moz-border-radius: 100px; -webkit-border-radius: 100px; border-radius: 100px; } .circle-up { width: 100px; height: 0px;border:0 solid transparent;border-top:100px solid #669;border-right:100px solid #669; -moz-border-radius: 100px 100px 0 0; -webkit-border-radius: 100px 100px 0 0; border-radius: 100px 100px 0 0; } .circle-lefttop { width: 100px; height: 0px;border:0 solid transparent;border-top:100px solid #669; border-radius:100px 0 0 0; }
透過調整邊框寬度與圓角,還能得到「小尾巴」與提示泡效果。提示泡的原理多了 :after 偽類,並利用 CSS3.0 的 transform:rotate 旋轉角度:
.pop{width:200px;height:100px;border-radius:20px;background:#669;margin-top:20px;position:relative} .pop:after{content: "";border: 0 solid transparent; border-bottom:30px solid #669;border-radius:0 0 0 200px;width:50px;height:50px;position:absolute;top:50px;-webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg);}
此外還可延伸出橢圓提示泡,其中 border-radius: 100px / 50px; 的「/」符號在 CSS 中較少見——圓角有水平與垂直兩個方向,「/」前代表水平、後代表垂直。
.oval-pop{width: 200px; height: 100px; background: #669; border-radius: 100px / 50px; margin-bottom:50px;position:relative}
由此可見,用 CSS 畫圖形其實就是對屬性的組合創造。再看膠囊形,它是畫圓與圓角矩形的延伸:
.capsule{border: 0 solid transparent; border-radius:100px;width:200px;height:70px;background:#669;} .up-capsule{border: 0 solid transparent; border-radius:100px 100px 0 0;width:70px;height:120px;background:#669;} .r45-capsule{border-radius:100px 100px 0 0;width:70px;height:120px;background:#669;transform: rotate(45deg);}
仔細觀察最後兩個傾斜的半膠囊,合在一起就是一個心形:
.heart{width:160px;height:200px;position:relative} .heart:before{content:" ";border-radius:100px 100px 0 0;width:80px;height:120px;background:#669;transform: rotate(-45deg);position:absolute;left:20px;} .heart:after{content:" ";border-radius:100px 100px 0 0;width:80px;height:120px;background:#669;transform: rotate(45deg);position:absolute;left:48px;top:0px;}
以上只是對 CSS 畫圖形的初步認識,基本上所有實現都是對邊框角度的調整與組合。多試幾次,相信每個人都能掌握這個技能!