skip to navigation or skip to content

James Whittaker - Interactive developer, designer and consultant


Hello, it's nice to see you. While you’re here you can read the blog find out about me, or just get in contact. Eventually there will be reviews, a 'labs' area for interactive goodness and much more. It's early days and i'm still tweaking the code and style so please hang on in there!

Yes it's me James

Em based layouts - Vertical rhythm calculator

comments closed posted on 18 January 2008

Typography on the web has become a point of focus for standards based developers recently. The pioneering work by Mark Boulton and Richard Rutter has highlighted the importance of good web typography. With the advent of more standards compliant browsers and a better understanding of the capabilities of CSS, more developers are spending the extra time and effort in pursuit of typography excellence within their designs.

For years we have touted the phrase ‘the web is not print’ and consequently have forgotten about the basics of designing for type, like working to a baseline rhythm. I have come across many a design where the developer has not thought about typography before diving straight into the code. The result is a mess of random amounts of padding and margin married with unnecessary style rules, this in turn leads to CSS code that is harder to understand and maintain.

One of the key concepts that we can use within our sites is consistent line-height in CSS terms. If you think of a ruled notebook, the lines are all equal heights.

The concept is fairly straight forward but in practice can be confusing especially for a CSS newcomer. All sizing needs to be relative to the base size. This allows for easy scaling up or down while keeping the required spacing and layout consistent, this can be refereed to as an elastic layout. To allow this we need to specify measurements in percent or em.

What is an em measurement?

A pixel is the ‘dot’ on your computer screen. Displays are measured in pixels across and down like 1024 × 768. These dots are unscalable and fixed in size. In some situations this is desirable and it would be better to set your measurements as pixels.

An em is a relative measurement based on the square of the font size at that particular point. As font sizes can vary across differing systems and displays, the result is that the em measurement will always be relatively sized and scalable.

As we are now developing for a wider range of devices and screen sizes we should look to adopt em instead of pixels in all of our designs. Let’s take a look at how.

The browser default size

Modern browsers ( IE7, Opera, Safari and Mozilla ) have a built in default font size of 16px so we can use that as the cornerstone for all of our sizing calculations from now on. At this point we need to make a choice as to the base font-size we are going to work with, nominally this will be 12px or 14px which we can set as a percentage of the 16px browser default. An example for 12px required size would be:

*/ 12 / 16 * 100 = 75% /*

As usual it’s not always that simple and Safari 2 does apply a 13px base size to fixed width fonts. To overcome this situation you will need to tell Safari to use a base size as a set pixel value, like 16px or 12px. Beware though that you must use conditional comments to block that statement from IE. For IE’s font scaling to work correctly it needs to be given a relative base size set in percent or em. For more guidance I refer you to Richard’s article on A List Apart titled – how to size text in CSS.

Working out a required font-size

Well that’s why I built the calculator! to save you the hassle of all of those calculations. I’m going to cheat here and direct you to a great article on 24ways titled – composing to a vertical rhythm. This is the background to the calculations behind my calculator.

Using the calculator

The calculator defaults to a required base font-size of 12px and a 21px line-height. If you have set yours to be any different then please change this now. Some people prefer a line-height to be 1.5 x the base font-size, I personally prefer a larger 1.75 x the font size. It give more breathing space I feel.

Now you have set the base sizes you need to start adding some style rules, so click the button and the interface will expand down. Now give the style a name, this could be p or h1 or any standard rule or your own based on your markup. This name will be in the CSS and can be altered later.

The parent rule is there for creating cascading calculations. Font-sizes in em’s are always based of the parent font-size. So if you need it it’s there if not then leave it set to document base.

Next type your required font-size in pixels and push the calculate button. Your CSS will be there ready to copy to the clipboard and the options to add padding or margins that will live update in the CSS code. BEWARE that if you change your font-size you will need to click the recalculate button as all the other measurements are based off the font-size.

The margins and padding are in line height units, refer to the 24ways article for more understanding or send me an email if your stuck.

The final thing is that you have a simple em calculator for working out pixel to em values based upon the current set font-size. You could use this for working out the width of an element in em’s. Where your design is say 120px you could get that value in em’s. Just enter the pixel value in the box and it will auto calculate.

The calculator

Sorry, you don't appear to have the required Flash Player plugin to view this content. This might be because you are viewing this site on a mobile device that does not support Flash or that your installed player might be out of date. If you would like to get the latest Flash Player for your system then please click here.

If you are still having problems then check that you have JavaScript enabled on your browser.

Em Calculator – now for your desktop!

I have now produced the calculator as an Adobe AIR application so that you can run it on your desktop. To run the application you will need the latest Adobe AIR runtime (version 1.0). It will then install and off you go. This version is just the same as the online version shown above. I’m still working on an AIR app with the integrated browser.

Sorry, you don't appear to have the required Flash Player plugin to view this content. This might be because you are viewing this site on a mobile device that does not support Flash or that your installed player might be out of date. If you would like to get the latest Flash Player for your system then please click here.

If you are still having problems then check that you have JavaScript enabled on your browser.

Contributions

If you like the EM Calculator and would like to make a contribution to it’s development please use the button below thanks.

Feedback and questions

This site has been built using an elastic layout and em sizing so feel free to have a look at the CSS, usual thing don’t steal it all but look, learn or challenge it.

If you find this useful or have any feedback please let me know. It is a beta version there could be a few little gremlins hanging around which I will address in future versions.

I am working on an Adobe AIR version that has an integrated browser for instant visual feedback so check back for that soon.

posted in: CSS  | Flex  | Tools 

The comments

No.1 Ivan M.  commented Mon Jan 21, 2008 at 5.32 pm

A superb calculator. Many thanks smile
http://www.visual-blast.com/online-tools/em-based-layouts-vertical-rhythm-calculator/

No.2 Angelo  commented Mon Jan 21, 2008 at 9.17 pm

I’ve been meaning to make a web interface to do almost what this calculator does. Thanks for this; I think you’ve inspired me.

No.3 John  commented Sat Jan 26, 2008 at 1.38 pm

Wow, amazing article.
I`ll be linking to this in my blog.

I`v always stayed away from em`s not wanting to spend the time to calculate the right sizes but this tool and explanation are great!

Keep up the amazing work.

No.4 Neil  commented Sun Jan 27, 2008 at 8.39 am

Thanks, James. That’s a handy piece of kit for the tool box, and a comprehensive write-up, too. Did you know it was covered by WebAppers.com?

A widget for OS X dashboard widget would be ace - I’m sure someone would be willing to do it for you when the AIR version is done.

No.5 jmwhittaker  commented Sun Jan 27, 2008 at 5.16 pm

Thanks for the great response! I’m surprised by the interest especially being picked up by Smashing Magazine and Boagworld to name a few.

Neil I was unaware of it being posted up on WebAppers.com so that’s a bonus too! I’ll look into the widget for OSX. Having never done one I’m interested to see what is involved but first the AIR verions needs finishing!

Also I seem to have an issue with the transparent SWF on OSX browsers, you can still see the background. It’s fine on a PC, I guess it’s something that I have or have not set in SWFObject. I’ll look into it.

No.6 Rob  commented Sun Jan 27, 2008 at 11.24 pm

James,
You need to fix your site.  In a smaller browser window, the right side goes goofy.

No.7 Dee  commented Mon Jan 28, 2008 at 4.19 am

Hi James, awesome tool.
Unfortunately I tried to download the air app with every available Os X browser and the link just doesn’t do it for me. It just opens (or saves, if right clicked) a zero byte .html page. Maybe .zip it?

No.8 jmwhittaker  commented Mon Jan 28, 2008 at 10.19 am

Dee,
Hmmm seems that Textdrive (Joyent) has not applied the new MIME Type for the .AIR file extension. AIR files need to have the MIME type of: application/vnd.adobe.air-application-installer-package+zip .air Not sure what is going on there I have filed a ticket so in the mean time I will upload it as a ZIP file hopefully that will work!

Rob,
Well spotted! I seem to have some rogue CSS somewhere. Leave it with me and I’ll get it fixed. Must admit that I have been spending more time on getting some mobile style sheets up and running than seeing the standard CSS on a small browser. Note to self - must test everything!

No.9 Dee  commented Mon Jan 28, 2008 at 10.36 am

.zip worked, got it. Useful indeed, made my day.
Wish-list for future development: being able to create basic layout elements the same way you do with type elements, and have them included in the css.

No.10 rutinerad  commented Mon Jan 28, 2008 at 12.31 pm

Nice article. I will test this the next time I produce a new layout.

By the way, the blog info at the top of the page should probably read “While you’re here” and not “While your here”.

No.11 Matthias  commented Mon Jan 28, 2008 at 8.40 pm

so the idea about 75% font-size is as old as Em based layouts are actual. but there are many reasons to not use this relation, because of roundingerrors and paternalism of the user. not everyone uses the 16px as the default font-size, some people like other smaller or bigger font-sizes. so if someone uses 12px you force him to have only a 9px wide font-size.

so why that?

use 100.01% (.01 for safari) and the user has always the experience he or she expected.

i really don´t know what is so difficult in using a caculator for development… so it´s a lot better for the free userexperience.

No.12 jmwhittaker  commented Mon Jan 28, 2008 at 9.16 pm

Matthias,

This is by no means the only way to size text. The 75% font size is just a guide and what I used on this site so that all major browsers render very closely to 12px. You are correct in saying that rounding error can be present this is why the calc goes to six decimal places. However I believe the margin is very small and in my tests only Safari was slightly picky about the decimal places.

I’m not sure what you mean by saying that not everyone uses a 16px default size, this is the default text size of the browser, you can then choose what size you wish your base to be (as in my example 12px hence 75%).

I personally have found this approach to suit my workflow and I get consistent sizing across all of the main browsers (IE 6 aside).

No.13 David Dorward  commented Tue Jan 29, 2008 at 11.42 am

The default font size is whatever the user picks. If they don’t pick anything, then it goes to the vendor default of 16px (chosen because it strikes a good balance between readability and scrolling).

The 75% is not 75% of 16px, it is 75% of the user’s preferred font size.

No.14 jmwhittaker  commented Tue Jan 29, 2008 at 12.46 pm

David,

Yes I should have explained what I meant by ‘default’ - without the user overriding the size within their browser. Thanks for the clarity. J

No.15 David Dorward  commented Tue Jan 29, 2008 at 1.08 pm

I still can’t help but think that setting the font size to 75% of the user’s preferred font size is a little silly.

No.16 Will Kelly  commented Tue Jan 29, 2008 at 5.32 pm

David taking your point of view to an extreme, it would also be silly to change the users preferred font colour and face, not to mention using cascading style sheets at all!

As its unknown whether or not a user has adjusted their ‘default’ font size, using percentages is a good compromise between setting the font size you desire, while still allowing the user to modify to their wishes.

No.17 Jean Alexandre  commented Tue Jan 29, 2008 at 7.39 pm

I can’t seem to run the air version of this app.  I downloaded the beta 3 and I’m running Leopard.  Any thoughts?

No.19 jmwhittaker  commented Tue Jan 29, 2008 at 9.31 pm

Jean,

I’m on OSX Tiger and have no issues, I do know someone who is running the app on Leopard without problem. I have re uploaded the file just to make sure.

I would guess it has something to do with your beta 3 AIR runtime installation. Did you install it over a previous version? Do other beta 3 AIR apps run ok?

No.20 Jean Alexandre  commented Tue Jan 29, 2008 at 10.15 pm

I haven’t tried any other air apps so this installation is my first.  The file has a .air extenstion but when I double click it it opens up waveburner which is part of Logic 8.  I’ll try downloading an air sample app and see what happens.

No.21 Jean Alexandre  commented Tue Jan 29, 2008 at 10.33 pm

Oh man, I’m a moron.  I forgot you have to actually install the air apps.  I remembered my boss telling me about that.  I got it up and running now.  Can’t wait to use it on my expression engine template I’m building.  Haven’t really got too deep with EE but so far it’s pretty sick.

Thanks again,

No.26 Mathminded  commented Sun Feb 3, 2008 at 6.49 pm

Great app! Thanks for taking the time to create this. I’m in the process of converting a site to be completely scalable (including the images) and this will come in very handy!

Quick question: I set the “document base values” to 12px and 12px, and created an element with a “required font size” of 12px. I then used the “Px to Em sizing calculator” to find the Em size for 12px. It said the Em size should be 0.999996em. Why didn’t it give 1em? Is this just a round-off problem within the Adobe app or am I misunderstanding something?

No.27 Gemma Weirs  commented Sun Feb 3, 2008 at 11.57 pm

Hi,

Thanks for a very useful article and the tool. I’ve added this to Technorati and Delicious, plus I’ve Stumbled and Furled it. smile

Cheers.

No.29 jmwhittaker  commented Mon Feb 4, 2008 at 10.08 am

Mathminded - You’re right it’s the rounding. To save people’s brain the cal limits it to 6 decimal places. So to work out the single pixel ratio you would obviously do 1 / 12 = 0.083333 recurring. Then * by 12 = would equal 1 but only 0.999996. In that case just round up if you like. I though that was better than having an infinite number of decimal places!

Gemma - Thanks for the comment I’m glad its useful thanks for the extra promo!

No.30 Mathminded  commented Mon Feb 4, 2008 at 4.56 pm

I just came across a major snafu in making my entire design scalable using ems. I have to have a background image on one of the divs, and I can size all of the images using ems _except_ this background image. I sure hope I’m missing something because this is the only thing that will keep me from having a completely scalable website. Yikes!

No.32 Stephanie Sullivan  commented Tue Feb 5, 2008 at 6.48 pm

My answer to “why scale fonts at all” for Matthias and David Dorward is posted to my blog:
http://communitymx.com/blog/index.cfm?newsid=901

No.36 jmwhittaker  commented Wed Feb 6, 2008 at 12.43 pm

Stephanie,

Good article and opinion on how em based layout and typography actually provides visually impaired users a better more controlled experience. Over the coming year we are hopefully going to see more designers and developers get to grips with ems and start pushing the boundaries further. Great stuff!

No.39 Dave Buchholz  commented Wed Feb 6, 2008 at 2.25 pm

Would it be possible to provide a dummies guide on how to install this, I have installed Adobe Air, downloaded the zip file from your site, opened the Adobe Air Application Installer but I cannot find a file that the installer recognises ?

I am using OSX Tiger.

Thanks

No.42 jmwhittaker  commented Wed Feb 6, 2008 at 4.41 pm

Dave,

Firstly you will need AIR beta 3 from Adobe labs. Install the runtime. Download and extract my ZIP file to your desktop. There should be a file with a .AIR extension. Just double click the.AIR file to install.

It was developed and tested on Tiger so there should be no problems their. If you have a previous beta installation of AIR then you might need to remove that first. I use AppZapper for removing any installed progs on my Mac.

Apart from that i’m not sure. Can you install and run any other beta 3 AIR apps?

No.44 Stephanie Sullivan  commented Thu Feb 7, 2008 at 2.33 am

Thanks James. smile Evidently, my original post to your comments didn’t come through (I didn’t receive the same page after posting the first one that I did after posting the one that showed up and my personal info wasn’t saved - don’t know what happened). In that comment I said “Great calculator!” to you. Keep up the good work…

I also wanted to repost the answer to Mathminded I had in that post regarding the background image.

Mathminded, obviously, you can’t scale a background image as you found. However, there are creative solutions (which I obviously can’t advise without seeing your page). They include:

Blending the image on one or both edges and adding a background color…

Creating the image so it can repeat…

Creating the image so that at default font sizes, it appears complete, yet at larger font sizes more of the image is displayed.

Depending on your layout, and I’ve not experimented with this, you might pull off some sort of z-index stacking whereby you put the image into a div as an actual image--scale the div and the image in em units and put the other content in a div on top of it. But I have no idea if that would work in your case.

Be creative and don’t give up! smile

No.45 jmwhittaker  commented Thu Feb 7, 2008 at 12.42 pm

Thanks Stephanie. I have had to start moderating comments until I can find a better solution to stopping spam coming through. Being new to Expression Engine I’m not sure what is about so I’ll have to look into it. The form should remember your details. I’ll investigate.

J

Leave your comment

Commenting is not available in this section entry.

return to article list


About the author

Hello, i'm , an Interactive designer, developer and consultant located in Worcester, England. more about me...

About this site

jameswhittaker.com is a weblog covering many aspects of interface development on the web. I'll be showcasing some of my work alongside small experiments and general inspirational chit-chat. So stay with me on this journey, you never know what's around the corner! more about the site...

Search the blog

My Flickr snapshots

Syndication feeds

Recent blog entries

You can find a full archive of blog articles here

Blog categories

My latest Delicious links

More quality links from my del.icio.us here

Of interest elsewhere

Singularity?