the Future of the Web
  • Home
  • Articles
  • Contact
  • Resizing a web layout based on browser size

    May 19 2007

    Some people thought that my new layout was too thin, and I had to agree. Originally, I wanted the width of the text on the page to be in a more narrow, more readable column. I also tried to stick to a layout that could fit within a browser on an 800x600 resolution. The result was a column of text that was less readable because it was too narrow.

    Today, I added a bit of JavaScript to the page to resize the layout for anyone with a browser wider than 930px. The JavaScript looks like this:

    var body_check = setInterval(function(){
        if (document.body) {
            clearTimeout(body_check);
    
            if (document.body.clientWidth > 930)
                document.body.className += ' wide';
        }
    }, 10);
    

    Every 10ms, this script checks if the body is available yet. As soon as it is, the checking is cancelled, and the 'wide' class is added to the body if the browser is wider than 930px.

    I opted for a polling technique instead of using window.onload, or even instead of addDOMLoadEvent, so the design wouldn't noticeably jump when the class was added.

    To go along with this JavaScript, I added the following in the CSS:

    #body { width: 760px; }
    #main h1 { width: 560px; }
    #main .section { width: 444px; }
    
    body.wide #body { width: 910px; }
    body.wide #main h1 { width: 710px; }
    body.wide #main .section { width: 594px; }
    

    I isolated the 3 fixed widths that would need to change, and simply increase each of them by 150px whenever the 'wide' class is added to the body.

    I hope this wider design is a bit more readable for the 98% of you with a higher resolution.

    Tags: javascript css layout browser
    View 4 Comments | Add a comment
  • Comments

    1. Mauvis at 9:41pm on May 20, 2007

    I dig it. I disabled JavaScript and I can see what you mean about it being a little too narrow (I'm 1280x1024.)

    Yay for progressive enhancement.

    2. Nick at 1:09pm on August 8, 2007

    awesome script
    how could you resize an image as well?

    3. Kristine at 10:30pm on March 21, 2008

    surely there's a way to make it work the other way around - resizing it for everyone *under* 930px? if there is a way, PLEASE could you let me know? that would save my final project!!!

    4. Jesse Skinner at 10:28am on March 23, 2008

    @Kristine - that's simple. Just change the example above to check for a width less than 930:

        if (document.body.clientWidth < 930) {
            document.body.className += ' thin';
        }

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • About Me
    • Email Me
    • RSS Feed RSS Icon
    • @JesseSkinner
  • Recent Articles

    • jQuery Live Events
    • I need web developers
    • buttons need type="submit" to submit in IE
    • Win $200 in a Web Dev Writing Contest
    • Use Arrays in HTML Form Variables
    • 5 Reasons Freelancers Can Succeed in a Shrinking Economy
    • Keeping a Live Eye on Logs
    • Using PHP's empty() Instead of isset() and count()
    • Testing Web Pages with Lynx
    • Stop CSS Background Flickering in Internet Explorer 6
    • See All...
  • Categories

    • javascript (39)
    • links (21)
    • about (19)
    • web (14)
    • html (12)
    • server (11)
    • css (8)
    • browsers (8)
    • carnival (7)
    • work (6)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
  • Older Articles

    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2010 The Future of the Web