Filed under CSS, JS

Extra fluid layouts with javascript

I’m a big fan of vector graphics. Because of that I love Macromedia Fireworks and the way it handles all elements like vectors. The good thing about that is that they can be resized and zoomed without them losing their sharpness.

This got me thinking about how to add some vectorization to CSS layouts. My idea was that you should let the width of the screen decide the font-size of, for example, your headers. This could make for nice fluid layouts and might even add to usability in some cases. There are probably other uses for this but I leave that to you, please use the comments if you come up with something.

Start by having a look at the dynamic font-size example just so we’re talking about the same thing. Try resizing the window and reloading and see that the headers really do adapt to the width of the browser window.

How it’s done

First we need a way to meassure the width of the text in the default font. For this I use my own little getTextWidth() that were constructed in my line-break script. It simply takes a piece of text, attaches it to the page, meassures it’s width and removes it again.

function getTextWidth(text) {
   var ea = document.createElement("span");
   ea.innerHTML = text;
   document.body.appendChild(ea);
   var len = ea.offsetWidth;
   document.body.removeChild(ea);
   return len;
}

Ok, now we know the width of some text in it’s non formated state. Next up is to scale it as much as needed.

function scaleUp(elem, targetWidth) {
   var blockWidth = getTextWidth(elem.innerHTML);
   var defaultSize = parseInt(elem.style.fontSize || '100%')
   var newSize = Math.floor(0.9*targetWidth/(blockWidth/defaultSize))
   elem.style.fontSize = newSize + "%";
}

As you see scaleUp() determines a percentage by looking at three things: it’s unformated width, it’s current width and the target width. Font-size and text width does not seem to follow eachother perfectly so an additional multiplication by 0.9 is needed.

This handy little function is then called with the element you want to scale and the width in pixels of how wide you want it. The calls look something like this:

var element = document.getElementsByTagName("h1")[0];
var width = document.body.offsetWidth;
scaleUp(element, width);

That’s it, hope you find some fun use for it.

[Update: After doing some research I see that Eric Meyer is already using something like this in his slideshow system S5. Well well, it was a funny exercise none the less.]

Feel free to leave a comment, or subscribe to my feed.

related

List some related articles:

You might want to browse all articles.

linkback

These people have linked to this article:

  • No linkbacks yet

To get a link in this list: make sure your blogging software supports trackbacks or pingbacks and simply link to this article like this:

<a href="http://friendlybit.com/css/extra-fluid-layouts-with-javascript/">Extra fluid layouts with javascript</a>

You can also trackback by copying this link, and pasting it into a trackback field in your blogging tool.

about

Author: Emil Stenström

Emil Stenström blogs about web development. Posts are bi-weekly.

To the about section

comments

What do you think about this article? I’d love to hear your view!

Scroll to comment box.

  1. zapada 15 Feb

    01

    Nice, now what if a reload wasn’t required? That would certainly give it a nice effect.

  2. Pete 4 Mar

    02

    I remember seeing someone’s site that switched from 2 to 3 columns and back as the window was resized (using javascript) — that’d be a good place to look for this sort of thing.

    Wish I could remember which site it was though.

  3. Pete 4 Mar

    03

    Found some good links — this one is best for me so far:

    http://www.themaninblue.com/writing/perspective/2003/12/22/

  4. Emil Stenström 6 Mar

    04

    Pete: thanks, good links!

  5. Emil Stenström 6 Mar

    05

    zapada: Very few people actually resize the page, that’s one of the reasons I decided not to make the code even more complex by adding event handling. Keep it simple.

  6. Christian 9 Mar

    06

    One page with a nice fluid layout is http://www.uxmag.com/

  7. Joey 8 Nov

    07

    I’ve seen some other blogs on this topic and the idea of fluid layouts seem very practical today especially since screen sizes vary so much. More specifically, browsers display lots of wasted space on wide aspect ratio screens. Taking advantage of that extra display area is a great thing. Although, I still wonder if having a dynamic layout will confuse users who aren’t expecting it. You know, some web users hate change, especially change that occurs from one desktop computer to the next.

If you want to you can follow the disussion through a comment feed.

add comment

This is the place where you get to say your opinion about this article, use it well. Formating with HTML is allowed. A red asterisk means that the following field is required.

Comment Form




Comment Form