用css写三角形的形状

在实现三角形之前,先对border了解

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body,
        div {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 0;
            border: #000 20px solid;
            border-left-color: red;
            border-right-color: blue;
            border-top-color: green;
            border-bottom-color: orange;
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

效果如下
在这里插入图片描述
总结:将width设置为0,就有border尺寸,颜色来实现来控制三角形的大小,若不同方位颜色设置transparent为透明,来组合三角形

向下三角形

.box {
            width: 0;
            border: transparent 20px solid;
            border-top-color: green;
            border-bottom: 0;
        }

向上三角形

.box {
            width: 0;
            border: transparent 20px solid;
            border-bottom-color: orange;
            border-top: 0;
        }

向左三角形

 .box {
            width: 0;
            border: transparent 20px solid;
            border-right-color: orange;
            border-left: 0;
        }

向右三角形

.box {
            width: 0;
            border: transparent 20px solid;
            border-left-color: orange;
            border-right: 0;
        }

猜你喜欢

转载自blog.csdn.net/qq_31093255/article/details/107519246