解决移动端 1px 的问题:快拿走!

2,641 阅读1分钟

一、关于移动端 1px 的问题

CSS 的 1px 在移动端往往大于 1px。不信可以自测!

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动端 1px 的问题</title>
    <style>
        body {
            margin: 0;
            padding: 20px
        }
        div {
            padding: 1em;
            margin: 1em;
        }
        .box1 {
            border-top: 1px solid #000;
        }
        .box2 {
            border-bottom: 1px solid #000;
        }
    </style>
</head>

<body>
    <div class="box1">
        1px 的上边框
    </div>

    <div class="box2">
        1px 的下边框
    </div>
</body>

</html>

image.png

二、产生原因

  • 设备像素比:dpr=window.devicePixelRatio,也就是设备的物理像素与逻辑像素的比值。
  • retina屏的手机上, dpr23css里写的1px宽度映射到物理像素上就有2px3px宽度。
  • 例如:iPhone6dpr2,物理像素是750(x轴),它的逻辑像素为375。也就是说,1个逻辑像素,在x轴和y轴方向,需要2个物理像素来显示,即:dpr=2时,表示1个CSS像素由4个物理像素点组成。

三、解决方案:伪元素 + transform 缩放

一条 1px 的边框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动端 1px 的问题</title>
    <style>
        body {
            margin: 0;
            padding: 20px
        }
        div {
            padding: 1em;
            margin: 1em;
        }
        .box1 {
            border-top: 1px solid #000;
        }
        .box2 {
            position: relative;
            /* border-bottom: 1px solid #000; */
        }
       /* ◆ box2 伪元素 + 缩放 实现 1px 的效果 */
        .box2::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 1px;
            background-color: #000;
            transform: scaleY(0.5);
        }
    </style>
</head>

<body>
    <div class="box1">
        1px 的上边框
    </div>

    <div class="box2">
        1px 的下边框
    </div>
</body>

</html>

image.png

四条边 1px 的边框

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动端 1px 的问题</title>
    <style>
        body {
            margin: 0;
            padding: 20px
        }
        div {
            padding: 1em;
            margin: 1em;
        }
        .box {
            position: relative;
            width: 200px;
            height: 200px;
        }
        .box:after {
            content: " ";
            position: absolute;
            top: 0;
            left: 0;
            width: 200%;
            height: 200%;
            transform: scale(0.5);
            transform-origin: left top;
            box-sizing: border-box;
            border: 1px solid #000;
            border-radius: 4px;
        }
    </style>
</head>

<body>
    <div class="box">
        1px 的四条边框
    </div>
</body>

</html>

image.png