使用border属性绘制三角形、五角星、心形

8 篇文章 0 订阅

1. 绘制三角形

<p>直角三角形</p>
<section class="triangle1">
	<style>
		#triangle1 {
			width: 0;
			height: 0;
			border-right: 50px solid transparent; 
			border-bottom: 50px solid red;
		}
	</style>
	<div id="triangle1"></div>
</section>

<p>等腰三角形</p>
<section class="triangle2">
	<style>
		#triangle2 {
			width: 0;
			height: 0;
			border-bottom: 50px solid red;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
		}
	</style>
	<div id="triangle2"></div>
</section>

<p>两个三角形叠加</p>
<section class="triangle3">
	<style>
		.triangle3 {
			position: relative;
		}
		#triangle3 {
			width: 0;
			height: 0;
			border-bottom: 50px solid red;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
		}
		#triangle3:after {
			content: '';
			display: block;
			width: 0;
			height: 0;
			position: absolute;
			top: 10px;
			left: 0;
			border-bottom: 50px solid #fff;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
		}
	</style>
	<div id="triangle3"></div>
</section>

效果如下:

2. 绘制梯形、菱形、平行四边形

<p>等腰梯形</p>
<section class="trapezoid">
	<style>
		#trapezoid {
			width: 40px;
			height: 0;
			border-bottom: 50px solid red;
			border-left: 30px solid transparent;
			border-right: 30px solid transparent;
		}
	</style>
	<div id="trapezoid"></div>
</section>

<p>直角梯形</p>
<section class="trapezoid">
	<style>
		#trapezoid2 {
			width: 40px;
			height: 0;
			border-bottom: 50px solid red;
			border-right: 30px solid transparent;
		}
	</style>
	<div id="trapezoid2"></div>
</section>

<p>菱形</p>
<section class="lozenge">
	<style>
		.lozenge {
			position: relative;
		}
		#lozenge {
			width: 50px;
			height: 50px;
			background: red;
			transform: rotate(45deg);
			position: absolute;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
		}
	</style>
	<div id="lozenge"></div>
</section>

<p>平行四边形</p>
<section class="parallelogram">
	<style>
		#parallelogram {
			width: 80px;
			height: 50px;
			position: relative;
			background: red;
			transform: skew(-35deg);
		}
		/*#parallelogram:after {
			content: '';
			position: absolute;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			z-index: -1;
			transform: skew(35deg);
			background: red;
		}*/
	</style>
	<div id="parallelogram"></div>
</section>

效果:

 

3. 绘制五角星

<p>五角星</p>
<section class="wujiaoxing">
	<style>
		.wujiaoxing {
			position: relative;
			width: 0;
			height: 0;
			border-top: 36px solid red;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
		}
		#wujiaoxing:before {
			content: '';
			position: absolute;
			top: -37px;
			left: -49px;
			border-top: 36px solid red;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
			transform: rotate(72deg);
			transform-origin: 50% 50%;
		}
		#wujiaoxing:after {
			content: '';
			position: absolute;
			top: -39px;
			left: -49px;
			border-top: 36px solid red;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
			transform: rotate(144deg);
			transform-origin: 50% 50%;
		}
	</style>
	<div id="wujiaoxing"></div>
</section>

<p>六角形</p>
<section class="liujiaoxing">
	<style>
		.liujiaoxing {
			position: relative;
		}
		#liujiaoxing {
			width: 0;
			height: 0;
			border-bottom: 80px solid red;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
		}
		#liujiaoxing:before {
			content: '';
			position: absolute;
			top: 25px;
			left: 0;
			border-top: 80px solid red;
			border-left: 50px solid transparent;
			border-right: 50px solid transparent;
		}
	</style>
	<div id="liujiaoxing"></div>
</section>

效果:

 

4.绘制心形

<p>心形</p>
<section class="heart">
	<style>
		.heart {
			position: relative;
			margin: 30px;
			width: 500px;
			height: 500px;
		}
		#heart {
			width: 100px;
			height: 100px;
			background: red;
			position: relative;
			transform: rotate(45deg);
		}
		#heart:before {
			content: '';
			width: 50px;
			height: 100px;
			position: absolute;
			left: -49px;
			top: 0;
			background: red;
			border-radius: 50px 0 0 50px;
		}
		#heart:after {
			content: '';
			width: 100px;
			height: 50px;
			position: absolute;
			left: 0;
			top: -49px;
			background: red;
			border-radius: 50px 50px 0 0;
		}
	</style>
	<div id="heart"></div>
</section>

 

 

代码地址:利用border绘制图形

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值