hairline细边框方案

判断是否支持

//1px兼容处理,判断当前系统能否渲染出0.5px
;(function (window) {
    var docEl = window.document;
    var bodyEl = docEl.getElementsByTagName('body')[0];

    if (window.devicePixelRatio && window.devicePixelRatio >= 2) {
        var testEl = docEl.createElement('div');
        testEl.style.border = '.5px solid transparent';
        bodyEl.appendChild(testEl);
        if (testEl.offsetHeight === 1) {
            bodyEl.classList.add('hairline');
        }
        bodyEl.removeChild(testEl);
    }
})(window);

方案一、简单粗暴的方式

给全部元素预设置边框宽度

防止ios有的机型0.5px不显示,或者圆角时候有缺口,设置为0.75

      .hairline  *, .hairline  *:before, .hairline *:after {
          border-width: 0.75PX !important;
          border-bottom-width: 0.75PX !important;
          border-top-width: 0.75PX !important;
          border-left-width: 0.75PX !important;
          border-right-width: 0.75PX !important;
      }

方案二、添加全局css

    .hairline {
        .lineTop {
            border-top-width: 0.75px !important;
        }
        .lineLeft {
            border-left-width: 0.75px !important;
        }
        .lineRight {
            border-left-width: 0.75px !important;
        }
        .lineBottom {
            border-left-width: 0.75px !important;
        }
    }

html中使用

给用到边框的元素加上class名称

<div class="lineTop lineBottom" style="border-top: 1px solid red;border-bottom: 1px solid red;"></div>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 方法 在一个对象中绑定函数,称为这个对象的方法。 在JavaScript中,对象的定义是这样的: 但是,如果我们给...
    wit92阅读 3,226评论 0 0
  • 前端常见的一些问题 1.前端性能优化手段? 1. 尽可能使用雪碧图2. 使用字体图标代替图片3. 对HTML,cs...
    十八人言阅读 4,879评论 0 1
  • 001.优雅降级和渐进增强 渐进增强(Progressive Enhancement):一开始就针对低版本浏览器进...
    造了个轮子阅读 3,017评论 1 1
  • 一、xue的生命周期是什么 vue每个组件都是独立的,,每个组件都有一个属于他的生命周期,从一个组件创建、数据初始...
    康娜阅读 4,601评论 0 0
  • 0.链接 状态码精选 https://www.cnblogs.com/yaya-003/p/12653602.ht...
    蛋挞xjc阅读 3,379评论 0 0