<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Daniel Koskinen &#187; jquery</title>
	<atom:link href="http://danielkoskinen.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://danielkoskinen.com</link>
	<description>Web designer, WordPress consultant and usability enthusiast</description>
	<lastBuildDate>Sat, 31 Mar 2012 14:00:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-20320</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Flexible fluid layouts with CSS and jQuery (Part 2 in series)</title>
		<link>http://danielkoskinen.com/flexible-fluid-layouts-with-css-and-jquery-part-2/</link>
		<comments>http://danielkoskinen.com/flexible-fluid-layouts-with-css-and-jquery-part-2/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 08:39:21 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://danielkoskinen.com/?p=302</guid>
		<description><![CDATA[<p><p><p>from <a href="http://danielkoskinen.com">Daniel Koskinen</a></p></p><p>In this post I'll demonstrate with a simple example how easy it is, with CSS and a bit of jQuery, to make layouts that adapt to the many different screen sizes out there.</p></p><p><p>Original post: <a href="http://danielkoskinen.com/flexible-fluid-layouts-with-css-and-jquery-part-2/">Flexible fluid layouts with CSS and jQuery (Part 2 in series)</a></p></p>]]></description>
			<content:encoded><![CDATA[<p><p>from <a href="http://danielkoskinen.com">Daniel Koskinen</a></p></p><p>This is a follow-up to last week&#8217;s post, <a href="http://danielkoskinen.com/2010/should-we-design-for-wide-screens-part-1/">Should we design for wider screens?</a>. In this post I&#8217;ll demonstrate with a simple example how easy it is, with CSS and a bit of jQuery, to make layouts that adapt to the many different screen sizes out there.<br />
If you&#8217;re feeling impatient, <a href="http://test.dani.fi/fluid/">jump straight to the demo</a>!</p>
<h2>Example HTML</h2>
<p>At the top level, my example HTML page has a #wrapper div, which contains a #header, #main div containing all the interesting stuff, and a #footer. The main section has #content, #primary, #secondary and #tertiary divs for the different content areas of my page. The first two content areas, #content and #primary are wrapped up in an additional #container div, but your own markup may obviously differ.</p>
<pre class="brush:php">&lt;body class="normal&gt;
&lt;div id="wrapper"&gt;
	&lt;div id="header"&gt; ... &lt;/div&gt;
	&lt;div id="main"&gt;
		&lt;div id="container"&gt;
			&lt;div id="content"&gt; ... &lt;/div&gt;
			&lt;div id="primary"&gt; ... &lt;/div&gt; 
		&lt;/div&gt;
		&lt;div id="secondary"&gt; ... &lt;/div&gt;
		&lt;div id="tertiary"&gt; ... &lt;/div&gt;
	&lt;/div&gt;&lt;!-- #main --&gt;
 
	&lt;div id="footer"&gt; ... &lt;/div&gt;
&lt;/div&gt;&lt;!-- #wrapper --&gt;
&lt;/body&gt;</pre>
<h2>Prepare your styles</h2>
<p>The basic idea here is to have different CSS styles for each width. The styles could be changed directly through jQuery, but that would quickly result in a lot of messy code. Instead, we&#8217;ll use jQuery only to change one class attribute. Initially I&#8217;ve added the class &#8216;normal&#8217; to the <code>body</code> element to ensure a sensible default for browsers with JavaScript disabled, but we&#8217;ll also need styles for &#8216;wide&#8217;, &#8216;slim&#8217; and &#8216;narrow&#8217;. This is easy to do with CSS descendant selectors:</p>
<pre><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">
<pre class="brush:css">#wrapper { margin: 0 auto; }
#header {overflow: hidden;}
#container,#content,#primary,#secondary,#tertiary
  {float:left;}
#footer {clear:left;width:100%;}

/* Wide ( over 1100px ) */
.wide #container {width:60%;}
.wide #content {width:66%;}
.wide #primary {width:34%;}
.wide #secondary,.wide #tertiary {width:20%;}

/* Normal ( over 800px ) */
.normal #container {width:50%;}
.normal #secondary,.normal #tertiary {width:25%;}

/* Slim ( over 600px ) */
.slim #container{width:66%;}
.slim #secondary,.slim #tertiary {width:34%;}

/* Narrow ( under 600px ) */
.narrow #secondary,.narrow #tertiary {width:50%;}
.narrow #secondary {clear:both;}</pre>
<p></span></p>
<pre><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">Unfortunately versions 7 and older of Internet Explorer deal differently with percentage widths compared to most other browsers, and can result in some funny behaviour. I see two primary ways to deal with it: either feed IE 6 and 7 it's own rules (e.g. a tested, fixed-width layout) or just make sure that the percentages never add up to 100 %. A nice explanation of this annoying feature can be found over at <a href="http://www.ojctech.com/content/css-jumping-columns-and-ies-percentage-rounding-algorithm">OJTech.com</a>.</span>
</pre>
<h2>The jQuery</h2>
<p>First of all I need to include jQuery, and perhaps the easiest way is to use <a href="http://code.google.com/apis/ajaxlibs/documentation/#jquery">Google&#8217;s jQuery</a> for this. The main bit is a function, checkWidth() that (you guessed it) checks the width of the browser window and sets the body class attribute accordingly. The code below should be pretty self-explanatory.</p>
<pre><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">
<pre class="brush:js">	function checkWidth () {
		if ($(window).width() &gt; 1400) {
			$('body').attr('class','superwide');
			$('#title').text('Wide ('+$(window).width()+')');
			}
		else if ($(window).width() &gt; 1100) {
			$('body').attr('class','wide');
			$('#title').text('Wide ('+$(window).width()+')');
			}
		else if ($(window).width() &gt; 800) {
			$('body').attr('class','normal');
			$('#title').text('Normal  ('+$(window).width()+')');
			}
		else if ($(window).width() &gt; 600) {
			$('body').attr('class','slim');
			$('#title').text('Slim ('+$(window).width()+')');
			}
		else {
			$('body').attr('class','narrow');
			$('#title').text('Narrow ('+$(window).width()+')');
			}
		}</pre>
<p></span></p>
<pre><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">In addition to this, all I need is some code that will tell the browser when to run the checkWidth() function.</span>
</pre>
<pre><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">
<pre class="brush:js">	$(document).ready(function(){

		// check the window size when page loads
		checkWidth();

		// check on resize
		$(window).resize(function() {
			checkWidth();
		});

	});</pre>
<p></span></p>
<pre><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">That's it! <a href="http://test.dani.fi/fluid/">View the finished demo</a>.</span>
</pre>
<p>After starting to write this post, I read the latest issue (#200) of .net magazine and noticed a mention of a very cool way to do a similar trick using only CSS media queries! <a href="view-source:http://people.opera.com/brucel/demo/MQ.html">Media Query demo by Bruce Lawson</a>. Haven&#8217;t tried it out yet, but I definitely will.</p>
<p>Hope you liked the post, feel free to <a href="http://twitter.com/home?status=RT+%40danielck+Flexible+fluid+layouts+with+CSS+and+jQuery+%28Part+2+in+series%29+http%3A%2F%2Fdani.fi%2FXs">tweet it</a>, or leave a comment below.</p>
<p><p>Original post: <a href="http://danielkoskinen.com/flexible-fluid-layouts-with-css-and-jquery-part-2/">Flexible fluid layouts with CSS and jQuery (Part 2 in series)</a></p></p>]]></content:encoded>
			<wfw:commentRss>http://danielkoskinen.com/flexible-fluid-layouts-with-css-and-jquery-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

