Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome not respecting rem font size on body tag?

I've taken to using rem's to size fonts in recent projects, then using px as a fallback for older versions of IE.

I've also been setting a font-size of 62.5% on thehtml so I can more easily set font sizes later on in the stylesheet, I then set a font-size of 1.4rem on the body so unstyled elements have a base font-size of at least 14 pixels, see the code below:

html { font-size: 62.5%; } /* font-size: 62.5% now means that 1.0 rem = 10px */
body { background: #fff; font-family: arial; font-size: 1.4rem; line-height: 1.6rem; }

The problem is, Chrome seems to handle this in a strange way ... Chrome seems to set the font sizes correctly on the inital page load, but on subsequent refreshes the font sizes are way bigger than they should be.

SEE FIDDLE (HTML copied below for future reference)

<!DOCTYPE html>
<html lang="en-GB">
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <p>This is a test, this font should have font-size of 14px.</p> 
        <p>This is a test, this font should have font-size of 14px.</p> 
        <p>This is a test, this font should have font-size of 14px.</p> 
    </body>
</html>

Remember, you might need to hit run once or twice in Chrome to see said effect.

Does anybody know what is causing this or if there's a way around it? Am I committing a crime by setting a 62.5% font-size on the html element (I realise there are arguements against doing so)?

like image 807
Sean Avatar asked Nov 20 '13 15:11

Sean


2 Answers

The easiest solution that I have found is to simply change the body definition to

body {
    font-size: 1.4em;
}

Because it is the body, you don't have to worry about compounding – just use rems everywhere else.

like image 171
randak Avatar answered Sep 28 '22 00:09

randak


Try:

html { font-size: 62.5%; } /* font-size: 62.5% now means that 1.0 rem = 10px */
*{font-size: 1.4rem;line-height: 1.6rem; }
body { background: #fff; font-family: arial;  }

Seems to look better on refreshing the page :)

FIDDLE

like image 40
SW4 Avatar answered Sep 28 '22 01:09

SW4