CSS绘制一个三角形

49 篇文章 23 订阅
11 篇文章 0 订阅

目录

等腰梯形

普通三角形(等腰三角形)

等边三角形

平行四边形


我们先来看这样一个图形,为什么呢?因为绘制梯形、三角形或者平行四边形就是在这个基础上绘制的!

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>创建三角形</title>
		<style>
			*{
				margin: 0;
				padding: 0;
			}
			.box{
				width: 100px;
				height: 100px;
				
				border: 100px solid red;
			}
		</style>
	</head>
	<body>
		<div class="box"></div>
	</body>
</html>

等腰梯形

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>创建三角形</title>
		<style>
			*{
				margin: 0;
				padding: 0;
			}
			.box{
				width: 100px;
				height: 100px;
				
				border: 100px solid;
				
				border-top-color: transparent;
				border-right-color: transparent;
				border-bottom-color: transparent;
				border-left-color: red;
			}
		</style>
	</head>
	<body>
		<div class="box"></div>
	</body>
</html>

普通三角形(等腰三角形)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>实现三角形</title>
		<style>
			*{
				margin: 0;
				padding: 0;
			}
			.box{
				width: 0px;
				height: 0px;
				
				border: 100px solid;
				
				border-top-color: red;
				border-right-color: transparent;
				border-bottom-color: transparent;
				border-left-color: transparent;
			}
		</style>
	</head>
	<body>
		<div class="box"></div>
	</body>
</html>

等边三角形

我们知道等边三角形,高是边长的一半的sqrt(3)倍,所以如果边长的一半为50的话,高度差不多就是86.6,又因为边长的一半就是左边框的厚度,高度就是上边框的厚度,所以等边三角形的代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>创建三角形</title>
		<style>
			*{
				margin: 0;
				padding: 0;
			}
			.box{
				width: 0px;
				height: 0px;
				
				border-top: 86.6px solid;
				border-left: 50px solid;
				border-right: 50px solid;
				
				border-top-color: red;
				border-right-color: transparent;
				border-bottom-color: transparent;
				border-left-color: transparent;
			}
		</style>
	</head>
	<body>
		<div class="box"></div>
	</body>
</html>

平行四边形

解题思路:由创建三角形我们得知这是上边框和右边框,一个平行四边形是不是只要再拿它的中心对称图形就可以了?所以平行四边形的代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>创建平行四边形</title>
		<style>
			*{
				margin: 0;
				padding: 0;
			}
			.box1{
				width: 0px;
				height: 0px;
				
				float: left;
				
				border: 100px solid;
				
				border-top-color: red;
				border-right-color: red;
				border-bottom-color: transparent;
				border-left-color: transparent;
			}
			.box2{
				width: 0px;
				height: 0px;
				
				float: left;
				
				border: 100px solid;
				
				border-top-color: transparent;
				border-right-color: transparent;
				border-bottom-color: red;
				border-left-color: red;
			}
		</style>
	</head>
	<body>
		<div class="box1"></div>
		<div class="box2"></div>
	</body>
</html>

绘制什么直角梯形之类的,是不是一个道理? 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端吕小布

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值