简体   繁体   English

如何在CSS中模拟现实世界的尺寸?

[英]How to simulate real world dimensions in css?

I want to make a website, where an object which has for example a width of 100mm , is displayed with that width no matter what resolution the monitor or smartphone has. 我想建立一个网站,无论显示器或智能手机的分辨率如何,都以该宽度显示例如100mm的对象。

For that reason I'm asking the user for his display dimensions to calculate the right ppi . 因此,我要求用户提供他的显示尺寸以计算正确的ppi However it doesn't work on all devices. 但是,它并不适用于所有设备。

EDIT: This is the code I have so far: 编辑:这是我到目前为止的代码:

var dpr = window.devicePixelRatio;
var inch = 25.4;

var pixelHeight = screen.height * dpr;
var pixelWidth = screen.width * dpr;

function calculatePpi(monitorDiagonal) {
    return (Math.sqrt(Math.pow(pixelWidth, 2) + Math.pow(pixelHeight, 2))) / monitorDiagonal;
}

function mmToPx(mm) {
    return  (((mm / inch) * calculatePpi(monitorDiag)));
}

Use css height and width. 使用CSS高度和宽度。

The height and width can be set to auto (this is default. Means that the browser calculates the height and width), or be specified in length values, like px, cm, etc., or in percent (%) of the containing block. 高度和宽度可以设置为自动(这是默认设置。这意味着浏览器会计算高度和宽度),也可以使用长度值(例如px,cm等)或包含块的百分比(%)进行指定。

So if for example you want to show 35cm width and 50cm height element you can set it with css: 因此,例如,如果您想显示35cm宽和50cm高的元素,则可以使用CSS进行设置:

#example {
  height: 50cm;
  width: 35cm;
}

Good luck! 祝好运!


It seems that although this should work, it's not enough accurate. 看来,尽管这应该可行,但不够准确。 See comments. 看评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM