CSS创建三角形的几种方法

结论:
  • svg方法(polygon)
  • canvas方法(moveTo 、lineTo、fill)
  • border方法
  • html字符
  • 正方形旋转方法

代码如下

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			/*svg方法*/
			.svg-triangle {
				width: 100px;
				height: 100px;
				margin: 0 10px;
			}
			
			.svg-triangle polygon {
				fill:#65b81d;
				/*stroke: #65b81d;
				stroke-width: 4;*/
			}
			
			/*旋转方法*/
			#square_box{
				display:inline-block;
				width:100px;
				height: 100px;
				overflow: hidden;
			}
			#square_box #square{
				width: 100%;
				height: 100%;
				transform: rotate(45deg);
				background: red;
				margin-left: -70%;
			}
			
			/*css边框*/
			#borders{
				width: 0;
				height: 0;
				border-width: 70px 50px 30px 50px;
				border-color:red transparent transparent transparent; 
				border-style: solid;
				box-sizing: border-box;
			}
		</style>
	</head>

	<body>
		<h4>svg方法</h4>
		<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="svg-triangle">
			<polygon points="0,0 100,0 50,100" />
		</svg>
		
		<h4>canvas方法</h4>
		<canvas id="triangle" height="100" width="100"></canvas>
		
		<h4>CSS 边框</h4>
		<div id="borders"></div>
		
		<h4>CSS 旋转正方形</h4>
		<div id="square_box"><div id="square"></div></div>
		
		<h4>HTML 字符</h4>
		<div style="font-size: 50px;">⇠</div>
		
	<script>
		function canvasAngle(){
			var triangle = document.getElementById("triangle");
			var canvas = triangle.getContext('2d');
			
			canvas.beginPath();
			canvas.moveTo(50,0);
			canvas.lineTo(100,100);
			canvas.lineTo(0,100);
			canvas.closePath();
			
			canvas.fillStyle = 'red';
			canvas.fill();
			
		}
		
		canvasAngle()
	</script>	
	</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值