CSS 以厘米为单位的 Div 宽度(英寸)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4023192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Div width in cm (inch)
提问by Damir
I need to put in my site div which is exactly 25cm width ( 10 inch) in every display. How I can do it ?
我需要在每个显示器中放入我的站点 div,它的宽度正好是 25 厘米(10 英寸)。我该怎么做?
采纳答案by Tomas
回答by Pekka
How I can do it ?
我该怎么做?
You can't.Update: apparently, you can on many modern systems: Check out @Tomas's answer. It seems not to be always entirely reliable, though.
你不能。更新:显然,您可以在许多现代系统上使用:查看@Tomas 的答案。不过,它似乎并不总是完全可靠。
Old answer:You can't. Monitors display different numbers of pixels. The pixel size varies wildly from monitor to monitor.
旧答案:你不能。显示器显示不同数量的像素。像素大小因显示器而异。
There are ways to interpolate the pixel size if you know the monitor size. This information is sometimes available to the operating system; however, it is impossible for a web site to get hold of this information.
如果您知道显示器尺寸,则有多种方法可以插入像素尺寸。该信息有时可供操作系统使用;然而,网站不可能获得这些信息。
The only way to go would be to have the user do a calibration. For example, ask the user to hold an A4 piece of paper to the monitor, and use a draggable ruler to determine the area it covers. Using that information, you can then calculate how many pixels you will need to show 25 centimeters.
唯一的方法是让用户进行校准。例如,让用户将一张 A4 纸放在显示器上,并使用可拖动的标尺来确定其覆盖的区域。使用该信息,您可以计算出显示 25 厘米需要多少像素。
Update:@Tomas claims in his answer that using CSS cm
values works on screen.
更新:@Tomas 在他的回答中声称使用 CSScm
值可以在屏幕上使用。
This is in fact true on my Windows 7 and 23" Plug&Play TFT Monitor (1920x1080 Pixels): 21cm
translates perfectly to the short side of a A4 sheet of paper in Chrome 7, IE6(!), IE7, Firefox 3.6.
这实际上在我的 Windows 7 和 23" 即插即用 TFT 显示器(1920x1080 像素)上是正确的:21cm
在 Chrome 7、IE6(!)、IE7、Firefox 3.6 中完美地转换为 A4 纸的短边。
It doesn't seem to be entirely reliable, though: @Yi Jiang can't get it to work on a TFT using Ubuntu Linux; also, older Monitors may not send through their size information so it'll be impossible for the OS to determine a correct size.
不过,它似乎并不完全可靠:@Yi Jiang 无法在使用 Ubuntu Linux 的 TFT 上运行;此外,较旧的监视器可能不会发送其大小信息,因此操作系统无法确定正确的大小。
Here's a simple JSFiddlefor testing.
这是一个用于测试的简单 JSFiddle。
回答by Richard
You can't. A program can only get the true physical dimensions of a screen by interrogating EDID as values returned by the Windows API are not reliable. A program can get the true values for resolution (e.g.1280 x 1024) and screen dpi, but browsers can't do any of this by themselves.
你不能。程序只能通过询问 EDID 来获取屏幕的真实物理尺寸,因为 Windows API 返回的值不可靠。程序可以获得分辨率(例如 1280 x 1024)和屏幕 dpi 的真实值,但浏览器本身无法做到这些。
There is a constant confusion between the "physical dpi" of a screen and "screen dpi". The physical dpi, more properly called pixels per inch, is obtained by dividing the maximum pixel width of the screen by the physical (ruler) width in inches. The pixels per inch are fixed by the manufacturing process. The screen dpi is a number that the user can set via the Control Panel and it's only purpose is to convert a value in inches into a number of pixels. The user settable screen dpi value has no direct relationship whatever with the physical dpi (pixels per inch) and is just a number with a default value of 96. There is nothing magic about 96, or 120
屏幕的“物理 dpi”和“屏幕 dpi”之间一直存在混淆。物理 dpi,更恰当地称为每英寸像素,是通过将屏幕的最大像素宽度除以以英寸为单位的物理(标尺)宽度而获得的。每英寸像素数由制造过程固定。屏幕 dpi 是用户可以通过控制面板设置的数字,它的唯一目的是将英寸值转换为像素数。用户可设置的屏幕 dpi 值与物理 dpi(每英寸像素数)没有任何直接关系,只是一个默认值为 96 的数字。 96 或 120 没有什么神奇之处
Number of screen pixels = number of inches x screen dpi
It's as simple as that.
屏幕像素数 = 英寸数 x 屏幕 dpi
就这么简单。
The reason 21 cm on a 23 inch monitor at 1920 x 1080 "translates" to the width of an A4 sheet (21 cm) is because with a 23 inch diagonal the screen width is 20.05 inches and at 1920 pixels across the pixel density is 95.76 pixels per inch.
With screen dpi default value of 96 then for one inch: pixels = 1 x 96 = 96 pixels
The pixel density of the 23 inch screen is 95.76 pixels per inch which matches the number of pixels you get, when specifying a length of one inch, with the default screen dpi value of 96.
1920 x 1080 的 23 英寸显示器上的 21 厘米“转换”为 A4 纸的宽度(21 厘米)的原因是因为 23 英寸对角线的屏幕宽度为 20.05 英寸,1920 像素的像素密度为 95.76每英寸像素数。
屏幕 dpi 默认值为 96,然后为一英寸:像素 = 1 x 96 = 96 像素
23 英寸屏幕的像素密度为每英寸 95.76 像素,这与指定一英寸长度时获得的像素数相匹配,默认屏幕 dpi 值为 96。
If screen dpi is changed in the Control Panel, or the monitor video resolution is changed, then 21 cm would not match the width of a sheet of A4.
如果在控制面板中更改了屏幕 dpi,或者更改了监视器视频分辨率,那么 21 厘米将与一张 A4 纸的宽度不匹配。
回答by ChrisF
You will need to get hold of the resolution of the display and the dot pitchof the monitor to be able to calculate this.
您需要掌握显示器的分辨率和显示器的点距才能计算出来。
Given these two values you'll be able to calculate the number of pixels you need.
鉴于这两个值,您将能够计算出所需的像素数。
However, you can't get hold of this information from a web site.
但是,您无法从网站获取此信息。
回答by Spudley
Given that you state it's a <div>
in a site, we know you're in a web browser environment.
鉴于您声明它是一个<div>
站点,我们知道您处于 Web 浏览器环境中。
Sadly for you, the web browser doesn't have any way to find out the screen's DPI. You can find out what the screen resolution is, so you'll know whether the user has 1024x760 or whatever, but you'll never know whether those 1024x768 pixels are being displayed on an iPhone sized screen or a billboard, or anything in between.
遗憾的是,Web 浏览器无法找到屏幕的 DPI。您可以找出屏幕分辨率是多少,因此您将知道用户是否有 1024x760 或其他分辨率,但您永远不会知道这些 1024x768 像素是显示在 iPhone 大小的屏幕上还是广告牌上,或者介于两者之间。
Sorry about that.
对于那个很抱歉。
回答by Bobby Hyman
I believe the layout engine would need to know three things to make this possible:
我相信布局引擎需要知道三件事才能做到这一点:
- Screen resolution
- DPI
- Physical monitor size
- 屏幕分辨率
- 新闻部
- 物理显示器尺寸
As far as I'm aware, it doesn't know all three.
据我所知,它不知道所有三个。