纯CSS画三角形

如何用CSS画三角形

需要哪边就将对应边去掉,其余两边颜色改为透明

<!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>Document</title>
</head>
<body>
    
    <div class="out">
        <div class="in"></div>
    </div>
    
    <style>
        .out {
            width: 50px;
            height: 50px;
        }

        .in {
            /* 内块的边框大小要为外块宽高的一半 */
            border-top: 25px solid red;
            border-bottom: 25px solid blue;
            border-right: 25px solid yellow;
            border-left: 25px solid green;
        }
    </style>

</body>
</html>

例:画左边三角形

<!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>Document</title>
</head>
<body>
    
    <div class="out">
        <div class="in"></div>
    </div>
    
    <style>
        .out {
            width: 50px;
            height: 50px;
        }

        .in {
            border-top: 25px solid transparent;
            border-bottom: 25px solid transparent;
            /* border-right: 25px solid yellow; */
            border-left: 25px solid green;
        }
    </style>

</body>
</html>
posted @ 2022-03-18 23:02  伏月廿柒  阅读(27)  评论(0)    收藏  举报