網頁中經常用到三角形的地方,用css寫三角形的方法很簡單
html:
<span class="u-tri"></span>
css:
.u-tri{
display: inline-block;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-left: 20px solid transparent;
border-right: 20px solid red;
}
效果圖:
想要三角形的尖角方向朝哪邊,對應的相反一邊設置顏色。比如尖角向上,則boder-bottom的color上色,其它三邊爲透明。
實例:對話框背景
先上最後的效果圖:
完整代碼:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
margin: 20px;
}
.box{
position: relative;
display: inline-block;
max-width: 200px;
padding: 4px 8px;
border-radius: 2px;
background-color: green;
line-height: 20px;
font-size: 14px;
}
.u-tri:before{
position: absolute;
left: -24px;
top: 4px;
content: '';
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 12px solid transparent;
border-right: 12px solid green;
}
</style>
</head>
<body>
<div>
<p class="box u-tri">測試</p>
</div>
<div>
<p class="box u-tri">nizahhhshs 哈哈哈哈哈哈我只是個則是的我只是個可愛的測試案列e</p>
</div>
</body>
</html>