巧用CSS实现各种形状的三角形
先看效果,选择你所需要的形状
实现原理
使用border实现,通过显示隐藏不同方向的border来实现不同形状的三角形
上干货(与上面图片一一对应哦~)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
/*向上三角形*/
.top {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-top: none;
border-bottom-color:pink;
}
/*向下三角形*/
.bottom {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-bottom: none;
border-top-color:pink;
}
/*向左三角形*/
.left {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-left: none;
border-right-color:pink;
}
/*向右三角形*/
.right {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-right: none;
border-left-color:pink;
}
/*向上三角形*/
.top2 {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-top: none;
border-bottom:60px solid pink;
}
/*向下三角形*/
.bottom2 {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-bottom: none;
border-top-color:pink;
border-top:60px solid pink;
}
/*直角三角形*/
.zhijiao1 {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-bottom: none;
border-left: none;
border-right-color: pink;
}
.zhijiao2 {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-bottom: none;
border-right: none;
border-left-color: pink;
}
.zhijiao3 {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-left: none;
border-top: none;
border-right-color: pink;
}
.zhijiao4 {
width: 0px;
height: 0px;
border: 20px solid transparent;
border-right: none;
border-top: none;
border-left-color: pink;
}
</style>
</head>
<body>
<div class="top"></div>
<br><br>
<div class="bottom"></div>
<br><br>
<div class="left"></div>
<br><br>
<div class="right"></div>
<br><br>
<div class="top2"></div>
<br><br>
<div class="bottom2"></div>
<br><br>
<div class="zhijiao1"></div>
<br><br>
<div class="zhijiao2"></div>
<br><br>
<div class="zhijiao3"></div>
<br><br>
<div class="zhijiao4"></div>
</body>
</html>
三角形的大小可以通过显示的border高度调整