一、基礎
<!DOCTYPE html> <html> <head> <title>basic shapes</title> <style type="text/css"> div{ box-sizing: border-box; } .div1{ width: 100px; height: 100px; border-top: 50px solid red; border-right: 50px solid blue; border-bottom: 50px solid yellow; border-left: 50px solid green; } .div2{ width: 100px; height: 100px; border-right: 50px solid blue; border-bottom: 100px solid yellow; border-left: 50px solid green; } .div3{ width: 100px; height: 100px; border-right: 25px solid blue; border-bottom: 100px solid yellow; border-left: 25px solid green; } </style> </head> <body> <div class="div1">div1</div> <span>border的神奇之處</span> <hr> <div class="div2">div2</div> <span>黃色三角形,底邊100px高100px</span> <hr> <div class="div3">div3</div> <span>黃色的等腰梯形,上底50,下底100,高100</span> </body> </html>
上面代碼的效果是這樣的
border是如何工作的,你可以從上面的結果自行體會。
二、特殊三角形
<!DOCTYPE html> <html> <head> <title>三角形</title> <style type="text/css"> div{ box-sizing: border-box; margin:20px; } .container::after{ content: ""; display: block; clear: both; } .div1{ width: 100px; height: 0; border-bottom: 100px solid red; border-left: 50px solid transparent; border-right: 50px solid transparent; float: left; } .div2{ width: 0; height: 100px; border-bottom: 50px solid transparent; border-left: 100px solid blue; border-top: 50px solid transparent; float: left; } .div3{ width: 100px; height: 0; border-top: 100px solid yellow; border-left: 50px solid transparent; border-right: 50px solid transparent; float: left; } .div4{ width: 0; height: 100px; border-bottom: 50px solid transparent; border-right: 100px solid green; border-top: 50px solid transparent; float: left; } .div5{ width: 100px; height: 0; border-bottom: 86.6px solid red; border-left: 50px solid transparent; border-right: 50px solid transparent; float: left; } .div6{ width: 100px; height: 0; border-bottom: 100px solid blue; /*border-left: 50px solid transparent;*/ border-right: 100px solid transparent; float: left; } .div7{ width: 100px; height: 0; border-bottom: 100px solid blue; /*border-left: 50px solid transparent;*/ border-right: 100px solid transparent; float: left; transform: rotate(135deg); transform-origin: 50% 50%; } </style> </head> <body> <div class="container"> <h4>等腰三角形</h4> <div class="div1"></div> <div class="div2"></div> <div class="div3"></div> <div class="div4"></div> </div> <div class="container"> <h4>其他特殊三角形</h4> <div class="div5">等邊</div> <div class="div6">直角</div> <div class="div7">直角</div> </div> </body> </html>
等腰三角形我做了4個方向的,當然你也可以用旋轉來做,就像div7那樣。製作三角形比較簡單,只要對我前面將的基礎加以改造就可以了。
三、特殊四邊形
<!DOCTYPE html> <html> <head> <title>四邊形</title> <style type="text/css"> div{ box-sizing: border-box; margin:20px; } .container::after{ content: ""; display: block; clear: both; } .div1{ background: transparent; width: 0; height: 100px; border-bottom: 50px solid transparent; border-right: 100px solid green; border-top: 50px solid transparent; float: left; position: relative; box-sizing: border-box; } .div1::after{ content: ""; position: absolute; top: -50px; left: 100px; background: transparent; width: 0; height: 100px; border-bottom: 50px solid transparent; border-left: 100px solid green; border-top: 50px solid transparent; float: left; position: relative; box-sizing: border-box; } .div2{ background: transparent; width: 80px; height: 0; border-bottom: 50px solid green; border-right: 40px solid transparent; border-left: 40px solid transparent; float: left; position: relative; box-sizing: border-box; margin-left: 100px; } .div2::after{ content: ""; position: absolute; top: 50px; left: -40px; width: 80px; height: 0; border-top: 50px solid green; border-right: 40px solid transparent; border-left: 40px solid transparent; box-sizing: border-box; } .div5{ width: 100px; height: 0; border-bottom: 100px solid green; border-left: 25px solid transparent; border-right: 25px solid transparent; float: left; } .div6{ width: 100px; height: 0; border-bottom: 100px solid green; border-right: 40px solid transparent; float: left; } .div7{ width: 100px; height: 100px; background: green; transform: skew(30deg,0); float: left; } </style> </head> <body> <div class="container"> <h4>菱形</h4> <div class="div1"></div> <div class="div2"></div> </div> <div class="container"> <h4>梯形和平行四邊形</h4> <div class="div5">等腰</div> <div class="div6">直角</div> <div class="div7">平行四邊形</div> </div> </body> </html>
四、圓和橢圓
<!DOCTYPE html> <html> <head> <title>圓和橢圓</title> <style type="text/css"> .div1{ width: 50px; height: 50px; border-radius: 25px; box-sizing: border-box; background: red; float: left; } .div2 { width: 100px; height: 80px; background: green; border-radius: 50px / 40px; float: left; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> </body> </html>
科普一下:http://www.css88.com/book/css/properties/border/border-radius.htm
以前都只用一個參參數的,所以寫博客可以幫助查漏補缺。
五、複雜一點的圖形
1、正多邊形
<!DOCTYPE html> <html> <head> <title>正多邊形</title> <style type="text/css"> .five { top: 50px; position: relative; width: 54px; border-width: 50px 18px 0; border-style: solid; border-color: red transparent; } .five::before { content: ""; position: absolute; height: 0; width: 0; top: -85px; left: -18px; border-width: 0 45px 35px; border-style: solid; border-color: transparent transparent green; } .six{ width: 100px; height: 0px; border-width:0px 18.59px 60px; border-style: solid; border-color: transparent transparent red; margin-top: 60px; position: relative; } .six::after{ content: ""; width: 100px; height: 0px; border-width:60px 18.59px 0px; border-style: solid; border-color: green transparent transparent; position: absolute; top: 60px; left: -18.59px; } </style> </head> <body> <div class="five"></div> <div class="six"></div> </body> </html>
正五邊形是借鑑的大神的(不知道是不是正的,我沒算),思路是做一個梯形,然後拼接一個三角形,正六邊形是我根據他的思路用兩個梯形拼接的,如果我沒有計算錯誤這應該是一個正六邊形(怎麼有點不像呢),計算目的是保證,腰和短底長相等。我刻意用不同顏色表示了拆分情況,便於理解。正 八變形怎麼做?只需要拆解成兩個梯形和一個矩形就好做了。正n邊形怎麼做,也是把它看作基本圖形的組合,只不過可能單靠兩個僞元素已經不夠用了。
2、大大的愛心送給你
.aixin{ border-width: 200px 150px 0; border-style: solid;; border-color: red transparent; width: 0px; position: relative; top: 200px; left: 100px; } .aixin::before{ content: ""; width: 180px; height: 180px; display: block; border-radius: 90px; background: red; position: absolute; top: -357px; left: -180px; } .aixin:after{ content: ""; width: 180px; height: 180px; display: block; border-radius: 90px; background: red; position: absolute; top: -357px; left: 0; }
數據沒算,調整出來的。人要實心啊,搞個空心太不像話了,你自己去弄吧。兩個圈圈加個三角形就可以了,原來愛這麼簡單。
3、對話框
<!DOCTYPE html> <html> <head> <title>對話框</title> <style type="text/css"> .div1{ width: 300px; height: 100px; border: 1px solid #97FFFF; position: relative; left: 100px; border-radius: 10px; } .div1::after,.div1::before{ content: ""; display: block; width: 0; height: 0; border-style: solid; border-color:transparent #97FFFF transparent transparent; position: absolute; top: 30px; left: -50px; } .div1::after{ border-width: 21px 50px 21px 0; } .div1::before{ top: 31px; border-width: 20px 50px 20px 0; border-color: transparent white transparent transparent; z-index: 1000; left: -49px; } .div2{ width: 300px; height: 100px; border: 1px solid #97FFFF; position: relative; left: 100px; border-radius: 10px; margin-top: 20px; } .div2::after,.div2::before{ content: ""; display: block; width: 0; height: 0; border-style: solid; border-color:transparent transparent transparent #97FFFF; position: absolute; top: 30px; left: 300px; } .div2::after{ border-width: 21px 0px 21px 50px; } .div2::before{ top: 31px; border-width: 20px 0px 20px 50px; border-color: transparent transparent transparent white; z-index: 1000; left: 299px; } </style> </head> <body> <div class="div1"> </div> <div class="div2"></div> </body> </html>
六、小結一下
用CSS還是畫出來很多東西的,對於稍微複雜點的圖形用拼接或者裁剪的方法實現,有時可能會需要一些簡單計算。本文所提到的只是九牛一毛,早有一些朋友做出了許多非常炫酷的圖形,你可以去搜搜看。就寫到這裏,留幾個大神的作品,以備不時只需。
參考: