Really quick question. Right now whenever I start a new site I do font-size like this; I set a :root font-size in PX then use REM for everywhere else regarding font-size. Is this the best practice or is there a better way? Pros cons in using percentage, responsive design conflicts etc. Many Thanks
Update: Is it bad practice to place the font-size in the root? A model css example would be amazing, I’d be extremely grateful. Thanks
@PaulOB would probably be the best person to answer it but I’ve always set the base font size (applied to body) to a percentage, then all other font sizes to rems. That allows the user to control the font-size that is comfortable to them and the rest of the font sizes will scale accordingly.
I’ve seen some people who’ve used font size scaled to the viewport width, but you have to be careful to set a minimum/maximum to those as well, which hurts my head sometimes, hence why I use the previous approach.
Thanks very much for your quick response. That percentage logic makes sense, so basically the entire site is based on what the user has set their browser settings to and the then as you said it all adjusts accordingly.
On Google I keep seeing these examples where they do this, but then i see others with 100%, some putting it in the body, some html some :root so i just need to get some clarity haha:
html {
/* 62.5% of 16px base font size is 10px */
font-size: 62.5%;
}
You mentioned how you place the base font-size in the body, is that better practice than setting in the :root?
For me personally, it’s semantics or personal choice. I limit :root to css variables and put normal declarations within the appropriate scope, hence why I put a base font size for the site in body.
My recommendation is to avoid setting font-size on the :root , <html> or <body> elements in favor of letting the browser’s default size serve as a baseline from where we can cascade our own styles. Since this default is accessible, the content will also be accessible. The WACAG 2.2 working draft states that:
When using text without specifying the font size, the smallest font size used on major browsers for unspecified text would be a reasonable size to assume for the font.
For all purposes you can assume the root is 16px and then size the rest of your page accordingly which allows users to adjust their own base size accordingly. .
All your input together has given my brain clarity again… yay!!!
The key bit i was missing is by doing nothing… REM unit simply goes back to basing the font-size on the default browser font size which is usually 16px. If the user sets the browser font-size to 20px everything adjusts accordingly.
There seems to be some outdated code in that article but I don’t really like to criticise other peoples methods as everyone has their own way of working and bending the rules sometimes is ok if you take care.
[off-topic]
I noticed the Medium linked article which only has two free article views per month
The article has a GitHub link which I really like because GitHub is designed to be ongoing and to be updated as and when innovations or standards change.
Perhaps @PaulOB could create an ongoing GitHub Repository for a basic page layout especially since articles quickly become outdated!
Edit:
GitHub is free, does not insist on membership before source code Zips can be downloaded and also has an abundance of other features. Designed by Linus Torvalds for his Linux Operating System source code… which was created over twenty years ago…
[/off-topic]
That’s actually a really good idea and I’m sure would help a lot of people out. @PaulOB
A kind of up to date HTML/CSS boilerplate featuring the latest standards expected of websites. Accessibility, semantics to help give people who want to produce follow industry standards from the get-go a proper good visual example of what their HTML bare-minimum should look like and include.
Right now i start every site with this HTML. Works for me but is it the best initial setup?
I’m not keen on putting code out there that people can (permanently) copy and paste as CSS changes so fast these days that practices are only best practices for a short time before the next new shiny thing comes along.
There are some basics of course but I would rather authors define their own rules based on the design in hand rather than continually copying and pasting without thinking about what they are doing.
Although the original (Eric Meyer) CSS reset from many many years was great at the time it ended up being included in thousands (if not millions of sites) and although mentioned in the code itself that the author should set suitable :focus styles (as they were removed) virtually no one did. In the end it just encouraged the copy and paste approach and a lot of authors didn’t actually learn anything.
There are many resets and boiler plates around (written by people more experienced than me) that are good to look at and study and work out why code is included and what it does but I would not include them all by default. I would look through them and pull out the pieces that suit the project in hand. At least I would ensure that I understood what the code was doing and why it was included.
That will remove all the margins from everything and then when you use the element you have to set your own margin as required. That would seem to make it a little pointless. Why not set the margin on the elements you use as you use them. Its good that you removed padding from it as that breaks some form elements (although its not as much of an issue as it was in the olden days ;)).
That’s only needed for 100% height layouts which is not that often (and we do have 100vh height now if needed anyway - although there are issues in ios with that). When you set the body to height:100% you are effectively capping the height at viewport height and then the layout overflows. This is not a problem but just not clearly understood by those that copy and paste and wonder why they can’t use height:100% everywhere.
If I look at some basic styles I included a little while ago in a site I see something like this.
If I look at that rule then I see that I have included a img{border:none}. That was originally for IE6 that set images inside links to have blue borders. Does that still happen? Is that code still needed. Copy and paste stops you thinking for yourself and checking if the code is still necessary.
You can lose the trailing closing slashes as they are not needed in html5.