Let's say we have the following test.htm
:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
.testdiv, .testdivB, .testdivC {
width: 50%;
height: 30%;
margin: 6px;
border: 2px solid gray;
font-size: 4em;
}
.testdivB {
font-size: 12px;
}
.testdivC {
font-size: 30pt;
}
</style>
</head>
<body>
<div class="testdiv">Test one writing</div>
<div class="testdivB">Test another writing</div>
<div class="testdivC">Test three writing</div>
</body>
</html>
I've tried testing this in Chromium, with the Developer Tools, which allows me to emulate different devices; here:
* Apple iPhone 4 : device resolution 320 x 480 , a/r 0.666667
* Google Nexus 10 : device resolution 800 x 1280 , a/r 0.625
* Amazon Kindle Fire HDX: device resolution 1600 x 2560 , a/r 0.625
the outcome is this:
In this example, all the devices are portrait, and have "nearly" the same aspect ratio (~ 0.6+), even if they have wildly differing resolutions.
So, in the two first cases, I'm getting approximately the same font sizes, as relative to the device screen size, which is what I want to achieve (even though, I'm confused how can testdivB
also show as nearly the same size, when it should be smaller) - so that's all good.
But, in the third example, which is on a device with the largest resolution, testdivB
unsurprisingly (as it is defined in px
) is much smaller relative to the device screen size; testdivA
is also not really surprising, since em
is relative to the "computed font-size" of the browser - and that one, if I remember correctly, is also expressed in pixels.
However, I thought that pt
would have taken the device screen resolution into account, but it looks like it doesn't:
http://www.w3.org/Style/Examples/007/units.en.html
In the past, CSS required that implementations display absolute units correctly even on computer screens. But as the number of incorrect implementations outnumbered correct ones and the situation didn't seem to improve, CSS abandoned that requirement in 2011. Currently, absolute units must work correctly only on printed output and on high-resolution devices.
Ok, so what options do I have, to achieve approximately the same font sizes (as relative to device screen sizes) in all three cases:
- If I don't want to use JavaScript (only HTML + CSS)?
- If I do want to use JavaScript (I'd guess there's a way to query for device screen - not viewport - resolution, but I'm not sure if there really is one)?