Doing responsive text by using media queries on :root to change the font size for smaller screens.

While trying to figure out a good way to do responsive text without manually setting font size using each media queries, I came up with an approach. I'll use rem everywhere for font-size and then use media queries on my html tag to change the size of root. This way I can control the breakpoints from one place which is something I've been struggling with.
html {
@media screen and (min-width: 100px) { font-size: 1px; }
@media screen and (min-width: 640px) { font-size: 10px; }
@media screen and (min-width: 1024px) { font-size: 16px; }
@media screen and (min-width: 1280px) { font-size: 20px; }
}
html {
@media screen and (min-width: 100px) { font-size: 1px; }
@media screen and (min-width: 640px) { font-size: 10px; }
@media screen and (min-width: 1024px) { font-size: 16px; }
@media screen and (min-width: 1280px) { font-size: 20px; }
}
Is this a good or a terrible idea? I can quickly change font-size and breakpoints of the entire app from one place instead of going to every place and change the rem value for font, which is pain to do. Edit: shoul've used rem instead of px inside media queries.
15 Replies
Jochem
Jochem2mo ago
it's terrible for accessibility setting font size in pixels takes away the end user's ability to adjust the font size to suit their needs
o_O
o_O2mo ago
I am using rem not px. the pixel is only used on the root element so you can change the zoom level and it'll be respected.
Jochem
Jochem2mo ago
you can change the zoom level, but not the font-size if all you need is a slightly larger font you can try using this and setting the font size in the html using rems too if you prefer, but you should never set a font size with pixels, at least not without using at least some (r)em component
o_O
o_O2mo ago
you're right. changing the font-size setting in my browser isn't changing it on the website. thanks for pointing it out.
Jochem
Jochem2mo ago
it still works if you use rem in the html element too, at least in firefox probably chrome too, just haven't tested there
o_O
o_O2mo ago
works on chromium too. just tested with rem instead of px on html
Jochem
Jochem2mo ago
it does make for a nice centralization if you do this though. You can probably also use variables if you wanted, depending on how you want to lay out your css
o_O
o_O2mo ago
You can probably also use variables if you wanted,
yea I saw kevin's video in which he used clamp() with vw and css variables. which method would you recommend? css vars or the way I did it.
Jochem
Jochem2mo ago
honestly don't have enough experience using either to make a sensible recommendation
o_O
o_O2mo ago
do you manually set it everywhere when you build your apps?
Jochem
Jochem2mo ago
purely from a code-cleanliness point of view, I'd lean towards variables though. This is effectively abusing the font size as a variable, and while I can't immediately think of any downsides, there might be ones I'm missing most of my experience is in building business software that is 100% intended for use on desktops. My last big project even had a monitor size requirement we set, so I don't mess with media queries a ton
o_O
o_O2mo ago
not having to worry about responsiveness actaully sounds really good lol
Jochem
Jochem2mo ago
it's freeing 😄 Not as useful if you want to build websites nowadays though
o_O
o_O2mo ago
since I am using tailwind and tailwind uses rem for everything, changing base font on html is messing up the entire app for me. for this project I'll go with using css variable in conjunction with css clamp and for my next one I'll try the afomentioned method. thanks mate! ♥️
Jochem
Jochem2mo ago
no worries! Good luck 🙂
Want results from more Discord servers?
Add your server
More Posts