Best unit for font-size in CSS

by

My brother Neal turned me onto this article on A List Apart

tl;dr

The best unit for defining font sizes in CSS are ems combined with font-sze:100%; in the body selector.

But working with ems can be a PITA. But if you’re using less css (or sass), it can be quite easy. 1 em = 16px so the conversion can happen in a parametric mixin.

 

// less
.font-size (@px: 16) {
    font-size: @px/16 em;
}
p {
    .font-size(18); // 1.125em
}

// sass
@mixin font-size ($px) {
    font-size: $px/16 em;
}
p {
    @include font-size(18); // 1.125em
}

PS, less rules, sass drools!

Tagged , ,
  • http://twitter.com/musicfuel James Newell

    Another pure CSS approach (by all means use SASS or LESS if you can) that has been used in recent years is to set font size to 62.5% on the body tag. Because most browsers use 16px as 100%, using 62.5% as the font-size  makes 1em equate to 10px. This makes conversion a bit easier being based on 10:

    10px = 1em
    11px = 1.1em
    12px = 1.2em
    13px = 1.3em

    20px = 2em

  • http://mimswright.com Mims H. Wright

    Very good addendum, James, thanks!

  • Pingback: My Thoughts on Reverie Framework by ThemeFortress