问题描述:移动端iPhone上的1px边框看起来像2px那么粗。
问题分析:不同的手机有不同的像素密度,在window对象中有一个devicePixelRatio属性,它可以反应设备的像素与css中的像素比。即devicePixelRatio=物理像素/独立像素。iPhone使用的是Retine屏,“Retina”是一种显示技术,可以把更多的像素点压缩至一块屏幕里,从而达到更高的分辨率并提高屏幕显示的细腻程度。因为Retine屏的分辨率始终是普通屏幕的2倍,1px的边框在devicePixelRatio=2的Retine屏下会显示成2px,所以css设置1px的样式,实际是2px的效果。
解决方案

1、用小数实现

div {
  border: 1px solid #dfdfdf;
}
@media screen and (-webkit-min-device-pixel-ratio: 2) {
  div {
    border: 0.5px solid #dfdfdf;
  }
}
@media screen and (-webkit-min-device-pixel-ratio: 3) {
  div {
    border: 0.333333px solid #dfdfdf;
  }
}

无法兼容安卓设备和iOS8以下的设备。

2、用border-image实现

.border-image-1px {
  border-bottom: 1px solid #dfdfdf;
}
@media screen and (-webkit-min-device-pixel-ratio: 2) {
  .border-image-1px {
    border-bottom: none;
    border-width: 0 0 1px 0;
    -webkit-border-image: url(../img/dot.png) 0 0 2 0 stretch;
    border-image: url(../img/dot.png) 0 0 2 0 stretch;
  }
}

修改颜色麻烦,需要替换图片,圆角需要特殊处理。

3、用background-image实现

@media screen and (-webkit-min-device-pixel-ratio: 2) {
  .background-image-1px {
    background: url(../img/line.png) repeat-x left bottom;
    -webkit-background-size: 100% 1px;
    background-size: 100% 1px;
  }
}

修改颜色麻烦,需要替换图片,圆角需要特殊处理。

4、用多背景渐变实现

@media screen and (-webkit-min-device-pixel-ratio: 2) {
  .background-gradient-1px {
    background:
      linear-gradient(#dfdfdf, #dfdfdf 100%, transparent 100%) left / 1px 100% no-repeat,
      linear-gradient(#dfdfdf, #dfdfdf 100%, transparent 100%) right / 1px 100% no-repeat,
      linear-gradient(#dfdfdf,#dfdfdf 100%, transparent 100%) top / 100% 1px no-repeat,
      linear-gradient(#dfdfdf,#dfdfdf 100%, transparent 100%) bottom / 100% 1px no-repeat
  }
}

/* 或者 */

@media screen and (-webkit-min-device-pixel-ratio: 2) {
  .background-gradient-1px{
    background:
      -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #dfdfdf), to(#dfdfdf)) left / 1px 100% no-repeat,
      -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #dfdfdf), to(#dfdfdf)) right / 1px 100% no-repeat,
      -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #dfdfdf), to(#dfdfdf)) top / 100% 1px no-repeat,
      -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #dfdfdf), to(#dfdfdf)) bottom / 100% 1px no-repeat
  }
}

圆角没法实现,多背景图片有兼容性问题。

5、用box-shadow实现

.box-shadow-1px {
  box-shadow: inset 0px -1px 1px -1px #dfdfdf;
}

边框有阴影,颜色变浅。
6、用viewport+rem实现

devicePixelRatio=2时,

<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no">

devicePixelRatio=3时,

<meta name="viewport" content="initial-scale=0.3333333333333333, maximum-scale=0.3333333333333333, minimum-scale=0.3333333333333333, user-scalable=no">

这种兼容方案相对比较完美,适合新的项目,老的项目修改成本过大。
手淘H5页面就采用了Flexible技术,核心是viewport+rem。

7、用伪元素+transform实现
原理是,把元素的border去掉,然后利用:before或者:after重做border,并让transform的 scale缩小一半,元素本身相对定位,新做的border绝对定位。

@media only screen and (-webkit-min-device-pixel-ratio: 2.0){
  .border-1px, .border-top-1px, .border-right-1px ,.border-bottom-1px , .border-left-1px{
    position: relative;
    border:none;
  }
  /*线条颜色*/
  .border-1px:after, .border-top-1px:after, .border-right-1px:after, .border-bottom-1px:after, .border-left-1px:after {
    background-color: #f00;
  }

  /*上边边框一像素*/
  .border-top-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    left:;
    top:;
    width: 100%;
    height: 1px;
    transform-origin: 0 0;
    transform: scaleY(0.5);
  }
  /*右边边框一像素*/
  .border-right-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    right:;
    bottom:;
    width: 1px;
    height: 100%;
    transform-origin: 0 0;
    transform: scaleX(0.5);
  }
  /*底边边框一像素*/
  .border-bottom-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    left:;
    bottom:;
    width: 100%;
    height: 1px;
    transform-origin: 0 0;
    transform: scaleY(0.5);
  }
  /*左边边框一像素*/
  .border-left-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    left:;
    top:;
    width: 1px;
    height: 100%;
    transform-origin: 0 0;
    transform: scaleX(0.5);
  }

  /*边框一像素*/
  .border-1px:after {
    pointer-events: none;
    content: "";
    box-sizing: border-box;
    position: absolute;
    left:;
    top:;
    width: 200%;
    height: 200%;
    border: 1px solid #dfdfdf;
    -webkit-transform: scale(0.5);
    transform: scale(0.5);
    -webkit-transform-origin: left top;
    transform-origin: left top;
  }
}
@media only screen and (-webkit-min-device-pixel-ratio: 3.0){
  .border-1px, .border-top-1px, .border-right-1px ,.border-bottom-1px , .border-left-1px{
    position: relative;
    border:none;
  }
  /*线条颜色*/
  .border-1px:after, .border-top-1px:after, .border-right-1px:after, .border-bottom-1px:after, .border-left-1px:after {
    background-color: #f00;
  }

  /*上边边框一像素*/
  .border-top-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    left:;
    top:;
    width: 100%;
    height: 1px;
    transform-origin: 0 0;
    transform: scaleY(0.333);
  }
  /*右边边框一像素*/
  .border-right-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    right:;
    bottom:;
    width: 1px;
    height: 100%;
    transform-origin: 0 0;
    transform: scaleX(0.333);
  }
  /*底边边框一像素*/
  .border-bottom-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    left:;
    bottom:;
    width: 100%;
    height: 1px;
    transform-origin: 0 0;
    transform: scaleY(0.333);
  }
  /*左边边框一像素*/
  .border-left-1px:after {
    pointer-events: none;
    content:"";
    position: absolute;
    left:;
    top:;
    width: 1px;
    height: 100%;
    transform-origin: 0 0;
    transform: scaleX(0.333);
  }

  /*边框一像素*/
  .border-1px:after {
    pointer-events: none;
    content: "";
    box-sizing: border-box;
    position: absolute;
    left:;
    top:;
    width: 300%;
    height: 300%;
    border: 1px solid #dfdfdf;
    -webkit-transform: scale(0.333);
    transform: scale(0.333);
    -webkit-transform-origin: left top;
    transform-origin: left top;
  }
}

移动端1px边框实现的更多相关文章

  1. 移动端1px边框

    问题:移动端1px边框,看起来总是2倍的边框大小,为了解决这个问题试用过很多方法,用图片,用js判断dpr等,都不太满意, 最后找到一个还算好用的方法:伪类 + transform 原理是把原先元素的 ...

  2. 移动端1px边框伪类宽高计算

    移动端1px边框在手机上看显得比较粗,于是我们用伪类结合css3缩放的方法去设置线条,但是如果设置div的一条边,水平线就设置宽度100%,垂直线就设置高度100%,那么如果是div的四条边呢?宽高1 ...

  3. 目前解决移动端1px边框最好的方法

    在移动端开发时,经常会遇到在视网膜屏幕中元素边框变粗的问题.本文将带你探讨边框变粗问题的产生原因及介绍目前市面上最好的解决方法. 1px 边框问题的由来 苹果 iPhone4 首次提出了 Retina ...

  4. 移动端1px边框问题

    用于手机端受dpr的影响,实际开发中,PC端和移动端展示的效果不太一样,往往在PC端显示的是1px,移动端常常是偏粗一些. 解决办法: 主要是用到伪类及缩放.在需要画边框的元素上,设置一个伪类,它的伪 ...

  5. 解决CSS移动端1px边框问题

    移动项目开发中,安卓或者IOS等高分辨率屏幕会把1px的border渲染成2px来显示,网上搜了一下,解决方法如下: 一.利用css中的transform的缩放属性解决,推荐这个.如下面代码. < ...

  6. 移动端1px边框解决方案

    在retina屏中,像素比为2(iPhone6/7/8)或3(iPhone6Plus/7Plus/8Plus),1px的边框看起来比真的1px更宽. 使用伪类加transform的方式 元素本身不定义 ...

  7. 解决移动端1px边框问题的几种方法

    1.边框粗细原因 在移动端下设置border为1px,在某些设备上看比1px粗. 这些由于不同的手机有不同的像素密度.在window对象中有一个devicePixelRatio属性,他可以反应css中 ...

  8. 移动端1px 边框完整方案(四个方向)

    使用stylus(预处理) 需要一个函数接收两个参数 第一个需要在哪个方向出现边框 第二个边框颜色 $border1px(face,$color) 根据传入的方向属性,调整其他参数 l  左右方向 t ...

  9. 关于移动端1px边框问题

    <div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...

随机推荐

  1. vue实战记录(三)- vue实现购物车功能之渲染商品列表

    vue实战,一步步实现vue购物车功能的过程记录,课程与素材来自慕课网,自己搭建了express本地服务器来请求数据 作者:狐狸家的鱼 本文链接:vue实战-实现购物车功能(三) GitHub:sue ...

  2. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)

    用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...

  3. <六>企业级开源仓库nexus3实战应用–使用nexus3配置yum私有仓库

    一两个星期之前,你如果在我跟前说起私服的事情,我大概会绕着你走,因为我对这个东西真的一窍不通.事实上也正如此,开发同学曾不止一次的跟我说公司的私服版本太旧了,许多新的依赖编译之后不会从远程仓库自动缓存 ...

  4. 针对监控摄像机(海康、大华等)录像 .h264 文件的流媒体播放设计

    监控摄像机(海康.大华等)内部带的录像功能一般录制的是h264文件,这种文件格式简单的把每一帧h264字节数据保存到文件里. 实际使用中,可能需要对特定录像进行反复检测,以训练.改进视频检测算法的准确 ...

  5. 应用调试(一)strace

    目录 编译 使用 原理 深入文档 title: 应用调试(一)strace date: 2019/1/15 23:35:14 toc: true --- 编译 #tar -xjf strace-4.5 ...

  6. Form -- 文件上传

    当我们选中文件,点击上传时即可. 而此按钮一般是一张图片覆盖了一个input标签而以.基于这个原理我们可以定制自己喜欢的样式 <div style="text-align: cente ...

  7. 乘积型Sobolev不等式

    (Multiplicative Sobolev inequality). Let $\mu,\lambda$ and $\gamma$ be three parameters that satisfy ...

  8. Dijkstra算法的C++实现

    Dijkstra算法是在图中寻找两顶点最短路径的算法.   下面以下图有向图为例,说明其基本思想. 上图为转化为邻接矩阵存储:     现在我要寻找1点到其他点的最短距离以及路径: a)1点到各点的距 ...

  9. JavaScript的数据类型和变量

    1.  数据类型:JS使用弱类型,共4种基本类型,其数据可以是变量,也可以是常量. a)         数值(整数和实数) b)         字符串型(用“”号或‘’括起来的字符或数值) c)  ...

  10. vue通过extend动态创建全局组件(插件)学习小记

    测试环境:nodejs+webpack,例子是看文章的,注释为自己的理解 创建一个toast.vue文件: <template> <div class="wrap" ...