老衲前端web实验室- 各种CSS多边形写法
Apr 4, 2014
老衲前端在之前的文章中介绍过如何使用css绘制一个等腰直角三角形(有需要的朋友,可以猛点几下),本文沿着这个思路扩展一下,可以总结出各种CSS多边形写法。最终效果也是受到群里基佬的启发,以及一些demo的演示。
基本的CSS多边形写法,如正方形、长方形、等腰三角形(也可以任意三角),就不在此赘述了。注:之前的文中提到,可以通过同时设置两个边框的border-color属性来绘制三角形,还可以只设置一边的border-color属性为transparent或者背景颜色,这样就可以输出下面这种形状的三角形(左下角三角形)。
CSS代码如下:
.triangle{ width:0; height: 0; border-bottom: 100px solid #E56212; border-right: 100px solid transparent; }
通过以上CSS(CSS多边形写法)代码不再边框的变换,我们是可以得到这种4个不角度的三角形状的。下面就一一列出我们今天的主题:CSS多边形写法,所绘制出的各种形状,好处自不必多说,减少了图片文件请求,可调整性强,特别是对如今,大为流行的扁平化的易用性更强。下面用到的CSS伪元素after和before,在IE8以下的不适用的,弥补的办法就是使用标签替换after和before。
一、绘制梯形:
.triangle { border-bottom: 100px solid #F36823; border-left: 50px solid transparent; border-right: 50px solid transparent; height: 0; width: 100px; }
二、六角星:
.triangle{ width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid red; position: relative; } .triangle: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; }
三、五角星:
.triangle{ margin: 50px 0; position: relative; display: block; color: #F36823; width: 0px; height: 0px; border-right: 100px solid transparent; border-bottom: 70px solid #F36823; border-left: 100px solid transparent; -moz-transform: rotate(35deg); -webkit-transform: rotate(35deg); -ms-transform: rotate(35deg); -o-transform: rotate(35deg); } .triangle:before { border-bottom: 80px solid #F36823; 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); } .triangle:after { position: absolute; display: block; color: #F36823; top: 3px; left: -105px; width: 0px; height: 0px; border-right: 100px solid transparent; border-bottom: 70px solid #F36823; border-left: 100px solid transparent; -webkit-transform: rotate(-70deg); -moz-transform: rotate(-70deg); -ms-transform: rotate(-70deg); -o-transform: rotate(-70deg); content: ''; }
四、五角形:
.triangle{ position: relative; width: 54px; border-width: 50px 18px 0; border-style: solid; border-color: #F36823 transparent; } .triangle:before { content: ""; position: absolute; height: 0; width: 0; top: -85px; left: -18px; border-width: 0 45px 35px; border-style: solid; border-color: transparent transparent #F36823; }
五、六边形:
.triangle{ width: 100px; height: 55px; background: #F36823; position: relative; } .triangle: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 #F36823; } .triangle: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 #F36823; }
六、八边形
.triangle{ width: 100px; height: 100px; background: #F36823; position: relative; } .triangle:before { content: ""; position: absolute; top: 0; left: 0; border-bottom: 29px solid #F36823; border-left: 29px solid #E3DED2; border-right: 29px solid #E3DED2; width: 42px; height: 0; } .triangle:after { content: ""; position: absolute; bottom: 0; left: 0; border-top: 29px solid #F36823; border-left: 29px solid #E3DED2; border-right: 29px solid #E3DED2; width: 42px; height: 0; }
七、心形:
#heart { position: relative; width: 100px; height: 90px; } #heart:before, #heart:after { position: absolute; content: ""; left: 50px; top: 0; width: 50px; height: 80px; background: #F36823; -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; -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, #infinity:after { content: ""; position: absolute; top: 0; left: 0; width: 60px; height: 60px; border: 20px solid #F36823; -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 { left: auto; right: 0; -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); }
九、食人豆:
#pacman { width: 0px; height: 0px; border-right: 60px solid transparent; border-top: 60px solid #F36823; border-left: 60px solid #F36823; border-bottom: 60px solid #F36823; 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; -moz-border-radius: 10px; -webkit-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; }
十一、12角星:
#burst-12 { background: red; width: 80px; height: 80px; position: relative; text-align: center; } #burst-12:before, #burst-12:after { content: ""; position: absolute; top: 0; left: 0; height: 80px; width: 80px; background: red; } #burst-12:before { -webkit-transform: rotate(30deg); -moz-transform: rotate(30deg); -ms-transform: rotate(30deg); -o-transform: rotate(30deg); transform: rotate(30deg); } #burst-12:after { -webkit-transform: rotate(60deg); -moz-transform: rotate(60deg); -ms-transform: rotate(60deg); -o-transform: rotate(60deg); transform: rotate(60deg); }
十二、8角星:
#burst-8 { background: #F36823; width: 80px; height: 80px; position: relative; text-align: center; -webkit-transform: rotate(20deg); -moz-transform: rotate(20deg); -ms-transform: rotate(20deg); -o-transform: rotate(20eg); transform: rotate(20deg); } #burst-8:before { content: ""; position: absolute; top: 0; left: 0; height: 80px; width: 80px; background: #F36823; -webkit-transform: rotate(135deg); -moz-transform: rotate(135deg); -ms-transform: rotate(135deg); -o-transform: rotate(135deg); transform: rotate(135deg); }
十三、钻石:
#cut-diamond { border-style: solid; border-color: transparent transparent #F36823 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: #F36823 transparent transparent transparent; border-width: 70px 50px 0 50px; }
十四、阴阳八卦
#yin-yang { width: 96px; height: 48px; background: #eee; border-color: #F36823; border-style: solid; border-width: 2px 2px 50px 2px; border-radius: 100%; position: relative; } #yin-yang:before { content: ""; position: absolute; top: 50%; left: 0; background: #eee; border: 18px solid #F36823; border-radius: 100%; width: 12px; height: 12px; } #yin-yang:after { content: ""; position: absolute; top: 50%; left: 50%; background: #F36823; border: 18px solid #eee; border-radius:100%; width: 12px; height: 12px; }