如何根据设备 pixelRatio 动态生成 .5px 边框?

解读

面试官抛出这道题,核心想验证三件事:

  1. 你是否知道移动端 1px 问题本质——CSS 像素与物理像素不对齐;
  2. 你是否能在 Less 这一「编译时」预处理语言里,把「运行时」才能拿到的 dpr 值传进来;
  3. 你能否用 Less 的变量、混合、条件、运算等能力,把「1 根物理像素」换算成「CSS 像素」并输出可维护的代码,而不是写死 0.5px、0.333px 一堆魔法数。
    国内面试场景里,这道题常作为「移动端适配」环节的压轴小题,答出「媒体查询 + 伪元素缩放」只能算及格;如果能用 Less 封装成「一套带语义化参数、可插拔、可扩展的 Mixin」,并且把 dpr 的注入方式讲清楚,基本就稳了。

知识点

  1. 设备像素比(window.devicePixelRatio,简称 dpr):物理像素 / CSS 像素。
  2. 1px 问题:dpr=2 时,1 CSS 像素对应 2×2 物理像素,线条看起来「粗」。
  3. Less 变量作用域与默认参数:允许给 Mixin 设默认值,调用时可覆盖。
  4. Less 条件语句(when 关键字):在编译期做分支,生成多套 CSS。
  5. Less 运算与单位:支持 1 / @dpr 这种数学运算,结果会自动带单位。
  6. 运行时注入:Less 的「编译时」本身拿不到 dpr,需要借助 JS 把 dpr 写到 html 的 data-dpr 属性或 CSS 变量,再由 Less 生成的代码去读取。
  7. 伪元素缩放方案:用 transform: scaleY(0.5) 把 1px 高的伪元素压成 0.5 物理像素,兼容老机型;配合 @media (min-resolution: 2dppx) 可做兜底。
  8. 国内主流方案:
    • 手淘 flexible:JS 设置 data-dpr,Less 里用 @dpr: data-dpr; 做运算;
    • 微信、小程序:rpx 体系已屏蔽 1px 问题,但 H5 仍需自己处理;
    • Vite/Webpack 项目:借助 postcss-px-to-viewport 或 postcss-write-svg 做后处理,但面试官更想听你「纯 Less」思路。

答案

  1. 先定义「运行时」把 dpr 注入到 html 标签:
// app.js 里放三行,国内 CDN 稳定
const dpr = window.devicePixelRatio || 1;
document.documentElement.setAttribute('data-dpr', dpr);
  1. Less 端写一个语义化 Mixin,支持「方向 + 颜色 + 圆角」扩展:
// 默认读取 html 上的 data-dpr,允许调用者手动覆盖
.border-1px(@dir: bottom, @color: #e5e5e5, @dpr: ~`document.documentElement.getAttribute('data-dpr') * 1`) {
  // 先清空旧伪元素,避免重复调用叠加
  &::before { display: none; }

  // 计算 CSS 像素值,1 物理像素 ÷ dpr
  @css-px: 1 / @dpr;

  position: relative;
  &::after {
    content: '';
    position: absolute;
    .set-border(@dir);
    background-color: @color;
    transform-origin: 0 0;
    pointer-events: none;
  }
}

// 抽方向设置,保证代码整洁
.set-border(@dir) when (@dir = bottom) {
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(@css-px);
}
.set-border(@dir) when (@dir = top) {
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(@css-px);
}
.set-border(@dir) when (@dir = left) {
  left: 0;
  top: 0;
  width: 1px;
  height: 100%;
  transform: scaleX(@css-px);
}
.set-border(@dir) when (@dir = right) {
  right: 0;
  top: 0;
  width: 1px;
  height: 100%;
  transform: scaleX(@css-px);
}
.set-border(@dir) when (@dir = all) {
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border: 1px solid transparent;
  transform: scale(@css-px);
  transform-origin: 0 0;
}

// 业务里一行调用
.card {
  .border-1px(bottom, #ddd);
}
  1. 编译产出(dpr=2 时):
.card::after {
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1px;
  background-color: #ddd;
  transform: scaleY(0.5);
  transform-origin: 0 0;
  pointer-events: none;
}
  1. 如果项目强制「不运行 JS」,可退而求其次,用媒体查询写死 2/3 档:
.border-1px-fallback(@color: #e5e5e5) {
  @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 2dppx) {
    &::after { transform: scaleY(0.5); }
  }
  @media (-webkit-min-device-pixel-ratio: 3), (min-resolution: 3dppx) {
    &::after { transform: scaleY(0.333); }
  }
}

但国内面试官普遍认为「媒体查询写死」只是保底,不能算「动态」。

拓展思考

  1. 圆角怎么办?
    伪元素缩放会把圆角也压扁,需要把 border-radius 也除以 dpr,或者改用 svg 背景、post-write-svg 插件。
  2. 暗黑模式 + 1px?
    把 @color 换成 CSS 变量 var(--border-color),让运行时切换主题即可,Less 只负责生成变量占位。
  3. 鸿蒙、快应用新容器?
    官方已提供 physical-pixel 单位,未来可能不再需要 0.5px Hack,但 Less 侧仍可用同一套 Mixin 做向下兼容。
  4. 编译时性能?
    如果项目里几千处调用,建议把 data-dpr 抽成全局 Less 变量,避免每处都执行一次 JS 表达式;本地开发阶段用 Vite 的 define 注入,生产阶段用 CSS 变量兜底。