css3写一个三角形

时间:2020-05-01

先定义一个简单的div盒子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        div {
            background-color: antiquewhite;
            width: 100px;
            height: 100px;
            border: 30px solid red;
        }
    </style>
</head>
<body>
<div></div>
</body>
</html>

然后设置宽高为0

<style type="text/css">
        div {
            background-color: antiquewhite;
            width: 0;
            height: 0;
            border: 30px solid red;
        }
</style>

重新定义每个边框的颜色

<style type="text/css">
        div {
            background-color: antiquewhite;
            width: 0;
            height: 0;
            border-width: 30px;
            border-color: red green yellow blue;
            border-style: solid;
        }
</style>

现在隐藏其它边框

<style type="text/css">
        div {
            background-color: antiquewhite;
            width: 0;
            height: 0;
            border-width: 30px;
            border-color: red transparent transparent transparent;
            border-style: solid;
        }
</style>