Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS fonts rem trick: 62.5% or 6.25%

Tags:

html

css

fonts

I would like to use font sizing with REM and on internet I found following trick:

html { font-size: 62.5%; } 
body { font-size: 1.4rem; } /* =14px */
h1   { font-size: 2.4rem; } /* =24px */

due to set font-size: 62.5%; coversion rem <-> px (pixels) is very easy (just divide pixel value by 10).

However I wonder - why not use value 6.25% - in that case our trick will be look like this:

html { font-size: 6.25%; } 
body { font-size: 14rem; } /* =14px */
h1   { font-size: 24rem; } /* =24px */

and this approach look to be more direct that for value 62.5% (we can convert rem to px without changing number value) - however I have question - why people "on internet" not use this approach - may be it cause some problems (that I'm not aware)?

like image 604
Kamil Kiełczewski Avatar asked Dec 21 '17 10:12

Kamil Kiełczewski


People also ask

Should I use em or rem for font-size?

EM is relative to the parent element's font size, so if you wish to scale the element's size based on its parent's size, use EM. REM is relative to the root (HTML) font size, so if you wish to scale the element's size based on the root size, no matter what the parent size is, use REM.

Should I use rem for fonts?

For me, usage of rem and em will be very good on the elements that are crucial like font-size, padding, and margin. But, when giving out border-width, I do still use px value because the value we give is so small that it is almost not noticeable if we change the root font-size.

Should I use rem or px CSS?

So to answer our original question, “what changes in our design when using rem instead of px ”. The answer is, for all users not changing root font-size (which, no doubt, is the large majority), absolutely nothing changes and your design looks just as it would with px .


1 Answers

You could use 6.25% so that you get your nice 1:1 rem values, then set font-size: 16em; on body to get the best of both worlds. It'll fix the tiny font problem in old browsers, and browsers that do understand your rem declarations will ignore it by looking to html's font-size when calculating sizes. As far as I can tell there are no drawbacks to doing it this way and you actually get a better sized fallback than you do with 62.5%, but there may be something I've missed so use it with caution.

like image 86
Jon Avatar answered Sep 22 '22 23:09

Jon