Be careful with CSS em units when taking advantage of rules of specificity?

Go To StackoverFlow.com

5

I'm having trouble writing maintainable CSS code when font size is specified with EM units as opposed to PX units. I've been accustomed to writing CSS code like this:

body {font-size: 12px;}
body .sidebar {font-size:11px;}
body .sidebar .loadmore {font-size:10px;}
body .sidebar .warning {font-size:13px;}

The idea is that on many pages through out the site, there's a lot of text that should have 12 pixel font-size. I take advantage of the rules of specificity to override the 12px font size in special areas of the site.

Let's say I rewrote the above code as:

body {font-size: 12em;}
body .sidebar {font-size:11em;}
body .sidebar .loadmore {font-size:10em;}
body .sidebar .warning {font-size:13em;}

If I replaced px with em in the code above, my understanding is that I lose the advantage of rules of specificity. Line 3 of the code would be interpreted as " 10 em of 11 em of 12 em" which is not at all the same meaning as " override all previous rules and use 10 em of (what is the default?)". Is what I've stated correct?

edit If what I've said is correct, then how does one write a font size rule such as "use font size X for all elements, but use font size Y on side bars"?

2012-04-03 21:51
by John
See my example http://jsfiddle.net/PJWrW/. For the nested elements, use values that multiply to the desired size - for example, 1.1em or 110% - jnylen 2012-04-03 22:02
@John Edits made to my original reply to reflect your own reply, but in short. jnylen is pointing you down the right track with his/her use of the scaling factors - JDD 2012-04-03 22:36


6

John the specificity that you are talking about will occur in the way that you have stated. A reference as to why can be seen: http://kyleschaeffer.com/best-practices/css-font-size-em-vs-px-vs-pt-vs/.

Edit As a reply to your edit please consider what jnylen posted in the comments of your original post.

A font size of 12 em as you have listed for body {font-size: 12em;} is going to scale the default font size to 12x it's current size. If you would like to use em's you need to consider at what scale rate you wish to use them and do the math. If you want to set fixed sizes with nested statements you need to stick to fixed attributes (pixels). The advantage of em's as stated in the article that I linked is that you can set a default size, say 12 px and then use em to scale them. For example in mobile based websites.

2012-04-03 21:58
by JDD
Good article. (15chars - jnylen 2012-04-03 22:01
Thank you I was going to leave a comment on your as well but I am quite new and apparently do not have full capabilities yet :) - JDD 2012-04-03 22:02


2

Yes, em values "multiply" together when the elements they apply to are nested. It's not necessarily a specificity issue - if you specified the rules individually for .loadmore and .sidebar you would see the same issue, since .sidebar contains .loadmore.

Here's an example of a way to work with this: http://jsfiddle.net/PJWrW/

I usually use either px or percentage units for font sizes, to make it explicit that I'm setting an absolute font size or modifying the parent font size.

I sometimes use em units for defining dimensions like paddings and widths, since an em unit is basically the width of a letter at the current font size.

2012-04-03 21:57
by jnylen


0

As far as I know, if you're using em's for font size, there is no one way to set a standard font size for elements in a left hand side bar (or elsewhere for that matter) whilst setting the specific size of some elements in that side bar. If you use em's you'll have to specify the sizes of all elements in that side bar.

Personally I usually use px for font sizing, simply because you can set a standard font size for certain elements as you say in your post.

The only convincing arguments I have heard for using em over px regard scaling issues that are no longer really a problem (IE6 doesn't allow you to change font size manually if you use px but most people stopped hacking for IE6 a while back) The only real situation I can see using em as an advantage is if you want to implement functionality whereby the user can change the font size via JavaScript like here

2012-04-03 22:40
by Sean
Ads