網(wǎng)頁(yè)中對(duì)話氣泡框下邊的小三角是非常有用的,繪制的方法很多,想什么字體圖標(biāo)、小圖片之類的都可以,但是都不夠靈活和快捷,有了 css3 之后繪制一個(gè)三角形就非常簡(jiǎn)單了
利用純 css 創(chuàng)建一個(gè)三角形
首先當(dāng)然是一個(gè)任意的空標(biāo)簽來(lái)當(dāng)小三角,下邊的 css 丟進(jìn)去即可:
代碼如下:
.triangle{
left:50%;
margin-left:-5px;
width:5px;
height:5px;
background:#fafafa;
bottom:-4px;
position:absolute;
transform:rotate(45deg);
-ms-transform:rotate(45deg);
-moz-transform:rotate(45deg);
-webkit-transform:rotate(45deg);
-o-transform:rotate(45deg);
border:1px solid #e5e5e5;
border-top:none;
border-left:none
}
代碼利用 css3 的 2d 旋轉(zhuǎn)屬性對(duì)正方形進(jìn)行旋轉(zhuǎn),并且隱藏掉頂部左右兩邊的邊框就可以實(shí)現(xiàn)小三角了。
例子
代碼如下:
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<script type=text/javascript src=/libs/jquery/1.4.4/jquery.min.js></script>
<title>三種純css實(shí)現(xiàn)三角形的方法</title>
<link rel=stylesheet >
<style type=text/css>
.page-info { padding:8px 15px; background:#fcea9e; border-bottom:1px solid #b6bac0; font:normal 16px georgia; }
.page-info h1 { float:left; }
.page-info a { font-weight:bold; color:#222; }
.return-article { float:right; }
.return-article h2{ display:inline; }
/*border模擬tips*/
.message-box { position:relative; float:left; margin:80px 0 0 100px; 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-border { position:absolute; left:30px; overflow:hidden; width:0; height:0;
border-width:10px; border-style:solid dashed dashed dashed; }
.tb-border { bottom:-20px; border-color:#c9e9c0 transparent transparent transparent; }
.tb-background { bottom:-19px; border-color:#e9fbe4 transparent transparent transparent; }
/*字符模擬tips*/
.triangle-character { position:absolute; left:30px; overflow:hidden; width:26px; height:26px; font:normal 26px 宋體; }
.tc-background { bottom:-12px; color:#e9fbe4; }
.tc-border { bottom:-13px; color:#c9e9c0;}
/*css3 transfrom 模擬tips*/
.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');
}
</style>
</head>
<body>
<div class=message-box>
<span>我是利用 border 屬性實(shí)現(xiàn)的</span>
<div class=triangle-border tb-border></div>
<div class=triangle-border tb-background></div>
</div>
<div class=message-box>
<span>我是利用 ◆ 字符實(shí)現(xiàn)的</span>
<div class=triangle-character tc-border>◆</div>
<div class=triangle-character tc-background>◆</div>
</div>
<div class=message-box>
<span>我是利用 css transfrom 屬性實(shí)現(xiàn)的</span>
<div class=triangle-css3 transform ie-transform-filter></div>
</div>
</body>
</html>