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;
}
- Follow us on Twitter, or subscribe to the Nettuts+ RSS Feed for the best web development tutorials on the web.
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
- http://www.broof.de BroOf
- Commenter
- http://www.BryanWatson.ca Bryan Watson
- kuldeep
- http://hdesignsPlus.com Hooman
- ryan
- http://www.nefariousindulgence.com Tim
- http://nolasnovelties.com dixie
- http://akiweb.eu/ Áki
- http://elasticss.com Sergio de la Garza
- http://www.samswitzer.org/ sam
- oconn96
- http://www.gregbabula.com Greg Babula
- http://emanuelesala.com ema
- http://net.tutspus.com Jeffrey way
- dave
- Wanyax
- http://www.thecssninja.com/ Ryan
- Retnuh
- http://adrusi.com Adrian
- Alexcsandru
- http://www.shabithishan.com shabith ishan
- http://davidfitzgibbon.com David Fitzgibbon
- khaled
- jmarreros
- http://mokshasolutions.com Moksha
- http://javatutor.net Java developer
- http://www.chimera-studios.co.uk/ Chimera Studios web design
- Mofoo
- Mofoo
- http://envexlabs.com Matt Vickers
- Jeffrey Way
- http://www.websiteperks.com John
- http://www.webtechwise.com/ Omer Greenwald
- http://owenconti.com/freedom Owen
- http://bloggerzbible.blogspot.com/ Bloggerzbible
- Roger
- http://ilopezdeaudikana.com mutiu
- http://www.lifesub.de Stefan
- http://www.keetee.com Allison
- http://pollfa.ir abdollah2
- http://bleddz.com Adel
- http://www.emanio.se Jonas Brauer
- http://www.jordanwalker.net/index.php Jordan Walker
- http://www.andrislinz.ch Andris
- http://www.designstudio16.com Saurabh Shah
- http://www.twitter.com/edMaga edMaga
- Jhon
- http://spotdex.com/ David Moreen
- http://www.stevendavisphoto.com Steven Davis
- http://www.BryanWatson.ca Bryan Watson
- Lea
- Norguad
- Norguad
- Norguad
- Norguad
- Rogier Borst
- Rogier Borst
- Norguad
- SAMY
- SkipSoft
- http://tacogames.org Marcus
- http://www.privateserverybk.com private serverlar
- http://www.dmcgames.co.nz DMCGames
