Are there any practical reasons to use em instead of pt font size units?
Depending on the country where you live, you might actually end up breaking the law using pt instead of em, depending on how hard your legislature want to enforce rules. Here in the UK, there is a disability discrimination act, which has been used to target companies where their websites have been rendered in a fixed font. This is treated as discrimination because it disadvantages the partially sited who may have increased their browser font sizes to compensate - but your site still renders fonts at the size you set, and not at the size they would expect.
Yes, it's harder to get to grips with relative font-sizes and fluid layouts, but if you want to comply with legislation, you have to take the time to get to grips with this.
For local government work in the UK, targets have been set to ensure that websites follow Double A guidelines, one of which states "Use relative rather than absolute units in markup language attribute values and style sheet property values". See here.
Why em instead of px?
The reason I asked this question was that I forgot how to use em's as it was a while I was hacking happily in CSS. People didn't notice that I kept the question general as I wasn't talking about sizing fonts per se. I was more interested in how to define styles on any given block element on the page.
As Henrik Paul and others pointed out em is proportional to the font-size used in the element. It's a common practice to define sizes on block elements in px, however, sizing up fonts in browsers usually breaks this design. Resizing fonts is commonly done with the shortcut keys Ctrl++ or Ctrl+-. So a good practice is to use em's instead.
Using px to define the width
Here is an illustrating example. Say we have a div-tag that we want to turn into a stylish date box, we may have HTML-code that looks like this:
<div class="date-box">
<p class="month">July</p>
<p class="day">4</p>
</div>
A simple implementation would defining the width of the date-box
class in px:
* { margin: 0; padding: 0; }
p.month { font-size: 10pt; }
p.day { font-size: 24pt; font-weight: bold; }
div.date-box {
background-color: #DD2222;
font-family: Arial, sans-serif;
color: white;
width: 50px;
}
The problem
However, if we want to size the text up in our browser the design will break. The text will also bleed outside the box which is almost the same what happens with SO's design as flodin points out. This is because the box will remain the same size in width as it is locked to 50px
.
Using em instead
A smarter way is to define the width in ems instead:
div.date-box {
background-color: #DD2222;
font-family: Arial, sans-serif;
color: white;
width: 2.5em;
}
* { margin: 0; padding: 0; font-size: 10pt; }
// Initial width of date-box = 10 pt x 2.5 em = 25 pt
// Will also work if you used px instead of pt
That way you have a fluid design on the date-box, i.e. the box will size up together with the text in proportion to the font-size defined for the date-box. In this example, the font-size is defined in *
as 10pt and will size up 2.5 times to that font size. So when you're sizing the fonts in the browser, the box will have 2.5 times the size of that font-size.
Is sizing fonts using em still relevant?
Do not specify the font-size in absolute length units for screen stylesheets. They render inconsistently across platforms and can't be resized by the User Agent (e.g browser). Keep the usage of such units for styling on media with fixed and known physical properties (e.g print)
If you will use this method, no need to calculate
You can set the font-size of the body to 62.5%(that is 62.5% of the default of 16px), which equates to 10px, or 0.625EMs. Now you can set your font-size in EMs with an easy to remember conversion, divide the px by 10.
* 12px = 1.2EMs
* 13px = 1.3EMs
* 16px = 1.6EMs
* 8px = 0.8EMs
* etc…
This makes everything SUPER easy to remember and eliminates the need for conversion tables. Of course, you will still need to use a conversion table for nested elements when using EMs, if you are not being specific in your CSS, which is a whole separate issue.
But 76% is much better and you can use this to calculate http://pxtoem.com/
Yes it's still relevant:
IE6 is still widely used and is unable to resize the fonts defined in px. => Usability issues. That alone is a no-no.
and
IE 7 and 8 don't resize text sized with pixels either, actually. They do have page zoom, but some people prefer to incease text size only.
Here's a summary of what's good and bad about font sizing in general.
Font size in css http://easycaptures.com/fs/uploaded/213/2470522253.png
I personally like ems. Others, like Chris Coyier over at CSS-Tricks.com likes pixels. (Chris has an excellent article on the different font units).
It really comes down to personal preference.
Almost similar or related questions on SO
Should we still use em and % for defining the font-size of the website elements?
Is there really any point to using relative font sizing in CSS?
Why em instead of px?
Font size in CSS - % or em?
CSS font size: relative vs. absolute values. Which to use?
Problem with EM
Using relative instead of fixed size in CSS
Helpful online tool for px to em
http://pxtoem.com/
http://riddle.pl/emcalc/
http://convert-to.com/pixels-px-to-em-conversion.html
Convert entire site from px to em (This tool is still in development)
http://converter.elementagency.com/
EM Calculator AIR application (will work on all os)
http://jameswhittaker.com/journal/em-based-layouts-vertical-rhythm-calculator/
http://jameswhittaker.com/projects/apps/em-calculator-air-application/
Windows apps
http://www.thebrightlines.com/2009/11/16/pixem-pixel-to-em-converter/
http://www.storkas.com/Projects.aspx(go at bottom)
Pixels to Ems Conversion Table for CSS
http://jontangerine.com/silo/css/pixels-to-ems/
http://reeddesign.co.uk/test/points-pixels.html
emchart
http://aloestudios.com/tools/emchart/
http://aloestudios.com/code/emchart/
Some more articles on this issue
http://www.d.umn.edu/itss/support/Training/Online/webdesign/type.html
Font size in CSS - % or em?
There's a really good article on web typography on A List Apart.
Their conclusion:
Sizing text and line-height in ems,
with a percentage specified on the
body (and an optional caveat for
Safari 2), was shown to provide
accurate, resizable text across all
browsers in common use today. This is
a technique you can put in your kit
bag and use as a best practice for
sizing text in CSS that satisfies both
designers and readers.
Should I define CSS margins in pixels or ems? Why? When?
em
units are used for better scalability of the page when the size of the elements depend on the page's scale. It's especially important for old browsers (e.g. IE6) and mobile platforms.
px
units are used for absolute values, while em
is relative to the font size of the particular element.1em
means one font-line, e.g. you have a box with font-size 12px
that means that 1em will be equal to 12px
Also, using px
seems easier because you know the exact value, but em
units inherit the value of their container.
<p>Text</p>
<div class="box">
<p>Lorem</p>
</div>
p {
font-size: 1.2em;
}
.box {
font-size: 1.2em;
}
In this case, the first <p>
will have font-size equal to the basic font-size * 1.2, and the second <p>
will display with font-size * 1.2 * 1.2.
How font-size em value work?
Using em as measurement value would just not inhert but increases accordingly.
Here's an example:
<div>
<p>
<span>foo bar</span>
</p>
</div>
body{
font-size: 1em;
}
div{
font-size: 1.5em;/*1.5em of 1em == 1.5em*/
}
div > p{
font-size: 1.5em;/*1.5em of 1.5em inherited from div == 2.25em*/
}
div > p > span{
font-size: 1.5em; /*1.5em of 2.25em inherited from p == 3.375em*/
}
So, I would recommend you to use px as measurement value.
Use ems to make scalable style sheets only.
If you want to know more about px
, em
, and %
please follow this link and this link
What is the difference between px, em and ex?
- Pixels (
px
) are browser dependent. It is the absolute size that you would see on your screen. Em
are sort of like percentages.Em
s is referring to the base text size. The value of1 em
means the same thing as a value of100 percent
. But you can also say it in the opposite way: A percentage value is just anem
multiplied by 100.- Points(
pt
) are what you would want to use in print media.
Is there really any point to using relative font sizing in CSS?
According to YUI Font CSS,
Always use percentages as the units
because they render more consistently
than ems, and because they allow
user-initiated resizing (unlike
pixels).
Relative font sizes work pretty well when they are part of a framework like YUI. Especially because they normalize how fonts work across browsers.
Personally, I do throw in absolute px every once in a while, but typically only for text that must somehow match up size-wise with some other design elements (like a menu).
The % stuff does break down when you assign % to a certain element and then a different % to a contained element. But that's the only real gotcha I've found.
Related Topics
Use Calc() Function in Makestyles
Continuously Adjust Element Size as Screen Size Changes
How to Override a Less Mixin Variable Based on a Parent's Variable
Offset Border Effect in Pure CSS
Firefox 1 Pixel Bug with Border-Collapse, Workaround
Html5 Vertical Spacing Issue with <Img>
Disable Zoom on a Div, But Allow Zoom on the Page (An Alternate Div)
Make Outer Div Be Automatically the Same Height as Its Floating Content
Css: How to Get Scrollbars for Div Inside Container of Fixed Height
Retina Displays, High-Res Background Images
How to Remove the Blue Highlight of Button on Mobile