问题描述:移动端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. Mysql注入小tips --持续更新中

    学习Web安全好几年了,接触最多的是Sql注入,一直最不熟悉的也是Sql注入.OWASP中,Sql注入危害绝对是Top1.花了一点时间研究了下Mysql类型的注入. 文章中的tips将会持续更新,先说 ...

  2. sqlalchemy和pymysql通过ssh连接远程mysql服务器

    首先需要一个模块sshtunnel,如果没有直接pip install sshtunnel 其实连个连接方式非常像: pymysql连接方式: import pymysql from sshtunne ...

  3. SQL Server数据库中表的增、删、改

    通过SqlCommand对象的ExecuteNonQuery方法执行命令行,来实现数据库中表的增.删.改.主要有5步 using System.Data.SqlClient;//载入数据库命名空间 p ...

  4. MAC OS进阶必看——这10个技巧让你秒变MAC达人

    文章内容及图片来源于:什么值得买,如果涉及版权问题,请联系作者删除 文章收录于:风云社区(提供上千款各类mac软件的下载) 使用mac系统也有好几个年头,出色的办公效率以及越来越广的兼容性让mac成为 ...

  5. python之shelve模块详解

    一.定义 Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的. 二.用途 可以作为一个简单的数据存储方案. 三.用法 使用时,只需要使用open函数获取一个 ...

  6. Go语言系列(七)- 读写操作

    终端读写 1. 终端读写 操作终端相关文件句柄常量 os.Stdin:标准输入 os.Stdout:标准输出 os.Stderr:标准错误输出 2. 终端读写示例 package main impor ...

  7. DirectX11 With Windows SDK--20 硬件实例化与视锥体裁剪

    前言 这一章将了解如何在DirectX 11利用硬件实例化技术高效地绘制重复的物体,以及使用视锥体裁剪技术提前将位于视锥体外的物体进行排除. 在此之前需要额外了解的章节如下: 章节回顾 18 使用Di ...

  8. RMQ(ST表)

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; int N, M, ...

  9. [再寄小读者之数学篇](2014-09-22 distributions and square integrable functions)

    Suppose that $f\in L^2$, $g\in \scrD'$, if $$\bex f=g,\mbox{ in }\scrD', \eex$$ then $f=g\in L^2$. I ...

  10. Celery - 一个懂得 异步任务 , 定时任务 , 周期任务 的芹菜

    1.什么是Celery?Celery 是芹菜Celery 是基于Python实现的模块, 用于执行异步定时周期任务的其结构的组成是由    1.用户任务 app    2.管道 broker 用于存储 ...