CSS is a syntax for specifying the appearance of text and other web content, sizes and lengths are defined in pixels, however pixels have different sizes in different devices, this is the most notable in mobile devices.

If the developer wishes to specify lengths in distance units like centimeters or inches, the CSS specification and its implementations allow the developer to specify their wish but fail to keep their promises. If a box is defined as 1 inch wide, the browser will convert the inch unit to pixels using a fixed ratio and will produce a box 72pixels wide, which will be of different lengths in mobile and desktop displays, none of which will be 1 inch wide.

The question is two fold:

  1. Why can't developers specify lengths using real world SI units?

  2. Why does css appear to support SI lengths but fail to do so? Admitting that it cannot do so would be better than lying. It appears there is a history of a failed feature here.

Thank you.

  • 1
    hacks.mozilla.org/2013/09/css-length-explained - very relevant. Here is the take out in short - a CSS inch is defined in such a way as to appear normally on different displays at different viewing distances. So a CSS inch on a tablet and on a TV will have different lengths. However, if you print out the content, you'd get exact mapping CSS inch for real inch on the page. – VLAZ Apr 2 at 6:16
  • Browsers do know at least the pixel density. Assuming a regular grid-like arrangement of the pixels, the browsers are well able to calculate the number of pixels that span a real world cm. – marstato Apr 2 at 7:39
  • Absolute units, like "cm" and "px" can be useful for page layout intended for printing on paper. Other than that, most web pages are far better designed if absolute units are never used at all. Specifying everything in relative units, like "%", "em", and "vh" allows every browser to format things as they appear best on every device. – Ray Butterworth Apr 2 at 14:07
  • Here is a nice answer, here is an extensive discussion and here is a slightly different (typesetting) perspective. – Vector Zita Apr 2 at 17:59

In general, a computer has no idea of the physical size of the monitor you have plugged in to it. That may be different for smartphones or tablets, of course. But some tablets have HDMI outputs. It's not really helpful to measure sizes in inches or cm anyway. A one inch box on the tablet in front of me appears much bigger than a one inch box on a 50" TV on the other side of the room.

| improve this answer | |
  • Exactly. There’s a reason online rulers require calibration – candied_orange Apr 2 at 13:23
  • The viewing distance is a good point. However, you mention that computers don't have idea of physical sizes, however xrandr on my system returns a fairly accurate measurement of my screen. As Basile points out this might be reported by HDMI and DVI interfaces. – Tomas Zubiri Apr 2 at 17:50
  • 1
    @TomasZubiri this should be expressed as: computers don't reliably report this. If for no better reason then that projectors exist. – candied_orange Apr 3 at 19:48
  • xrandr on my computer accurately reports the height and width of my monitor in mm. As candied_orange mentions, it's probably about reliability. In general, computers know the size of the monitor, but browsers need to run everywhere, and browser features need to run the same for everyone. – Tomas Zubiri Jun 30 at 19:17

Why don't web browsers know the physical dimensions of a display?

On GNU/Linux desktops (in particular those running X11 i.e. Xorg), my Firefox web browser usually don't take the full screen (as reported by xdpyinfo). I am using a non-tiling window manager (sometimes icewm), and have both emacs, thunderbird, firefox opened on a wide LCD screen. Of course I can resize the X11 windows used by firefox.

Even if the browser knew the dimension of the display, it won't be able to use all of it without my approval. Of course firefox knows the dimension of its X11 window.

On my personal home page, you see a photo of my desktop (probably running XFCE). Read also about EWMH and ICCCM.

Also, old CRT monitors (those are still used in many less rich parts of the world) connected by VGA don't reliably report their physical dimensions (or pixel dimensions) to the computer. I believe that you need DVI or HDMI for that.

At last, I do have two different LCD monitors of different dimensions and pixel densities on the same Linux desktop (see xrandr)

Study also the source code of firefox or Qt. Both are open source and cross-platform. You could need a lot of months to study their source code (many millions of lines).

| improve this answer | |
  • 1
    The point is not whether the window size allows for the requested dimensions but whether, given that the requested dimension fits within the window, it is accurate. – marstato Apr 2 at 13:45
  • Yeah, even if the occuppies half the screen, having the physical dimensions of half the screen is what the qeustion is about. It's good to know that monitors started reporting their screen dimensions after vga. – Tomas Zubiri Apr 2 at 17:46
  • The question is: How can a website display a line that is exactly one inch long? When I display things on a 17 inch monitor and you display on a 60 inch screen? – gnasher729 Apr 3 at 5:28
  • In general, it cannot, in particular if the website is seen from a poor country where CRT are commonplace. – Basile Starynkevitch Apr 3 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.