Over a million developers have joined DZone.

Solve Common Sizing Problems with CSS3's New rem Unit

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

CSS promised to keep layouts well-proportioned, ensuring consistent measurement by including its own units.

Besides pixels, the most commonly used CSS unit is probably the em, which is simply the computed font-size of the current element. Using this font-relative unit, designers can keep the relation between text and non-text constant -- crucial for pleasing and intelligible design.

The em proved a bit unexpectedly annoying in more complex layouts, because the em is calculated relative to font-size of the current element -- whose font-size may already have been calculate relative to another element. As Jonathan Snook explains, in a popular post:


The problem with em-based font sizing is that the font size compounds. A list within a list isn't 14px, it's 20px. Go another level deeper and it's 27px! These issues can be worked around by declaring any child elements to use 1em, avoiding the compounding effect.

The only way around this before CSS3 was to reset every child element's font-size to 1em, cutting off all compounding effects.

But that's obviously a bit of a hack, and consequently easy to forget about.

The solution? CSS3's rem (root element) unit -- measured relative to the root element, as the name suggests, not the current element.

How to do it? You can probably guess already, but if not, here's Jonathan's full explanation. (Works in all major browsers, too, now that Opera is in 11.60.)

If you've wrestled with ems before, chances are you'll find rem extremely useful at some point.


Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}