用CSS实现三角形及其原理

盒子模型

首先要充分了解盒子模型的构造
图片来自百度

原理

各边边框宽度相同

有内容的盒子

在这里CSS样式中的width和height是内容的宽度和高度

<style>
	div {
        width: 40px;
        height: 40px;
        border-top: 20px solid #000;
        border-right: 20px solid red;
        border-left: 20px solid blue ;
        border-bottom: 20px solid greenyellow;
    }
</style>
<body>
  <div></div>
</body>
  • 图示如下:
    在这里插入图片描述
去掉某一边的边框

这里以去掉下部分边框为例,其它类似

<style>
    div {
        width: 40px;
        height: 40px;
        border-top: 20px solid #000;
        border-right: 20px solid red;
        border-left: 20px solid blue ;
    }
</style>
<body>
  <div></div>
</body>
  • 图示如下:

在这里插入图片描述

去掉内容

去掉内容就是把width和height都置为0,可以看出上下左右边框各为一个三角形

<style>
    div {
        width: 0px;
        height: 0px;
        border-top: 20px solid #000;
        border-right: 20px solid red;
        border-left: 20px solid blue ;
        border-bottom: 20px solid greenyellow;
    }
</style>
<body>
  <div></div>
</body>
  • 图示如下:
    在这里插入图片描述
<style>
    div {
        width: 0px;
        height: 0px;
        border-top: 20px solid #000;
        border-right: 20px solid red;
        border-left: 20px solid blue ;
    }
</style>
<body>
  <div></div>
</body>
  • 图示如下:
    在这里插入图片描述

各边边框不尽相同

有内容的盒子

各个边框宽度不同

<style>
    div {
        width: 40px;
        height: 40px;
        border-top: 20px solid #000;
        border-right: 30px solid red;
        border-left: 40px solid blue ;
        border-bottom: 50px solid greenyellow;
    }
</style> 
<body>
  <div></div>
</body>
  • 图示如下:
    在这里插入图片描述
去掉某一边的边框

这里以去掉下部分边框为例,其它类似

<style>
    div {
        width: 40px;
        height: 40px;
        border-top: 20px solid #000;
        border-right: 30px solid red;
        border-left: 40px solid blue ;
    }
</style> 
<body>
  <div></div>
</body>
  • 图示如下
    在这里插入图片描述
去掉内容

去掉内容就是把width和height都置为0,可以看出上下左右边框各为一个三角形

<style>
    div {
        width: 0px;
        height: 0px;
        border-top: 20px solid #000;
        border-right: 30px solid red;
        border-left: 40px solid blue ;
        border-bottom: 50px solid greenyellow;
    }
</style> 
<body>
  <div></div>
</body>
  • 图示如下
    在这里插入图片描述
<style>
    div {
        width: 0px;
        height: 0px;
        border-top: 20px solid #000;
        border-right: 30px solid red;
        border-left: 40px solid blue ;
    }
</style> 
<body>
  <div></div>
</body>
  • 图示如下
    在这里插入图片描述

三角形

将要展示的三角形设置为某一颜色,其它各个边框的颜色设置为透明(transparent

<style>
    div {
        width: 0px;
        height: 0px;
        border-top: 20px solid #000;
        border-right: 20px solid red;
        border-left: 20px solid blue ;
        border-bottom: 20px solid greenyellow;
    }
    #div1 {
        width: 0px;
        height: 0px;
        border-top: 20px solid #000;
        border-right: 20px solid transparent;
        border-left: 20px solid transparent;
        border-bottom: 20px solid transparent;
    }
    #div2 {
        width: 0px;
        height: 0px;
        border-top: 20px solid transparent;
        border-right: 20px solid red;
        border-left: 20px solid transparent;
        border-bottom: 20px solid transparent;
    }
    #div3 {
        width: 0px;
        height: 0px;
        border-top: 20px solid transparent;
        border-right: 20px solid transparent;
        border-left: 20px solid blue;
        border-bottom: 20px solid transparent;
    }
    #div4 {
        width: 0px;
        height: 0px;
        border-top: 20px solid transparent;
        border-right: 20px solid transparent;
        border-left: 20px solid transparent;
        border-bottom: 20px solid greenyellow;
    }
</style>
<body>
  <div id="div1"></div>
  <div id="div2"></div>
  <div id="div3"></div>
  <div id="div4"></div>
  <div></div>

</body>
  • 图示如下
    在这里插入图片描述
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值