巧用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高度调整

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值