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;
}

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Kaspars

    Thanks guys, I really needed it.

  • http://www.tiffanybestsilver.com Christmas Day Gifts

    That’s wonderful, just keep it up.

  • http://www.adesignlink.com Chad P

    THere is another way of doing something similar to this using semantic markup: http://css-tricks.com/resolution-specific-stylesheets/

    This enables you to change the layout based on the users window size.

    example:

    &ltlink rel=’stylesheet’ media=’screen and (min-width: 701px) and (max-width: 900px)’ href=’css/medium.css’ /&gt

    The only drawback is that IE doesn’t support it. Darn you IE.

  • Abdul-hadi Hawari

    Thanks a lot, so simple and useful :)

  • http://www.capt.com.cn 卡比特

    这文章写得太好了,博主真牛

  • http://www.ahmetkayar.com.tr/ Ahmet Kayar

    Thanks guys, Good video

  • http://psykhopathiasexualis.blogspot.com Genaro

    Amazing examples. Thanks!

  • http://www.taliawebdesign.co.uk web design coventry

    thanks for simple but very useful tut.

  • http://www.iworlds.it Frank

    Very nice tutorial! Thanks man! :)

  • http://w3tricks.wordpress.com Labbeb

    Hi frnds,

    Here is the latest tips for the responsive design.

    Please have a look on below linnk. it will help you make responsive design with any conditional tag…

    here the trick works with the CSS…

    http://w3tricks.wordpress.com/2012/05/24/responsive-theme-design/

  • http://www.chefkhawla.com Ahmed Abbouh

    Hello! this is another amazing tip to learn in the morning, thank you so much guys.