3 ms·
Excerpt from a personal page I’m in the process of writing: :root { /* We wish to make the width range 768..1400 correspond to font-sizes 16..24.
by chrismorgan 11y ago
Excerpt from a personal page I’m in the process of writing:
: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);
}
- david-given 11y agoI found myself wanting to do precisely your last example not long ago. I hadn't thought of using min-width and max-width media selectors to fake min and max; that's really neat. ...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!
- pygy_ 11y agoThere's also this SASS mixin: http://www.adrenalinmedia.com.au/the-agency/insights/this-changes-everything-css-fluid-properties.aspx http://www.adrenalinmedia.com.au/the-agency/insights/this-ch... The linear equation solver is there, but it assumes px, and there's no license...