如何实现0.5px边框
<div class='box box1'></div>
方法一: 伪元素+scale
优点: 兼容性好
实现方式: 将容器设置伪元素,设置绝对定位,宽高设置200%,边框设置为1px,然后用transform:scale(0.5),使伪元素缩小到原来的一半,此时伪元素的边框会和容器的边缘重合。
.box { width: 360px; height: 50px; border-radius: 5px; margin-top: 20px; line-height: 50px; } .box1 { position: relative; } .box1::after { position: absolute; bottom: 0; z-index: -1; width: 200%; height: 200%; content: ""; display: block; border: 1px solid red; border-radius: 5px; transform: scale(0.5); transform-origin: left bottom; }
方法二:背景渐变
优点:简单,适合一根线的边框
实现方式:给容器设置伪元素,设置绝对定位,高度1px,背景图设置线性渐变,一半设置颜色,一半设置透明,就可以实现0.5px边框了。但是不能展示圆角
.box { width: 360px; height: 50px; border-radius: 5px; margin-top: 20px; line-height: 50px; } .box1 { position: relative; } .box1::after { content: ""; position: absolute; left: 0; bottom: 0; width: 100%; height: 1px; background-image: linear-gradient(0deg, red 50%, transparent 50%); }
方法三: 利用阴影代替边框
实现方式:利用盒子阴影设置
可以实现更细的边框
.box { width: 360px; height: 50px; border-radius: 5px; margin-top: 20px; line-height: 50px; } .box1 { box-shadow: 0 0 0 0.5px red; }
浙公网安备 33010602011771号