如何实现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;
      }
复制代码

 

posted @   又又儿  阅读(1052)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示