移动端1px边框实现
问题描述:移动端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边框实现的更多相关文章
- 移动端1px边框
		
问题:移动端1px边框,看起来总是2倍的边框大小,为了解决这个问题试用过很多方法,用图片,用js判断dpr等,都不太满意, 最后找到一个还算好用的方法:伪类 + transform 原理是把原先元素的 ...
 - 移动端1px边框伪类宽高计算
		
移动端1px边框在手机上看显得比较粗,于是我们用伪类结合css3缩放的方法去设置线条,但是如果设置div的一条边,水平线就设置宽度100%,垂直线就设置高度100%,那么如果是div的四条边呢?宽高1 ...
 - 目前解决移动端1px边框最好的方法
		
在移动端开发时,经常会遇到在视网膜屏幕中元素边框变粗的问题.本文将带你探讨边框变粗问题的产生原因及介绍目前市面上最好的解决方法. 1px 边框问题的由来 苹果 iPhone4 首次提出了 Retina ...
 - 移动端1px边框问题
		
用于手机端受dpr的影响,实际开发中,PC端和移动端展示的效果不太一样,往往在PC端显示的是1px,移动端常常是偏粗一些. 解决办法: 主要是用到伪类及缩放.在需要画边框的元素上,设置一个伪类,它的伪 ...
 - 解决CSS移动端1px边框问题
		
移动项目开发中,安卓或者IOS等高分辨率屏幕会把1px的border渲染成2px来显示,网上搜了一下,解决方法如下: 一.利用css中的transform的缩放属性解决,推荐这个.如下面代码. < ...
 - 移动端1px边框解决方案
		
在retina屏中,像素比为2(iPhone6/7/8)或3(iPhone6Plus/7Plus/8Plus),1px的边框看起来比真的1px更宽. 使用伪类加transform的方式 元素本身不定义 ...
 - 解决移动端1px边框问题的几种方法
		
1.边框粗细原因 在移动端下设置border为1px,在某些设备上看比1px粗. 这些由于不同的手机有不同的像素密度.在window对象中有一个devicePixelRatio属性,他可以反应css中 ...
 - 移动端1px 边框完整方案(四个方向)
		
使用stylus(预处理) 需要一个函数接收两个参数 第一个需要在哪个方向出现边框 第二个边框颜色 $border1px(face,$color) 根据传入的方向属性,调整其他参数 l 左右方向 t ...
 - 关于移动端1px边框问题
		
<div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...
 
随机推荐
- jmeter5.1在windows(含插件安装)及linux环境下安装
			
jmeter下载 前提:已经安装jdk8+ jmeter下载地址:http://jmeter.apache.org/download_jmeter.cgi 有Binaries和Source版本 前者是 ...
 - 初识 go 语言
			
目录 go简介 安装 hello world 函数 变量 常量 可见性规则 结束 前言: 最近组内要试水区块链,初步方案定为使用fabirc来弄,而fabric的智能合约就是用go写的,借此机会正好学 ...
 - PHP 加解密方法大全
			
最近看见一篇文章讲的是PHP的加解密方法,正好也自己学习下,顺便以后有用到的地方也好能快速用上,仅供自己学习和复习,好了不多BB,上代码. 基于这几个函数可逆转的加密为:base64_encode() ...
 - visual studio 不能跳转到函数定义
			
解决办法: 工具-->扩展和更新-->联机.搜索“Go To Definition”下载然后关闭visualstudio进行安装,重启后就ok了
 - 分布式监控系统开发【day38】:监控数据如何画图(九)
			
一.画图代码 1.收集处理数据 class GraphGenerator(object): ''' generate graphs ''' def __init__(self,request,redi ...
 - GO 基础
			
基本语法练习 打印 乘法表 package main import ("fmt") func main(){ for n:=1;n<=9;n++{ for m:=1;m< ...
 - SpringBoot系列: 所有配置属性和官方文档
			
Spring Boot 通用配置参数https://docs.spring.io/spring-boot/docs/current/reference/html/common-application- ...
 - [Luogu P1516]青蛙的约会
			
按照题意,显然可以列出同余方程,k即为所求天数,再将其化为不定方程 ,那么对这个方程用扩展欧几里德算法即可得出k,q的一组解,但是方程有解的充要条件是(m – n) 和L不同时为零并且x – y是m ...
 - Geometric regularity criterion for NSE: the cross product of velocity and vorticity 4: $u\cdot \om$
			
在 [Berselli, Luigi C.; Córdoba, Diego. On the regularity of the solutions to the 3D Navier-Stokes eq ...
 - Xss Bypass备忘录
			
Xss Bypass备忘录 技术要发展,免不了风波. 也许这些攻攻防防会更好的促进技术的发展也说不定 就让这一次次的爆破换来将来更精练的技术的无比的宁静吧 我们静观其变吧! 缅怀当初那份最纯真Hack ...