Going elastic with ems, layout techniques

September 7, 2006 | Published in: Web design & development | Tags: , , , , , 1

Image of a lensMost sites on the web are coded using pixels for widths, which is acceptable, however these sites make it harder for users with poor eyesight to increase the text size whilst keeping the layout in place and usable.

That is where ems can help. This guide will explain ems, and show you how to convert a layout to utilize ems.


Title changed, I made a mistake, liquid is for % layouts, ems is elastic. Sorry for the confusion, and thanks to Mike Cherim for pointing out my mistake.


What are ems?

An ‘em’ is a unit of measurement that is relative to the base font/font size. Basically, where pixels are fixed (a pixel is always the same width), an em’s width can vary, depending on the font size. This makes them very powerful, as by using ems you can essentially make the widths change depending on user preference.

Example sites

Visit the two sites listed below, and resize the text in either firefox or internet explorer (see browser support to learn how to do this).

Notice anything different between them?

BBC.co.uk has a static, pixel based layout, meaning when the text resizes the layout stays the same. As you can see, resizing too much breaks the layout and ruins the usability.

Mailsift on the other hand, was coded (by me) using ems, meaning as text resizes, the layout does too, keeping the text in pretty much the same place, avoiding the problems on the bbc site.

Other examples of sites that use ems include

5 reasons why ems can improve accessibility

  1. Internet explorer has trouble resizing pixel font-sizes
  2. Pixel layouts often break when increasing the font size.
  3. Visually impaired people can in effect ‘zoom in’ to your page.
  4. People with large monitors can make your site larger by increasing the size.
  5. People with low resolution can make the text smaller, to fit in their windows.

Browser support

Increasing the text size in the manner I have shown is supported primerily by Firefox and Internet explorer, Opera has its own resizing method (covered in Future).

To resize in firefox go to view > text size, or use the shortcut keys (control and + or -).

In IE6 go to view > text size, and select a text size.


How I convert a site to make it elastic

This is how I personally make a site elastic with ems. Before trying this please backup your existing css stylesheet, I don’t want your hate mail if you cock it up!

  1. Firstly, you need a layout done in CSS (see example 1). The example shows a basic page using pixels for font sizes and the actual layout. Nothing is resizable in IE6.
  2. Now to get your hands dirty. Add the following to the ‘body’ style.
    body {
             font-size:62.5%;
    }

    This is the base font size. I have chosen 62.5% as it is round about 10px, which will represent 1em. See example 2.

  3. Now you have a base font size you can make all other font sizes relative to this value. Example 3 shows how I have done this, paragraphs are set as 1.4em, headings are 2em. You can resize the text (in IE) in this example, but the layout is static.
  4. Next we need to work on the layout. Convert the px values to em, divide the value by 10 and add em after it. For example, the container div has a width of 716px, this should be converted to 71.6em, it should look roughly the same. See example 4.

And that’s basically it, look at example 4 and try resizing, the layout should change with the text.

Testing & what to watch out for

The main thing to watch out for when setting em widths is that due to inheritance in css, if you change a font size in a div, the width of the ems will need modifying to keep it the same. For example, If I set the font size the 1.4em in the container, the containers width itself would increase. You can avoid this by not changing the font size in divs with widths, or by dividing the width by the new font size. Example; original container width – 71.6em, divided by new font size (1.4em) makes it roughly 51em to keep it the same width.

Making images resize with ems

Images can also be resized with the layout, albeit tricky. To achieve this you shouldn’t set a width for the image in the html, you should set it in css. Look at example 4 again; notice the image stays the same size as you resize the text.
By removing the html width and height attribute, we can manipulate this image in css. Add a class of ‘image_size’ to the image in the html:

<img class="image_resize" src="lens.jpg" alt="Lens" />

Now in the css add the following:

.image_resize{
		 float:left;
		 margin-right:8px;
		 margin-bottom:4px;
		 width:12.5em;
		 }

Now the image will resize with the text (see Final example). *Note that using this method requires a different css style for each image if their sizes are not the same.

In the future

We are already seeing accessibility tweaks regarding resizing, notably in Opera and IE7. These browsers have a built in zoom function, meaning they can resize any page. These improvements mean that in the future we may not need to make our layouts and text sizes increase in the way described in this article, as the browsers will handle it. But for now, If you are keen on accessibility, you should make your site easy to read for the visually impaired, whatever their browser of choice.

Found this post useful? Why not buy me a coffee!

Related Entries

One Response to “Going elastic with ems, layout techniques”

RSS feed for comments on this post.

  1. Sreekanti says:

    Thanks a lot Mike. Great article! I was asked to fix a site where the text spilled out on zooming the text in IE and Firefox. I knew that the issue was with the CSS layout code, but did not know how to convert and which aspects to take care of. Your article explained crisp and clearly the steps to convert a site into elastic form and it worked beautifully for me.

    thanks again,
    Sreekanti

    Comment made on February 9, 2007 at 6:03 pm

The comments are closed.

About this site

Blue Anvil is the online web design journal & portfolio of , a web designer from Norfolk, England. Read More »
ThemeSlice
  • Featured work - More

    • Beefjack
    • Integrity
    • theotaku.com
  • Latest Tweet - More

    • @AzN102 If you mean the social link, you need to create a page called 'social' and give it the template called 'home'.
  • Out of the blue - More

    • MiniCard 1.1.7 Update

      I have just uploaded 1.1.7 of MiniCard here and to the WordPress theme directory. This updates includes:

      • New networks; xing, gowalla, yelp, foursquare, mobileme, google buzz
      • A way to change link text and define multiple links of the same network
      • A way to define your own custom links + icons
      • Improved admin panel

      Hope you like it, and don’t forget you can show your support by purchasing the premium pack from here.

    • Switched: From Shared to VPS

      It’s been about two weeks now since I made the transition from a shared reseller hosting account to a VPS (Virtual Private Server) account – impressions so far, excellent performance but fiddly to configure.

      The reason I wanted to change from shared hosting was the fact the server was always being hacked (even though ALL my scripts were secure), there was frequent downtime, support blamed me for problems every time, and it was slow as hell.

      Those used to a shared hosting environment would probably not know where to start when faced with configuring a VPS. Luckily, a lot of it was pre-configured when I received my account – certainly some of the major security holes were patched. I was not satisfied with those however. As a victim of hacking in the past (previous host swears it was not there fault, something I don’t believe) I took extra care to secure it as a much I could – configuring brute force detection, the firewall, installing mod security (excellent rules for that here: http://www.atomicorp.com/wiki/index.php/Atomic_ModSecurity_Rules) and going though multiple guides (like this one: http://www.webhostingtalk.com/showthread.php?t=468168) with a fine-tooth comb.

      The result? My pages are loading at least 6 times faster, I have had no down time (or at least have not noticed any), and I feel in control and happy. No longer am I at the mercy of shared hosts :)

      If your interested, I chose ServInt as my provider as they offered a great deal, as well as being a managed service (so I’m not on my own if I screw things up). I was tempted by the bells and whistles of Media Temple, but felt the ServInt service was better value.

    • Download Monitor 3.2.2 Maintenance Release

      Download Monitor has received some more love and has been updated. Here’s the change log from the new version:

      • Small bugfix in uploader.php – cat ID
      • Changed stats graph calculation – thanks lggemini
      • Changes to headers in download.php to avoid caching
      • File Browser fixes – $root was clashing with something….
      • exclude_cat works in all sections of download_page now
      • Removed hardcoding of /uploads/
      • Added action to download.php – should be able to use it to stop a download if you want – maybe limiting downloads per day or something? Whatever you want…
      • Made it so if you post new file on ‘edit’ screen, the post date is updated.
      • Fixed the ‘blank meta’ section which blanks out custom field values when nothing is set.
      • Moved ‘allow_url_fopen’ check.
      • Someone said downloads don’t work with spaces in the name. They do! Wasting my time sonny…
      • All work and no play make jolley a dull boy
      • Had to rename capabilities so they work. Apologies if you have to set this up again! Cheers to Mark Dingemanse.
      • {category_ID} custom format tag added. Useful if you want to send someone to its category on the DL page I guess. Also added {category_other} so when no category is set “other” is shown – this is because the download page can show an ‘other’ section if you want it to.
      • You can now manually edit the post date on the edit download screen.

      If you have edited capabilities for download monitor user permissions, you’ll have to again sorry! This is because I named them too long. Also, you should check your forced downloads still work because there was a logic error meaning they may not have been forced after-all…

      Enjoy.

    • Mahousive update to Download Monitor (3.2)

      Today I completed the update for the Wordpress Download Monitor Plugin – many tweaks, fixes, and features added. There were no changes to the database structure so people upgrading should be fine. Here is the list from the change log:

      • {user} tag added for custom formats
      • ‘autop’ option fix
      • Download page buttons applied with CSS so they are easier to customise/translate.
      • Fix for pagination bug after editing a download
      • Category output fix on edit downloads screen
      • Category urls on download page use ID rather than name to prevent errors when cats have the same names.
      • exclude_cat added to download_page shortcode
      • Localised ‘hits’ ‘date’ ‘title’ on download page
      • Option to disable the download logging
      • Read file ‘chunked’ some people found large files were corrupted so this should help (fingers crossed)
      • Added show_tags option to download page – displays x amount of tags on the download page.
      • File Browser root setting and download.php logic/mime types modified thanks to Jim Isaacs (jidd.jimisaacs.com)
      • Interface Improvements
      • Bulk edit categories, custom fields, tags, member only downloads
      • Added roles for download monitor admin – should be able to use with a role manager plugin if you want anyone other than admin to access the admin section e.g. http://wordpress.org/extend/plugins/capsman/
      • Change redirect after add
      • Edit Cat names/parents
      • Dedicated tags and thumbnails fields (they still use meta table though)

      And yes, those category link bugs are fixed at long last, and you can edit category names finally. Phew!