小程序1rpx边框不完美

问题展示

原因

rpx类似rem,渲染后实际转换成px之后可能存在小数,在不同的设备上多多少少会存在渲染的问题。而1rpx的问题就更加明显,因为不足1个物理像素的话,在IOS会进行四舍五入,而安卓好像统一向上取整,这也是上面两种设备表现不同的原因

解决方法

我们采用的方法是采用translate:scale(0.5)的方法对边框进行缩放

css 复制代码
.select-box{
  position: relative;
  border-width: 0rpx !important;
  padding: 0.5rpx;
  z-index: 0;
}
.select-box::after{
  box-sizing: border-box !important;
  position: absolute;
  border-width: 2rpx !important;
  left: 0;
  top: 0;
  width: 200% !important;
  height: 200% !important;
  transform-origin: 0 0;
  transform: scale(0.5) !important;
  z-index: -1;
}
  • 给.select-box的元素设置边框宽度为0
  • 给::after伪元素宽高为两倍,边框设置2rpx,
  • 边框其他样式继承元素的设置
  • 然后再缩放0.5来达到边框为1rpx的效果

题外话

border: 1rpx solid #ccc = border-width、border-style、border-color

相关推荐
double_eggm10 小时前
微信小程序2
微信小程序·小程序
慕斯fuafua15 小时前
CSS——盒模型
前端·css
尽欢i16 小时前
前端响应式布局新宠:vw 和 clamp (),你了解吗?
前端·css
小李子呢021119 小时前
前端八股CSS(1)---响应式布局的方法
前端·css
码视野19 小时前
课后报名小程序 — 从需求到原型的全栈实践
小程序
周周记笔记20 小时前
初识HTML和CSS(一)
前端·css·html
aq553560020 小时前
网页开发四剑客:HTML/CSS/JS/PHP全解析
javascript·css·html
下北沢美食家1 天前
CSS面试题2
前端·css
小霍同学1 天前
Flex + Grid 混合布局指南
css
打瞌睡的朱尤1 天前
微信小程序1~25
微信小程序·小程序