如何在一个正方形盒子中添加一条对角线?

新手上路,请多包涵

如何在一个正方形中添加一条对角线?

阅读 3.2k
2 个回答

可以直接用 CSS 线性渐变生成

.box{
    background: linear-gradient(to right bottom, transparent calc(50% - .5px), currentColor calc(50% + .5px), transparent 0);
}

image.png

https://codepen.io/xboxyan/pen/GRXEayb

横条绝对定位到正方形盒子的左上角,然后使用 transform:rotate(45deg) 就行。
哦,对了,记得切换 transform-originleft

<style>
  .box { width: 200px; height: 200px; border: 1px solid black; position: relative; overflow: hidden }
  .line{ width: 400px; height: 1px; background: red; position: absolute; transform: rotate(45deg); transform-origin: left;}
</style>
<body>
  <div class="box">
    <div class="line"></div>
  </div>
</body>

image.png

推荐问题