This is a question from the past and it was raised a bit strange. But the answer below is more than relevant.
Shoveled a dozen articles, I began to realize that ppi
- a big stick in the wheel. How to deal with it is not yet clear.
So, we have an elementary page: jsfiddle.net
We want 14px
(for example) - as the main font size. Suppose that it was in the PSD
from the designer. From this we will make a start.
We turn around all the content in div.font-size-setting
, which we set the font-size: 1.4em
(since after normalization we had 10px
).
We look at the page from different devices:
If there were iphone4, the font and the square would be even smaller. The problem is obviously a big difference in ppi
( pixels per inch
) devices. Regular monitors have 96ppi
. Do iphone3 163ppi
. Whit 220ppi
.
The obvious solution is to determine the ppi
device and increase the font. There are no problems with the latter. We set in div.font-size-normalize
and depending on the dpi
set the appropriate font: jsfiddle.net
We get:
div.font-size-setting
- to set the font size from the designer,
div.font-size-normalize
- we make this font of the same size (visually) on all devices.
Yablofon slightly stretched the font itself or increased the distance between the lines, but in fact it all fits. Anyway, the square with the dimensions in em
is the same everywhere, even with a ruler.
The question is how to determine ppi
?
1. media queries - resolution
Media queries are able to determine not only the width and height, but also many other characteristics, including dpi
( dots per inch
). In general, dpi
is a meaningless parameter for the display, since it is a parameter for printing by the printer, but in our case it is the same as ppi
.
I decided that it would not be bad to take a step at 0.1em
. This is approximately every 10ppi
. Taking our 96ppi
for 1em
made up a tablet and counted the range so that the values of interest are in the middle. Made requests:
@media screen and (min-resolution: 90dpi) and (max-resolution: 100dpi) { .font-size-normalize { font-size: 1em; /* normal desktops */ } } @media screen and (min-resolution: 101dpi) and (max-resolution: 111dpi) { .font-size-normalize { font-size: 1.1em; } } @media screen and (min-resolution: 112dpi) and (max-resolution: 122dpi) { .font-size-normalize { font-size: 1.2em; } } /*...*/ @media screen and (min-resolution: 158dpi) and (max-resolution: 168dpi) { .font-size-normalize { font-size: 1.7em; /* iphone3 */ } } /*...*/ @media screen and (min-resolution: 216dpi) and (max-resolution: 226dpi) { .font-size-normalize { font-size: 2.3em; /* ps vita */ } } /*...*/ @media screen and (min-resolution: 322dpi) and (max-resolution: 332dpi) { .font-size-normalize { font-size: 3.4em; /* iphone4 */ } }
Recent versions of desktop browsers have worked through media queries. Browsers iphone3 and ps vita are not mastered. I think the last apple apples, apple pads, droid players will still cope, but nevertheless the solution is far from bulletproof: (
2. media queries - devicePixelRatio and the canonical pixel dip - density(device)-independent pixel
- something like a canonical pixel. Equal to one physical pixel at 160ppi
. On the other hand, we have the devicePixelRatio
parameter, which can be used as an argument of a media query relative to the canonical pixel:
@media screen and (device-pixel-ratio: 1) {}
You can write a lot of queries similar to the example above:
@media screen and (min-device-pixel-ratio: 1) and (max-device-pixel-ratio: 1.1) { .font-size-normalize { font-size: 2.3em; /* ps vita */ } }
But I haven’t really done it for any device / computer / browser. It seems that this parameter can only be an integer: unit - for iphone3
, deuce - for iphone4
. Moreover, vendor prefixes are required, which at least indicates a lack of cross-browser or cross-device design. Another inconsistency is that the “ device-pixel-ratio: 2
” (meaning 320ppi
) works on iphone4
, which has 326ppi
.
3. JS
alert(window.devicePixelRatio);
I think it works in the same way as the second method. On iphone3
I saw "1", as well as in the desktop browser and on Windows, which is of course nonsense. Iphone4 showed "2", but after the previous files it does not matter anymore. Another problem: the brakesill failed and showed undefined
.
4. Compiled JS
functions defining ppi
. Something like take device-width
and divide by width
. It works on mobile devices, since you cannot flatten the browser window, but in the desktop browser we can simply reduce the window size and everything will break.
In the end, I did not find a single full-featured solution. I think it just has to take time until the " device-pixel-ratio: n
" will be understood by all. In the meantime, you need to figure out how to maximize the number of successful ppi
definitions. After all, the methods described above are partially, but still work.
Who thinks what about this, what solutions can be offered and how can you summarize the above methods into one half-working option?