用CSS3繪製圖形,CSS3繪製圖形

來源:互聯網
上載者:User

用CSS3繪製圖形,CSS3繪製圖形

參考資料:http://blog.csdn.net/fense_520/article/details/37892507

本文非轉載,為個人原創,轉載請先聯絡博主,謝謝~

準備:

<!DOCTYPE html><head><meta charset="UTF-8"><title>- UED - </title><style type="text/css" src="css/style.css"></style><script type="text/javascript" src="js/ext.js"></script></head><div class="xxx"></div>
xxx按需更改
div { background:black;}

好,我們開始畫圖吧!


1. 矩形

.rectangle { width:100px; height:100px;}

2. 圓形

.circle { width:100px; height:100px; border-radius:50px;}

圖 13. 圓柱形
.cylinder {width:100px; height:100px; border-radius:100px/50px;}
圖 2-1 圖2-2 圖2-3橙色為border-radius:100px/50px時的情況,紫色為border-radius:50px/100px的情況,值分別是水平半徑和垂直半徑,在正方形內圈出了一個圓柱形,可以發現,border-radius總保持它的圓心在中心了。為什麼不會是圖2-2的樣子呢<待解決1>?當我們設定border-radius:100px/100px的時候,你會驚奇地發現,它又變回一個圓形了,你會以為它是圖2-3的樣子的,但實際上不是,它跟圖1的圓形一樣大小,這又是為什麼呢<待解決2>?4. 橢圓形
.oval {width:200px; height:100px; border-radius:100px/50px;}
跟圓柱形差不多,把正方形的寬擴張到200px,它正好容納了那個橢圓,橢圓就是畫出來的橢圓啦!5. 三角形
[class^=triangle-] { width:0px; height:0px;}
先以上三角形為例:
.triangle-up { border-left:50px solid transparent; border-right:50px solid transparent; border-bottom:100px solid red;}
這個三角形是怎麼來的呢?我細細研究了一下,發現border之間有一條分界,而這條分界是45度傾斜的,-3,兩border間有明顯分界,這就是我們畫三角形所要利用到的原理,上面已經設定width和height為0,這時兩border交匯處就有一個由兩個三角形組成的矩形啦,設定其中一個border為透明,就剩下一個三角形了。對,就是這麼簡單,那這個上三角形該怎麼畫?設定左右border和border-bottom,讓左右border變透明,這時你們看到的上三角形其實是由兩個三角形組成的,-4。其他種類的三角形類似,你看著哪幾個border能組成這個三角形順便調整一下border-width就行了。曾經有一道筆試題——畫出一條絲帶,現在想想挺簡單的,不就是一個黃色左三角形+黃色矩形+白色左三角形定位做成的嘛,如下:
div { float:left;}#test { border-top:100px solid transparent; border-bottom:100px solid transparent; border-right:100px solid yellow;}#test2 { width:800px; height:200px; background:yellow;}#test3 { border-top:100px solid transparent; border-bottom:100px solid transparent; border-right:100px solid white; margin-left:-100px;}

圖-3

圖-46. 平行四邊形——現成的屬性——transform:skew(20deg) 左傾斜的意思7. 梯形其實跟上面說的上三角形一個道理,不過設定它的width為100px,height為0,也就是兩個三角形加一個width為100px,height為border-bottom寬度的矩形畫成的。8.六角星
.star-six { width:0; height:0; border-left:50px solid transparent; border-right:50px solid transparent; border-bottom:100px solid red; position:relative;}.star-six:after { width:0; height:0; border-left:50px solid transparent; border-right:50px solid transparent; border-top:100px solid red; position:absolute; content:""; top:30px; left:-50px;}
第一行:建立一個上三角形,粉色部分(中間被遮擋了),position為relative。第二行:偽類的位置為absolute,因star-six類的寬高均為0,偽類的起始位置在圖-5所示的圓圈中,要明白一個原理,若star-six寬高不為0,偽類塊是在star-six內部的,因為分別設定了position為relative和absolute,這時兩三角形組成了個平行四邊形,這也算是畫平行四邊形的一種方法吧。當我嘗試去掉position:absolute,發現偽類元素只剩下左邊的一半了,而且位於star-six元素的右上方,這是為什麼<待解決3>?(before是在後面,而after是在前面的?真奇怪) 答:看走眼了,:after確實是在後面了。接下來就是定位了top:30px; left:-50px; 將原三角形移動到綠色三角形所示位置,這時他們組成了一個六角形。其他:content:"",這個一定要有,若沒有就是normal了,網上看到有篇文章寫到”content:normal將 :before 和 :after 虛擬元素計算成‘none’”,而content:none的虛擬元素是不會被產生的,關於content屬性,我也不太懂,各位自行尋找資料。
圖-59. 五角星
.star-five { margin:50px 0; position:relative; width:0px; height:0px; border-left:100px solid transparent; border-right:100px solid transparent; border-bottom:70px solid red;    -webkit-transform:rotate(35deg);        -moz-transform:rotate(35deg);         -ms-transform:rotate(35deg);          -o-transform:rotate(35deg); }  .star-five:before { border-bottom:80px solid orange; border-left:30px solid transparent; border-right:30px solid transparent; position:absolute; height:0; width:0; top:-45px; left:-65px; display:block; content:'';     -webkit-transform:rotate(-35deg);        -moz-transform:rotate(-35deg);         -ms-transform:rotate(-35deg);          -o-transform:rotate(-35deg); }  .star-five:after { border-bottom:70px solid green; border-left:100px solid transparent; border-right:100px solid transparent; position:absolute; display:block; color:red; top:3px; left:-105px; width:0px; height:0px; content:'';    -webkit-transform:rotate(-70deg);        -moz-transform:rotate(-70deg);         -ms-transform:rotate(-70deg);          -o-transform:rotate(-70deg); }
使用前面的提到的方法,分別繪製三角形再旋轉和定位而成,代碼所示,:before偽類元素畫出的是五角星頂部的小三角形,我們也可以把:before畫成跟star-five和:after一樣大小的三角形再旋轉定位。10. 五角大樓(或者說是五邊形)——由兩個梯形組成(或者你用各種奇形怪狀拼湊),沒什麼好說的了。11. 六邊形 = 上三角形 + 矩形 + 下三角形12. 八邊形 = 上梯形 + 矩形 + 下梯形13. 愛心 = 兩個擁有兩圓角的矩形,-6,(transform-oritin定義旋轉的基準點,對rotate屬性起作用)
圖-614. 無窮符號 = 兩個矩形(分別擁有3個圓角和一個直角)15. 雞蛋 =  一個帶圓角的矩形16. 藥丸
.pill { width:0px; height:0px; border-right:60px solid transparent; border-top:60px solid red; border-bottom:60px solid red; border-radius:60px;}
圖-7這個我也不知道是怎麼形成的,照理說,它只有上邊、下邊和右邊的相交部分,border-xxx-left應該沒作用才是,但他又確實起作用了,對這個變化有點納悶,-7後兩步<待解決4>。17. 對話方塊 = 圓角矩形 + 左三角形18. 鑽石 = 梯形 + 下三角形

我覺得用css3畫圖形在實際應用中沒什麼大的用處,純屬用來練手好了(或許還可以應付幾道筆試題呢)。
最後,謝謝你認真看完了,不足之處,歡迎指出!順便幫我解答幾個待解決問題。


完整代碼:
div { background:black;}/*矩形*/.rectangle { width:100px; height:100px;}/*圓形*/.circle { width:100px; height:100px; border-radius:50px;}/*圖柱形*/  .cylinder {width:100px; height:100px; border-radius:100px/50px;}  /*橢圓形*/  .oval {width:200px; height:100px; border-radius:100px/50px;}/*各種三角形*/[class^=triangle-] { width:0px; height:0px;}.triangle-up { border-left:50px solid transparent; border-right:50px solid transparent; border-bottom:100px solid red;}.triangle-down { border-left:50px solid transparent; border-right:50px solid transparent; border-top:100px solid red;}.triangle-equal { border-left:50px solid transparent; border-right:50px solid transparent; border-bottom:50px solid red;}.triangle-left { border-top:50px solid transparent; border-right:100px solid red; border-bottom:50px solid transparent;}.triangle-right { border-top:50px solid transparent; border-left:100px solid red; border-bottom:50px solid transparent;}.triangle-left-up { border-top:100px solid red; border-right:200px solid transparent;}  .triangle-right-up { border-top:100px solid red; border-left:200px solid transparent;}  .triangle-left-down { border-bottom:100px solid red; border-right:200px solid transparent;}  .triangle-right-down { border-bottom:100px solid red; border-left:200px solid transparent;}/*平行四邊形*/  .parallelogram { width:150px; height:100px; margin-left:20px;      -webkit-transform: skew(20deg);       -moz-transform: skew(20deg);         -o-transform: skew(20deg); }/*梯形*/.trapezoid { width:100px; height:0px; border-bottom:100px solid red; border-left:50px solid transparent; border-right:50px solid transparent;}  /*六角星*/.star-six { width:0; height:0; border-left:50px solid transparent; border-right:50px solid transparent; border-bottom:100px solid red; position:relative;}  .star-six:after { width:0; height:0; border-left:50px solid transparent; border-right:50px solid transparent; border-top:100px solid red; position:absolute; content:""; top:30px; left:-50px;}/*五角星*/.star-five { margin:50px 0; position:relative; width:0px; height:0px; border-left:100px solid transparent; border-right:100px solid transparent; border-bottom:70px solid red;    -webkit-transform:rotate(35deg);        -moz-transform:rotate(35deg);         -ms-transform:rotate(35deg);          -o-transform:rotate(35deg); }  .star-five:before { border-bottom:80px solid orange; border-left:30px solid transparent; border-right:30px solid transparent; position:absolute; height:0; width:0; top:-45px; left:-65px; display:block; content:'';    -webkit-transform:rotate(-35deg);        -moz-transform:rotate(-35deg);         -ms-transform:rotate(-35deg);          -o-transform:rotate(-35deg); }  .star-five:after { border-bottom:70px solid green; border-left:100px solid transparent; border-right:100px solid transparent; position:absolute; display:block; color:red; top:3px; left:-105px; width:0px; height:0px; content:'';    -webkit-transform:rotate(-70deg);        -moz-transform:rotate(-70deg);         -ms-transform:rotate(-70deg);          -o-transform:rotate(-70deg); }/*五角大樓*/.pentagon { position:relative; width:54px; border-width:50px 18px 0; border-style:solid; border-color:red transparent;}  .pentagon:before { content:""; position:absolute; height:0; width:0; top:-85px; left:-18px; border-width:0 45px 35px; border-style:solid; border-color:transparent transparent red;}  /*六邊形*/  .hexagon { width:100px; height:55px; background:red; position:relative;}  .hexagon:before { content:""; position:absolute; top:-25px; left:0; width:0; height:0; border-left:50px solid transparent; border-right:50px solid transparent; border-bottom:25px solid red;}  .hexagon:after { content:""; position:absolute; bottom:-25px; left:0; width:0; height:0; border-left:50px solid transparent; border-right:50px solid transparent; border-top:25px solid red;}  /*八邊形*/.octagon { width:100px; height:100px; background:red; position:relative;}    .octagon:before { content:""; position:absolute; top:0; left:0; border-bottom:29px solid red; border-left:29px solid #eee; border-right:29px solid #eee; width:42px; height:0;}.octagon:after { content:""; position:absolute; bottom:0; left:0; border-top:29px solid red; border-left:29px solid #eee; border-right:29px solid #eee; width:42px; height:0;}/*愛心*/  .heart { position:relative; width:100px; height:90px;}.heart:before { position:absolute; content:""; left:50px; top:0; width:50px; height:80px; background:red;    -moz-border-radius:50px 50px 0 0;         border-radius:50px 50px 0 0;      -webkit-transform: rotate(-45deg);         -moz-transform: rotate(-45deg);          -ms-transform: rotate(-45deg);           -o-transform: rotate(-45deg);              transform: rotate(-45deg);      -webkit-transform-origin:0 100%;         -moz-transform-origin:0 100%;          -ms-transform-origin:0 100%;           -o-transform-origin:0 100%;              transform-origin:0 100%; }  .heart:after { left:0; content:""; position:absolute; width:50px; height:80px;    background:red;    -moz-border-radius:50px 50px 0 0;         border-radius:50px 50px 0 0;      -webkit-transform: rotate(45deg);         -moz-transform: rotate(45deg);        -ms-transform: rotate(45deg);           -o-transform: rotate(45deg);            transform: rotate(45deg);      -webkit-transform-origin:100% 100%;         -moz-transform-origin:100% 100%;          -ms-transform-origin:100% 100%;           -o-transform-origin:100% 100%;              transform-origin :100% 100%; }/*無窮大符號*/  .infinity { position:relative; width:212px; height:100px; }  .infinity:before { width:60px; height:60px; border:20px solid red; content:""; position:absolute;    -moz-border-radius:50px 50px 0 50px;           border-radius:50px 50px 0 50px;      -webkit-transform: rotate(-45deg);         -moz-transform: rotate(-45deg);          -ms-transform: rotate(-45deg);           -o-transform: rotate(-45deg);              transform: rotate(-45deg); }  .infinity:after { right:auto; width:60px; height:60px; border:20px solid red; content:""; position:absolute;    -moz-border-radius:50px 50px 50px 0;         border-radius:50px 50px 50px 0;    -webkit-transform: rotate(45deg);       -moz-transform: rotate(45deg);          -ms-transform: rotate(45deg);         -o-transform: rotate(45deg);            transform: rotate(45deg); }/*雞蛋*/  .egg { width:126px; height:180px; background-color:red;    -webkit-border-radius:63px 63px 63px 63px/108px 108px 72px72px;             border-radius:50% 50% 50% 50%/60% 60% 40% 40%; } /*藥丸*/  .pill { width:0px; height:0px; border-right:60px solid transparent; border-top:60px solid red; border-bottom:60px solid red;    border-top-left-radius:60px;    border-top-right-radius:60px;    border-bottom-left-radius:60px;    border-bottom-right-radius:60px; }/*對話方塊*/  .talkbubble { width:120px; height:80px; background:red; position:relative;    -webkit-border-radius:10px;       -moz-border-radius:10px;            border-radius:10px;}  .talkbubble:before { content:""; position:absolute; right:100%; top:26px; width:0; height:0; border-top:13px solid transparent; border-right:26px solid red; border-bottom:13px solid transparent;}/*鑽石*/.cut-diamond { border-style:solid; border-color:transparent transparent red transparent; border-width:0 25px 25px 25px; height:0; width:50px; position:relative; margin:20px 0 50px 0;}.cut-diamond:after { content:""; position:absolute; top:25px; left:-25px; width:0; height:0; border-style:solid; border-color:red transparent transparent transparent; border-width:70px 50px 0 50px;}



幫忙用純 CSS3 畫出這個圖案,,像的話加分

<div style="border:25px solid white; width:0; height:0; float:left; border-right-color:black;"></div>
<div style="height:50px; width:80px; background:black; border-top-right-radius:8px; border-bottom-right-radius:8px; display:inline-block; float:left;"></div>

哈哈,畫了一個,樓主收好了。

希望對你有協助哦~~
 
css3 幫畫一個水滴圖型

<html>
<body>
<style type="text/css">

span{height:40px; width:40px; display:block; position:relative;}

.demoSpan1{width:26px;}

.demoSpan1:before{content:'1'; height:26px; width:26px; display:block; position:absolute; top:2px; left:0px; z-index:1; line-height:26px; background:#333; border-radius:40px;-webkit-border-radius:40px;-moz-border-radius:40px; color:#fff; text-align:center;}

.demoSpan1:after{content:''; height:0px; width:0px; display:block; position:absolute; bottom:2px; left:3px; border:10px transparent solid; border-top-color:#333; border-width:15px 10px 0px 10px; }

</style>
<span class="demoSpan1">
</span>
</body>
</html>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.