CSS 实现三角形的2种方法

一、利用border
1、元素本身的长宽为0
2、将不需要的部分通过 border-color 来设置隐藏

//html
//css #first { width: 20px; height: 20px; border-width: 10px; border-style: solid; border-color: red green blue brown; } #second { width: 0; height: 0; border-width: 10px; border-style: solid; border-color: red green blue brown; } #third { width: 0; height: 0; border-width: 10px; border-style: solid; border-color: red transparent transparent transparent; }

效果:


Paste_Image.png

二、利用transform旋转 45 度实现三角形
我们来实现一个带小三角的提示框

//html
我是利用 css transfrom 实现的
//css .message-box { position:relative; width:240px; height:60px; line-height:60px; background:#E9FBE4; box-shadow:1px 2px 3px #E9FBE4; border:1px solid #C9E9C0; border-radius:4px; text-align:center; color:#0C7823; } .triangle-css3 { position:absolute; bottom:-8px; bottom:-6px; left:30px; overflow:hidden; width:13px; height:13px; background:#E9FBE4; border-bottom:1px solid #C9E9C0; border-right:1px solid #C9E9C0; } .transform { -webkit-transform:rotate(45deg); -moz-transform:rotate(45deg); -o-transform:rotate(45deg); transform:rotate(45deg); } /*ie7-9*/ .ie-transform-filter { -ms-filter: "progid:DXImageTransform.Microsoft.Matrix( M11=0.7071067811865475, M12=-0.7071067811865477, M21=0.7071067811865477, M22=0.7071067811865475, SizingMethod='auto expand')"; filter: progid:DXImageTransform.Microsoft.Matrix( M11=0.7071067811865475, M12=-0.7071067811865477, M21=0.7071067811865477, M22=0.7071067811865475, SizingMethod='auto expand'); }
Paste_Image.png

另:利用transform实现一个四边形
主要是借助了transform: skew(...)

//html
上海
//css .city { display: inline-block; padding: 5px 20px; border: 1px solid #44a5fc; color: #333; transform: skew(-20deg); }
Paste_Image.png

最后,附上jsfiddle示例
https://jsfiddle.net/fozero/d2hbda2v/

参考:
http://m.blog.csdn.net/article/details?id=51490107
http://web.jobbole.com/83248/

你可能感兴趣的:(CSS 实现三角形的2种方法)