Em based layouts - Vertical rhythm calculator

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 CSSterms. 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 CSSnewcomer. 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 emmeasurement 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 orh1 or any standard rule or your own based on your markup. This name will be in the CSSand 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.
Em Calculator – Adobe AIR application
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. 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.
Posted in: journal

Leave a response, or a trackback from your website
28 Comments so far (rss)
Ivan M said:
A superb calculator. Many thanks
http://www.visual-blast.com/online-tools/em-based-layouts-vertical-rhythm-calculator/
on January 21st, 2008 at 5:32 pm
Neil said:
Thanks, James. That
on January 27th, 2008 at 8:39 am
admin said:
Thanks for the great response! I
on January 27th, 2008 at 5:16 pm
Rob said:
James,
You need to fix your site. In a smaller browser window, the right side goes goofy.
on January 27th, 2008 at 11:24 pm
Dee said:
.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.
on January 28th, 2008 at 4:19 am
admin said:
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
on January 28th, 2008 at 10:19 am
Dee said:
Hi James, awesome tool.
Unfortunately I tried to download the air app with every available Os X browser and the link just doesn
on January 28th, 2008 at 10:36 am
rutinerad said:
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
on January 28th, 2008 at 12:31 pm
Matthias said:
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
on January 28th, 2008 at 8:40 pm
admin said:
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
on January 28th, 2008 at 9:16 pm
David Dorward said:
The default font size is whatever the user picks. If they don
on January 29th, 2008 at 11:42 am
admin said:
David,
Yes I should have explained what I meant by
on January 29th, 2008 at 12:46 pm
David Dorward said:
I still can
on January 29th, 2008 at 1:08 pm
Will Kelly said:
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
on January 29th, 2008 at 5:32 pm
Jean Alexandre said:
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.
on January 29th, 2008 at 8:15 pm
admin said:
Jean,
I
on January 29th, 2008 at 9:31 pm
Jean Alexandre said:
I haven
on January 29th, 2008 at 10:15 pm
Jean Alexandre said:
Oh man, I
on January 29th, 2008 at 10:33 pm
Mathminded said:
Great app! Thanks for taking the time to create this. I
on February 3rd, 2008 at 6:49 pm
Gemma Weirs said:
Hi,
Thanks for a very useful article and the tool. I
on February 3rd, 2008 at 11:57 pm
admin said:
Mathminded - You
on February 4th, 2008 at 10:08 am
Mathminded said:
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
on February 4th, 2008 at 4:56 pm
Stephanie Sullivan said:
My answer to
on February 5th, 2008 at 6:48 pm
admin said:
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!
on February 6th, 2008 at 12:43 pm
Dave Buchholz said:
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
on February 6th, 2008 at 2:25 pm
admin said:
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
on February 6th, 2008 at 4:41 pm
Stephanie Sullivan said:
Thanks James. Evidently, my original post to your comments didn
on February 7th, 2008 at 2:33 am
admin said:
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
on February 7th, 2008 at 12:42 pm