用css画一个三角形——利用边框

原理:可以自己动手体验一下,记得更加深刻。将一个盒子设置成宽高为200px的正方形,把边框设置为100px。四个边框设置为不同颜色,当宽度调为0时,盒子会怎样变化,高度也调为0时,盒子会怎样变化。宽高都为0时,边框会并到一起,就形成了四个三角形,那我们直接把边框设置为透明,想要向上的三角形,就把上边框颜色设置为不透明的颜色,就能得到四个方向的三角形


 原理参考:博主钟阿能

链接:http://t.csdn.cn/JJNfK

 代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>css三角形</title>
    <style>
        .up {
            height: 0;
            width: 0;
            border: 200px solid transparent;
            border-bottom-color: palegoldenrod;
            /* border-width: 0 200px 400px; */
        }
        
        .down {
            height: 0;
            width: 0;
            border: 200px solid transparent;
            border-top-color: aquamarine;
            margin-top: 100px;
        }
        
        .left {
            height: 0;
            width: 0;
            border: 200px solid transparent;
            border-left-color: aquamarine;
            margin-top: -100px;
        }
        
        .right {
            height: 0;
            width: 0;
            border: 200px solid transparent;
            border-right-color: aquamarine;
        }
        
        .box {
            width: 100px;
            height: 100px;
            border: 50px solid red;
            border-bottom-color: greenyellow;
            border-left-color: palevioletred;
            border-right-color: aqua;
        }
    </style>
</head>

<body>
    <div class="up"></div>
    <div class="down"></div>
    <div class="left"></div>
    <div class="right"></div>
    <div class="box"></div>
</body>

</html>

 还可以调整边框宽度,得到等腰三角形

   .up {
            height: 0;
            width: 0;
            border: 200px solid transparent;
            border-bottom-color: palegoldenrod;
            border-width: 0 200px 400px;
        }

直角三角形:

  .right {
            height: 0;
            width: 0;
            border: 100px solid transparent;
            border-left-color: purple;
            border-bottom-color: purple;
        }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值