使用css transform属性画正三角形

404 阅读1分钟

效果是这样的:

image.png

<!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 transform属性画正三角形</title>
    <style>
      .box1,
      .box1::after,
      .box1::before {
        width: 50px;
        height: 50px;
        position: relative;
        background-color: red;
        overflow: hidden;
        float: left;
      }
      .box1::after,
      .box1::before {
        content: "";
        position: absolute;
        background-color: #fff;
        transform-origin: bottom right;
        transform: rotate(60deg) scaleX(1.2);
      }
      .box1::before {
        transform-origin: left bottom;
        transform: rotate(-60deg) scaleX(1.2);
      }
      .box2 {
        width: 100px;
        height: 20px;
        display: inline-block;
        background-color: lightslategray;
      }
    </style>
  </head>
  <body>
    <div class="box1"></div>
    <div class="box2"></div>
  </body>
</html>