移动端像素和 CSS 中 px/view-port/scale

2023-07-23  本文已影响0人  zackzheng

最近做了个小测试,验证 CSS 中的 px 和实际设备屏幕像素的映射关系,记录一下过程。

初始测试

加载本地 html 文件:

<!DOCTYPE html>
<html>

<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type" />
  <style>
    .invoice-box {
      margin: 0;
      padding: 0;
      background-color: #2f44ff;
      width: 645px;
      height: 100px;
    }
  </style>
</head>

<body>
  <div class="invoice-box">
  </div>
</body>

</html>

在像素宽度为 1290 的模拟器(iPhone 14 Pro Max)上运行。

计算得出 645px 确实占 980px 接近 2/3。

测试二

将色块的宽度改为 980px ,验证测试一的结论:

<!DOCTYPE html>
<html>

<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type" />

  <style>
    .invoice-box {
      margin: 0;
      padding: 0;
      background-color: #2f44ff;
      width: 980px;
      height: 100px;
    }
  </style>
</head>

<body>
  <div class="invoice-box">
  </div>
</body>

</html>

测试三

给 body 设置 style:

<!DOCTYPE html>
<html>

<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type" />

  <style>
    .invoice-box {
      margin: 0;
      padding: 0;
      background-color: #2f44ff;
      width: 980px;
      height: 100px;
    }
  </style>
</head>

<body style="margin: 0;padding: 0;">
  <div class="invoice-box">
  </div>
</body>

</html>

实际效果符合预期。

测试四

将 viewport 的宽度设置为 980px,看看是不是测试一结论所说的默认值:

<!DOCTYPE html>
<html>

<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type" />
  <meta name="viewport" content="width=980px;">
  <style>
    .invoice-box {
      margin: 0;
      padding: 0;
      background-color: #2f44ff;
      width: 980px;
      height: 100px;
    }
  </style>
</head>

<body style="margin: 0;padding: 0;">
  <div class="invoice-box">
  </div>
</body>

</html>

测试五

发现 viewport 可以设置 initial-scale,设置为 1 、1.316326(物理像素宽度1290/980)、0.438775(逻辑像素宽度430/980)看看什么效果:

<!DOCTYPE html>
<html>

<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type" />
  <meta name="viewport" content="initial-scale=0.438775;">
  <style>
    .invoice-box {
      margin: 0;
      padding: 0;
      background-color: #2f44ff;
      width: 980px;
      height: 100px;
    }
  </style>
</head>

<body style="margin: 0;padding: 0;">
  <div class="invoice-box">
  </div>
</body>

</html>

scale 实际就是CSS 像素和设备逻辑像素的比例(PC 端则是 CSS 像素和物理像素的比例),从而影响物理像素的呈现。

测试六

发现 view port 的 width 可以设置为 device-width:

<!DOCTYPE html>
<html>

<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type" />
  <meta name="viewport" content="width=device-width; initial-scale=1.0;">

  <style>
    .invoice-box {
      margin: 0;
      padding: 0;
      background-color: #2f44ff;
      width: 430px;
      height: 100px;
    }
  </style>
</head>

<body style="margin: 0;padding: 0;">
  <div class="invoice-box">
  </div>
</body>

</html>

通过修改色块的 width ,可以测出 device-width 等于 430px,即设备的逻辑像素宽度

如果 width 和 scale 冲突,会怎样呢?

小结

移动端元素布局

同时设置 viewport 的 width 和 scale,可以规避横屏的问题,但是元素宽度如果设置为具体值无法达到预期效果,只能设置 % 或 vw。

不同场景下考虑使用不同的方式处理,如不考虑竖屏的情况,方式三似乎是最佳的。

其他概念

layout viewport、visual viewport、ideal viewport

结论

参考

https://www.cnblogs.com/psyduck/p/14347706.html
https://www.cnblogs.com/iifeng/p/11199359.html

上一篇 下一篇

猜你喜欢

热点阅读