美文网首页
移动端边框「1px」的问题

移动端边框「1px」的问题

作者: TingsLee | 来源:发表于2019-06-26 15:11 被阅读0次

原因:由于现在的手机几乎都是retina屏,css设置的1px会被渲染成2px的物理像素(针对像素比等于2的屏幕),因此看起来会比较粗。

方案:

  1. 直接设置0.5px

ios8+可以识别浮点类型的单位,因此可以渲染这个0.5px。然而,绝大部分的android机是不支持浮点类型单位的。所以这种方案pass...

  1. 利用背景图

不管是border-image,还是background-image,图片的弊端还是很明显的:想改变颜色就必须要换图片,而且利用图片也比较麻烦。所以不推荐这种方案...

  1. viewport+rem实现

同时通过设置对应viewportrem基准值,这种方式就可以像以前一样轻松愉快的写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">

这种兼容方案相对比较完美,适合新的项目,老的项目修改成本过大。对于这种方案,可以看看《使用Flexible实现手淘H5页面的终端适配rem自适应布局》

  1. 多背景渐变实现

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}

这种方案显示是比较牛的,不仅实现了1px的边框,还能实现多条边框。缺点就是不能实现圆角的1px边框,浏览器的兼容性也要考虑...

5、伪类 + transform 实现

个人认为伪类+transform是比较完美的方法。利用:before或者:after 实现 border ,并transformscale缩小一半,将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;
}

4条border的实现:

.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;
}
或者:
.scale-1px:after{
    content:'';
    position:absolute;
    border:1px solid #000;
    top:-50%;
    right:-50%;
    bottom:-50%;
    left:-50%;
     -webkit-transform:scale(0.5);
    transform:scale(0.5);
}

相关文章

  • 移动端适配及1px边框问题

    1、移动端适配 2、1px边框问题

  • 前端遇到的那些技术难点及性能优化之css篇

    移动端兼容 css篇 移动端的 1px 问题描述:1px 的边框。在高清屏下,移动端的 1px 会很粗。 产生原因...

  • 移动端1像素边框问题

    移动端1像素边框问题: 设置一个div的底部边框为 1px solid #000; 实际表现却是边框线是模糊的,或...

  • 1px

    CSS中1px分割线处理移动web开发之像素和DPR详解7种方法解决移动端Retina屏幕1px边框问题IOS基础...

  • 1px边框问题

    问题:ui设计稿中边框为1px,在实际的前端开发,移动端设备中会出现设置border:1px solid #000...

  • vue慕课网去哪儿实战项目笔记

    1.移动端解决1px边框问题,使用border.css;https://www.cnblogs.com/jy136...

  • web前端面试题

    1 移动端适配1px的问题 构建1个伪元素, 将它的长宽放大到2倍, 边框宽度设置为1px, 再以transfor...

  • 移动端1px边框问题

    造成边框变粗的原因 其实这个原因很简单,因为css中的1px并不等于移动设备的1px,这些由于不同的手机有不同的像...

  • 移动端1px边框问题

    解决方案一 使用rem为单位。 使用js获取dpr值动态计算视口缩放,设置viewport content属性。 ...

  • 移动端1px边框问题

    自己开发出的边框粗于设计师给定的宽度吗?设计师验收通过不过吗?如果你也遇到看看解决办法吧。 解决方法一:伪类+tr...

网友评论

      本文标题:移动端边框「1px」的问题

      本文链接:https://www.haomeiwen.com/subject/ntyccctx.html