Quick Tip: Different Layouts for Different Widths
videos

Quick Tip: Different Layouts for Different Widths

It’s becoming more and more common for web sites and applications to provide different layouts dependent upon the user’s window size, or resolution. This can be accomplished in a variety of ways, ranging from CSS to JavaScript solutions.

In this video quick tip, we’ll learn how laughably simple it is to do this with a touch of jQuery, and the resize() method.

By utilizing jQuery’s “resize()” method, we can easily listen for when the user changes the width of their browser window.

function checkWindowSize() {

	if ( $(window).width() > 1800 ) {
		$('body').addClass('large');
	}
	else {
		$('body').removeClass('large');
	}

}

$(window).resize(checkWindowSize);

Then, subsequently, we target our desired CSS properties accordingly.

#container {
	width: 800px;
	height: 1000px;
	background: #e3e3e3;
	margin: auto;
}

/* Change container size for larger windows. */
.large #container {
	width: 1000px;
}

#nav {

	width: 100%;
	height: 100px;
	border-bottom: 1px solid white;
	background: #999999;

}

.large #nav {
	float: left;
	width: 200px;
	border-bottom: none;
	border-right: 1px solid white;
	height: 1000px;
}

Add Comment

Discussion 75 Comments

Comment Page 1 of 21 2
  1. BroOf says:

    Hehe nice tip thank you jeffrey! This is awesome for mobilephones!

  2. kuldeep says:

    Hey really nice tuts I hope i am gone use this sooner or later……………….

  3. Hooman says:

    useful tip. tnx jeff

  4. ryan says:

    yea great tip for all those readers out there using old monitors. they aren’t as lucky as us web designers to have huge monitors. =D

  5. Tim says:

    That’s a pretty awesome tip. I love how it seems complex but only requires a few additional lines of code. Thanks!

  6. dixie says:

    You have taught me SOOOOO Much,,, I learn best by watching the results,, but could I ask a favor,,,,on your demo’s you use a black background,,, it makes it hard to see the code as you type it in,,,,,could you use a white background,,?

    Thank you for teaching an old dog the new tricks,,,
    Dixie

  7. Áki says:

    That’s really a quick tip.

    Thanks

  8. Everybody should checkout Elastic CSS Framework, to accomplish this in a simpler way

  9. sam says:

    or you could drop the dumb fixed-width websites and make one that actually works at multiple resolutions! It’s not that hard!!

    • oconn96 says:

      Fluid websites have been done before but some screens now are close to 2000 pixels wide. Would you want to read a line of text that wide? because with fluid layouts the potential is there. Designing for 1024 is what I go for now, always put in one of these tricks to compensate for widescreen users.

  10. Greg Babula says:

    Thanks, this could be very useful soon

  11. ema says:

    thanks Jeff, what happen to ur blog? http://www.jeffrey-way.com/ getting goddady default page

  12. Wanyax says:

    Just as I was scratching my head over how to get this effect. This site is a godsend! Thanks Jeffrey. :)

  13. Ryan says:

    You could use media queries for the better browsers to accomplish this without javascript. I did an article on orientation detection for the iPhone using CSS3 media queires http://www.thecssninja.com/css/iphone-orientation-css. This could be modified to do what you’re demo does and fallback to javascript when it’s not available.

  14. Retnuh says:

    Nice Tut…

  15. Adrian says:

    never thought of that but it’s a great idea. anyone have an idea of how to animate that?

  16. Alexcsandru says:

    Nice man ! tnx for this! you are the best !

  17. nice tip Jeffrey, thanks for sharing. :)

  18. khaled says:

    This is A useful tip jeff!: Thanks a lot!

  19. jmarreros says:

    very nice tip, thanks for sharing Jeffrey

  20. Moksha says:

    good tutorial, thanks

  21. Mofoo says:

    Hey nice tut but the if statement in checkWindowSize has the same code for both branches…

  22. John says:

    Hey Jeffery great screencast.

    Do you or anyone else know of a Windows Editor that auto completes your lines of code like yours? That’s a huge time saver!

  23. Beautiful way to maximize your site’s compatibility to different resolutions. simple and great!

  24. Owen says:

    Hello Jeffrey, I have been following a lot of your jQuery tuts lately, but for this one, my page only resizes after I shrink and then click refresh. I have it set to

    $(function () {

    rather than

    function checkWindowSize() {

    because with the latter option my page won’t resize at all, with or without refresh

  25. Roger says:

    your are the best teacher I have…
    everyday I learn something new and very useful!

    thank’s a lot!

  26. mutiu says:

    quick and tasty…

  27. Stefan says:

    Very cool tip! I like it so much. I’m working on a Mac with a very high resolution and all my Designs look very chaotic on a windows pc… Helpful!

  28. Allison says:

    Nice. Thanks Jeff!

  29. Adel says:

    Great tip. thank you jeffrey!

  30. Jonas Brauer says:

    Nice walk through!
    One of the best I’ve seen. Quick, simple and great information.

  31. That is great, I know some clients still use 800×600 on a 22 inch monitor :-)

  32. Andris says:

    This is a great tip. Especially because it degrades fine. Thanx Jeff!

  33. Saurabh Shah says:

    woot ! what an awesome tip … Thanks Jeff !!!

  34. edMaga says:

    Awesome! Very helpful

  35. Jhon says:

    That’s a big one! Thank you very much!

  36. David Moreen says:

    I always had a problem similar to this, when I try to make a bar type thing with an iframe. The only issue is that I have thought myself to always assume that the user has javascript off… :/

  37. Steven Davis says:

    Cool. I’ve been doing exactly this on a website I’m doing at work. I basically listen for users resolution on page load and window resize. If below 1280, nothing, if above, it adds a “wide” class to the body :)

  38. Bryan Watson says:

    Great Tip!

    Also, this is a good way to incorporate some advertising space in your websites for those who sufficient screen real estate to show it.

  39. Lea says:

    Very cool.. these webdesign studies are so muchfun since I found this site – Caution highly addictive ! Thank you so much

  40. Norguad says:

    Hi,
    I have just one question. The resizing function works great, but if I have my browser window maximized, the code does not add the large class to the body. Is there a way how to solve it?

    • Jeffrey Way says:
      Author

      Are you sure? It seems to be working for me. What browser are you using?

      • Norguad says:

        I tested in FF3.6, Safari 4, Opera 10 – it is not working in the moment, when the browser starts in maximized window. If I resize the window, it works, even if I maximize it from not maximized window.

      • Norguad says:

        Btw. Using PC Windows Vista.

      • Norguad says:

        No one has the same problem as me?

      • Rogier Borst says:

        I’m working on Windows 7 and have the same problem. No matter what browser I use (Firefox, IE, Opera, Safari or Google Chrome), when the window is maximized and I load my site, it doesn’t apply the large class.
        It seems that the script used for the Nettuts site (tutsResize.js) is different and it works even when you start out with a maximized browser window. I’m no Javascript / jQuery expert, but I think that script works better because it also fires when the page is loaded.
        I’ll see if I can come up with a solution, because I’d love to get this working properly. Great tip, Jeffrey :)

    • Rogier Borst says:

      I found the problem.
      There’s a difference between the code in the screencast and the code on this page.

      In the screencast you’ll see the following two lines:

      checkWindowSize();
      $(window).resize(checkWindowSize);

      The first line fires the resize function on window load. The second line fires the resize function whenever the window is resized afterwards.

      In the snippet on this page, that first line (checkWindowSize()) is left out, so the function won’t be fired when the window loads. Add the line, and everything works fine again.

  41. SAMY says:

    I don’t Knopw anything about javascript but i think if you help me to do that i’ll really Thank you , i say that because i try it but without any succes .
    my probleme that i want to do that but me when the size is begger than 1800 put to my #Header a background else put an other background can you help to do that Please !?

  42. SkipSoft says:

    WOW!!!!

  43. Marcus says:

    Great tut!
    I’d love it if you could make a tutorial how too make like rounded corners for internet explorer & opera!

  44. That’s a pretty awesome tip. I love how it seems complex but only requires a few additional lines of code. Thanks!

  45. DMCGames says:

    Great tips. It is particularly useful when adjusting the site’s layout for different screen sizes.

Comment Page 1 of 21 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.