
June 24, 2011
January 24, 2011
December 19, 2010
June 2, 2010
April 22, 2010
April 2, 2010
April 1, 2010
March 30, 2010
February 12, 2010
February 4, 2010
January 29, 2010
January 29, 2010
How do I figure out a font-size in pixels if I use em units in my CSS document?
As a designer, it’s easier for me to think of font-size and line-height in terms of pixels. But as a responsible Web Designer, I know it’s a best practice to use em units.
I think of it this way:
em = relative = flexible = good
pixel = absolute = not flexible = bad
The definitions of em units and pixel units in terms of CSS:
How do I know what the font-size is of the relative font? I need to know that without any styles the default font-size for a standard font is 16 pixels.
So, by default, 1 em = 16 pixels.
By default 1 em is equal to 16 pixels for standard fonts.
The formula for calculating pixels into ems:
[desired pixel size] ÷ [default pixel size of the element’s containing block] = [size in ems]
Example: 24px ÷ 16px = 1.5em
The formula for calculating ems into pixels:
[em unit] × [default pixel size of the element’s containing block] = [size in pixels]
Example: 1.5em × 16px = 24px
I always start by declaring the font-size on the body tag to be 100%. This seems to keep the font-size from going wildly out of control when the text size is increased or decreased.
Simply, in an external style sheet:
body {font-size: 100%;}I adjust the default font-size in my page’s main content containing block, in this case, #container, because I think 16 pixels is too big for body copy set in Arial.
Instead, I’ll set it to 12px.
#container {font: 0.75em/1.5em arial, helvetica, sans-serif; /* 12px/18px */}Assuming I have <div id="container"></div> as my containing block, the font-size for any element I add inside this div will now be 12px/18px.
I arrive at that value for font-size using the first formula:
12px ÷ 16px (the default) = 0.75em
You can also think of it this way: 12 is 75% of 16.
I arrive at that value for line-height using the second formula:
1.5em × 12px (now default) = 18px
To calculate line-height, I have to take 1.5 × 12px (the new font-size of my containing block), instead of 1.5em × 16px (the original default) to figure out the pixel units.
I usually want my headings to stand out a little more, so I like to increase the font-size for those too.
h1 {font: 2em/1.5em arial, helvetica, sans-serif; /* 24px/36px */}h2 {font: 1.5em/1.5em arial, helvetica, sans-serif; /* 18px/27px */}h3 {font: 1.333em/1.5em arial, helvetica, sans-serif; /* 16px/24px */}h4 {font: 1.166em/1.5em arial, helvetica, sans-serif; /* 14px/21px */}h5 {font: 1em/1.5em arial, helvetica, sans-serif; /* 12px/18px */}h6 {font: 1em/1.5em arial, helvetica, sans-serif; /* 12px/18px */}The advantage to this approach is that you won’t have to worry about setting the font-sizes for lists, paragraphs or any other elements as long as you want those to be the new default size you set, in the above case, 12px.
For cases where you want to set the font-size above or below 12px, you’ll have to set up some additional classes or ids.
#side-container p {font: 0.9167em/1.5em arial, helvetica, sans-serif; /* 11px/16px */}11px ÷ 12px[default] = 0.9167em
1.5em × 11px = 16px
Because relative unit font-sizes are inherited, I try not to override them more than twice. It’s a pain to calculate a % of a %.
I noticed, and after some searches I found out many others have noticed too, that Safari’s default pixel size for monospace fonts is 13px, not 16px. To solve the problem, don’t include the keyword “mono or monospace” in your font declaration. For some reason, Safari will now treat the monospace font specified as normal.
ol.code-block li code {font: 11px/14px "Monaco", "Courier";}