Precise control over responsive typography
madebymike.com.au
madebymike.com.au
:root {
/* We wish to make the width range 768..1400 correspond to font-sizes 16..24.
To convert this to the form font-size: calc(x * 100vw + y),
we solve the simultaneous equation 1400 * x + y = 24, 768 * x + y = 16
This comes to x = 1 / 79, y = 496 / 79. So, */
font-size: calc((100vw + 496px) / 79);
}
/* min-font-size: 14px; */
@media (max-width: 610px) {
:root {
font-size: 14px;
}
}
/* max-font-size: 24px; */
@media (min-width: 1400px) {
:root {
font-size: 24px;
}
}
/* One extra rule: if the screen isn’t tall enough (arbitrarily selected to be 768px high), cap that font-size at 20px (which would by the earlier formula be reached at width 1084px (20*79-496)).
Ideally we’d use a font-size rule factoring both vw and vh into it, but that kind of stuff takes too much thought for me at the moment. Yes, dear reader, I am lazy. Can you credit it? */
@media (max-height: 768px) and (min-width: 1084px) {
:root {
font-size: 20px;
}
}
I’ve been contemplating replacing the last one with an aspect-ratio query, a technique that occurred to me later for something in the arrangement where I really needed it.Still, with all of this sort of fanciness, I find myself so much missing min(), max() and broad custom properties support. (Especially the latter. People may think their preprocessor variables cover all they need, but they are utterly wrong. They are an amazing tool for more responsive design, making advanced techniques which are agony at present—and thus typically skipped, leaving “responsive” mostly very boring and clumsy—easy and logical.)
I really wish I could do the first three like this:
:root {
font-size: max(14px, min(calc((100vw + 496px) / 79), 24px);
}...I wonder if a CSS preprocessor could implement min and max using this technique?
Edit: d'oh, this is precisely what the article was talking about!
The linear equation solver is there, but it assumes px, and there's no license...
If I'm holding a 6 inch tablet, or a 9 inch tablet, both 2 feet away from my face and with potentially different ppi, there's no reason for the 9 inch tablet to have 50% larger font size.
Declaring size relative to pixels solves this only when PPI is constant.
More specifically, in CSS 2.1, a “reference pixel” has an angle that corresponds to one physical pixel on a 96dpi display at a nominal arm’s length distance of 28in. A measurement of 1px is then recommended to refer to the whole number of device pixels that best approximates the reference pixel.
In principle, that means whether you’re holding a phone or tablet with a Retina-class display relatively close, looking at a traditional large format desktop screen with fewer pixels-per-inch, or looking at a projected screen from a typical viewing distance in a lecture theatre, sizing in terms of px should still have approximately the same effect.
However, you do still have a difference of up to 1/3 of the nominal size to contend with: a browser running on a device where a physical pixel is around 150% the size of a nominal reference pixel has to choose whether to use 1 or 2 display pixels for each px, resulting in output that is either 2/3 or 4/3 of the theoretical ideal size.
I’ve been assuming this is part of the reason we see so many blogs currently displaying irritatingly large body text if they’re viewed on a desktop or laptop screen: the designers found that a more traditional font-size was too small for comfortable reading on whatever mobile device they were using at the distance they prefer to hold it, so they increased it.
For more on the CSS px definition, you can check the W3C spec:
The goal here is to adjust font size appropriately for users with different viewport widths. The interactive smooth-scaling effect that you get when drag-resizing the browser window is nice, but it's a side effect, not the actual goal of the technique.