6种解决移动端1px的方案

简介: 6种解决移动端1px的方案

在CSS中我们一般使用px作为单位,需要注意的是,CSS样式里面的px和物理像素并不是相等的。CSS中的像素只是一个抽象的单位,在不同的设备或不同的环境中,CSS中的1px所代表的物理像素是不同的。在PC端,CSS的1px一般对应着电脑屏幕的1个物理像素,但在移动端,CSS的1px等于几个物理像素。

一、伪元素+transform(常用)

构建1个伪元素, border为1px, 再以transform缩放到50%。

对于老项目,有没有什么办法能兼容1px的尴尬问题了,个人认为伪类+transform是比较完美的方法了。 原理是把原先元素的 border

去掉,然后利用 :before 或者 :after 重做 border ,并将 transform 的 scale

缩小一半,原先的元素相对定位,新做的 border 绝对定位。

单条border样式设置:

.scale-1px{
  position: relative;
  border:none;
}
.scale-1px:after{
  content: '';
  position: absolute;
  bottom: 0;
  background: #000;
  width: 100%;
  height: 1px;
  -webkit-transform: scaleY(0.5);
  transform: scaleY(0.5);
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
}

四条boder样式设置:

.scale-1px{
  position: relative;
  margin-bottom: 20px;
  border:none;
}
.scale-1px:after{
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  border: 1px solid #000;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 200%;
  height: 200%;
  -webkit-transform: scale(0.5);
  transform: scale(0.5);
  -webkit-transform-origin: left top;
  transform-origin: left top;
}

最好在使用前也判断一下,结合 JS 代码,判断是否 Retina 屏:

if(window.devicePixelRatio && devicePixelRatio >= 2){
  document.querySelector('ul').className = 'scale-1px';
}
  • 优点:可以满足所有场景,且修改灵活。
  • 缺点:对于已使用伪类的元素(例如clearfix)要多层嵌套。

二、viewport + rem 实现(常用)

同时通过设置对应viewport的rem基准值,这种方式就可以像以前一样轻松愉快的写1px了。

在devicePixelRatio = 2 时,输出viewport:

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

在devicePixelRatio = 3 时,输出viewport:

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

这种兼容方案相对比较完美,适合新的项目,老的项目修改成本过大。

  • 优点:

所有场景都能满足

一套代码,可以兼容基本所有布局

  • 缺点:

老项目修改代价过大,只适用于新项目

三、使用border-image实现

准备一张符合你要求的border-image:

.border-bottom-1px {
  border-width: 0 0 1px 0;
  -webkit-border-image: url(linenew.png) 0 0 2 0 stretch;
  border-image: url(linenew.png) 0 0 2 0 stretch;
}

上文是把border设置在边框的底部,所以使用的图片是2px高,上部的1px颜色为透明,下部的1px使用视觉规定的border的颜色。

  • 优点:

可以设置单条、多条表框。

  • 缺点:

更换颜色和样式麻烦,需要更改图片;

某些设备上会模糊。

四、使用background-image实现

background-image 跟border-image的方法一样,你要先准备一张符合你要求的图片。然后将边框模拟在背景上。

样式设置:

.background-image-1px {
  background: url(../img/line.png) repeat-x left bottom;
  -webkit-background-size: 100% 1px;
  background-size: 100% 1px;
}

优缺点与border-image一样;

五、多背景渐变实现

与background-image方案类似,只是将图片替换为css3渐变。设置1px的渐变背景,50%有颜色,50%透明。

样式设置:

.background-gradient-1px {
  background:
    linear-gradient(#000, #000 100%, transparent 100%) left / 1px 100% no-repeat,
    linear-gradient(#000, #000 100%, transparent 100%) right / 1px 100% no-repeat,
    linear-gradient(#000,#000 100%, transparent 100%) top / 100% 1px no-repeat,
    linear-gradient(#000,#000 100%, transparent 100%) bottom / 100% 1px no-repeat
}
/* 或者 */
.background-gradient-1px{
  background:
    -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) left / 1px 100% no-repeat,
    -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) right / 1px 100% no-repeat,
    -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) top / 100% 1px no-repeat,
    -webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) bottom / 100% 1px no-repeat
}
  • 优点:

可以实现单条、多条边框

边框的颜色随意设置

  • 缺点:

代码量不少

圆角没法实现

多背景图片有兼容性问题

六、使用box-shadow模拟边框

利用css 对阴影处理的方式实现0.5px的效果

样式设置:

.box-shadow-1px {
  box-shadow: inset 0px -1px 1px -1px #c8c7cc;
}
  • 优点:代码少,兼容性好。
  • 缺点:边框有阴影,颜色变浅。
相关文章
|
小程序 JavaScript
微信小程序:px与rpx之间转化
微信小程序:px与rpx之间转化
微信小程序:px与rpx之间转化
|
4月前
|
前端开发
移动端1px的解决方案
移动端1px的解决方案
53 0
|
4月前
|
前端开发
面试官【说一下移动端1px的解决方案】
面试官【说一下移动端1px的解决方案】
35 0
设计稿750px移动端自适应,100px=1rem
设计稿750px移动端自适应,100px=1rem
|
9月前
|
移动开发 前端开发
|
前端开发 JavaScript Android开发
|
前端开发
前端使用postcss-px-to-viewport实现移动端或者PC端自适应
前端使用postcss-px-to-viewport实现移动端或者PC端自适应
1133 0
|
前端开发 JavaScript
bilibili 移动端vw / vh 手段制作案例
bilibili 移动端vw / vh 手段制作案例
182 0
bilibili 移动端vw / vh 手段制作案例
|
编解码 前端开发 JavaScript
移动端页面开发适配 rem布局原理
移动端页面开发适配 rem布局原理