如何实现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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏