<?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>Corunet. El Blog &#187; html</title>
	<atom:link href="http://blog.corunet.com/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.corunet.com</link>
	<description>Web development, usability and more</description>
	<lastBuildDate>Fri, 23 Oct 2009 15:33:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Three column layout with full page height</title>
		<link>http://blog.corunet.com/three-column-layout-with-full-page-height/</link>
		<comments>http://blog.corunet.com/three-column-layout-with-full-page-height/#comments</comments>
		<pubDate>Thu, 14 Jun 2007 19:36:56 +0000</pubDate>
		<dc:creator>David Pardo</dc:creator>
				<category><![CDATA[CSS and Javascript]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.corunet.com/english/three-column-layout-with-full-page-height</guid>
		<description><![CDATA[
Sometimes I wish I could do things that were easily done with table-based layouts but quite hard using just CSS.
Following a couple of posts in a CSS related Spanish mailing list (Ovillo),  a guy called Zafonic showed me how to use negative margins and positive paddings to make equal sized columns. With a couple [...]]]></description>
			<content:encoded><![CDATA[<p><img class="size-thumbnail wp-image-90 alignleft" title="3" src="http://blog.corunet.com/wp-content/uploads/2007/06/3-150x150.png" alt="The final result" width="150" height="150" /></p>
<p>Sometimes I wish I could do things that were easily done with table-based layouts but quite hard using just CSS.<br />
Following a couple of posts in a CSS related Spanish mailing list (Ovillo),  a guy called Zafonic showed me how to use negative margins and positive paddings to make equal sized columns. With a couple javascript functions, I patched it to fill the full browser window. Let&#8217;s see how&#8230;<br />
<strong>Update:</strong> Error in code solved. Thanks to the testers<span id="more-64"></span></p>
<h2>The problem</h2>
<p>We&#8217;re going to write a three-column plus footer layout that covers all the browser space. The requisites are the following:</p>
<ul>
<li>The three columns will have different background colors</li>
<li>We&#8217;re not going to use background images (so, no <a href="http://alistapart.com/articles/fauxcolumns/" onclick="pageTracker._trackPageview('/outgoing/alistapart.com/articles/fauxcolumns/?referer=');">faux-columns</a>)</li>
<li>We want the page to cover the full height of the browser. So, the footer will be at the bottom of the screen.</li>
<li>The layout has to degrade gracefully for javascript disabled browsers</li>
<li>All the code has to be valid and accesible</li>
</ul>
<h2>The beginning</h2>
<p>The first part is simple. We need some html that defines three columns and a footer. Lets wrap the three columns within a div (called, by the way, container) and call the three columns col1, col2 and col3. The html code will be something like:</p>
<p class="toggle_code"><a href="/uploads/code/body.html" onclick="jQuery('#code_0').toggle('slow');return false">body.html</a></p>
<div  style="display:none" id="code_0">
<pre class="brush: html">&lt;body&gt;
	&lt;div id=&quot;container&quot;&gt;
		&lt;div id=&quot;col1&quot;&gt;
			first column&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;
		&lt;/div&gt;
		&lt;div id=&quot;col2&quot;&gt;
			second column&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-
		&lt;/div&gt;
		&lt;div id=&quot;col3&quot;&gt;
			third column&lt;br /&gt;-&lt;br /&gt;-
		&lt;/div&gt;
	&lt;/div&gt;
	 &lt;div id=&quot;footer&quot;&gt;footer&lt;br /&gt;-&lt;/div&gt;
&lt;/body&gt;
</pre>
</div>
<p>We need to give the columns a width and float them to the left. The css would be:</p>
<p class="toggle_code"><a href="/uploads/code/first.css" onclick="jQuery('#code_1').toggle('slow');return false">first.css</a></p>
<div  style="display:none" id="code_1">
<pre class="brush: css">body{
	margin:0 auto;
}
#col1{
	background-color:red;
	width:20%;
	float:left;
}
#col2{
	background-color:blue;
	width:60%;
	float:left;
}
#col3{
	background-color:yellow;
	width:20%;
	float:left;
}
#footer{
	height:40px;
	clear:both;
	background-color:black;
	color:white;
	width:100%;
}
</pre>
</div>
<p>So, we already have a three column layout, but each column has a diferent height and the footer is far from the bottom:</p>
<p><img src="/uploads/threecolumns/1.png" alt="first version" /></p>
<h2>Negative margins to the rescue</h2>
<p>As I already told you, I had&#8217;t heard about creating layouts using negative margins (I should have read <a href="http://alistapart.com/articles/negativemargins" onclick="pageTracker._trackPageview('/outgoing/alistapart.com/articles/negativemargins?referer=');">this article from A List Apart</a> some time ago). In a nutshell, it says that you can use a large negative margin and the opposite positive padding to &#8220;make room for the columns&#8221;. I recommend to read it if you haven&#8217;t, it&#8217;s very useful.<br />
So, let&#8217;s go on the code. We&#8217;re going to add a -5000 pixel margin-bottom and a 5000 pixel padding-bottom to each column:</p>
<p class="toggle_code"><a href="/uploads/code/second.css" onclick="jQuery('#code_2').toggle('slow');return false">second.css</a></p>
<div  style="display:none" id="code_2">
<pre class="brush: css">body{
	margin:0 auto;
}
#col1{
	background-color:red;
	width:20%;
	float:left;
	margin-bottom:-5000px;
 	padding-bottom:5000px;
}
#col2{
	background-color:blue;
	width:60%;
	float:left;
	margin-bottom:-5000px;
 	padding-bottom:5000px;
}
#col3{
	background-color:yellow;
	width:20%;
	float:left;
	margin-bottom:-5000px;
 	padding-bottom:5000px;
}
#footer{
	height:40px;
	clear:both;
	background-color:black;
	color:white;
	width:100%;
	margin-bottom:-5000px;
 	padding-bottom:5000px;
}
</pre>
</div>
<p>This way we get same height columns. This looks much better:</p>
<p><img src="/uploads/threecolumns/2.png" alt="second version" /></p>
<p>but we don&#8217;t have 100% height yet. Internet explorer supports 100% height but all the other browsers don&#8217;t. So, we&#8217;ll need to use javascript to solve it:</p>
<h2>Filling the screen &#8211; Here comes Javascript</h2>
<p>The first thing we need is to find the height of the browser window. I use a small javascript function that takes into account the diferent behaviours of the different browsers:</p>
<p class="toggle_code"><a href="/uploads/code/height.js" onclick="jQuery('#code_3').toggle('slow');return false">height.js</a></p>
<div  style="display:none" id="code_3">
<pre class="brush: js">function windowHeight(){
	var alto= 0;
	if( typeof( window.innerWidth ) == &#039;number&#039; ) {
		alto= window.innerHeight;
	} else if( document.documentElement &amp;&amp; ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		alto= document.documentElement.clientHeight;
	} else if( document.body &amp;&amp; ( document.body.clientWidth || document.body.clientHeight ) ) {
		alto= document.body.clientHeight;
	}
	return alto;
}
</pre>
</div>
<p>This function returns the height of the browser window. It does one thing and does it well&#8230; Using this new knokledge, we can set the height of the columns, making them the browser&#8217;s window height minus the footer, to fill the screen completely. The other function we&#8217;ll need is:</p>
<p class="toggle_code"><a href="/uploads/code/fill.js" onclick="jQuery('#code_4').toggle('slow');return false">fill.js</a></p>
<div  style="display:none" id="code_4">
<pre class="brush: js">function fillthescreen(){
	winH = windowHeight(); //This returns the screen heigth
	heightNeeded=winH-40; //We need to substract the footer height
	if( typeof( window.innerWidth ) != &#039;number&#039; ) { //Explorer doesn&#039;t recognize minHeight
		document.getElementById(&#039;co11&#039;).style.height=heightNeeded+&#039;px&#039;; //So, we use height (and explroer bug)
	}
	document.getElementById(&#039;col1&#039;).style.minHeight=heightNeeded+&#039;px&#039;; //For every other browser, we use minHeight
}
</pre>
</div>
<p>This function works by calling the last one (windowHeight) and substracting the footer size. If we&#8217;re in Explorer, we use style.height to modify the height of the columns and in all other browsers, minHeight, since Explorer doesn&#8217;t support that attribute. We&#8217;re going to call that function via onload.<br />
And we get this:</p>
<p><img src="/uploads/threecolumns/3.png" alt="third version" /></p>
<p>If the window is not tall enough to fit the column height, the page behaves normally:</p>
<p><img src="/uploads/threecolumns/4.png" alt="small screen" /></p>
<h2>The full code</h2>
<p>Putting all together, this is what we get:</p>
<p class="toggle_code"><a href="/uploads/code/3.html" onclick="jQuery('#code_5').toggle('slow');return false">3.html</a></p>
<div  style="display:none" id="code_5">
<pre class="brush: html">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Demo three columns&lt;/title&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	function fillthescreen(){
		winH = windowHeight(); //This returns the screen heigth
		heightNeeded=winH-40; //We need to substract the footer height
		if( typeof( window.innerWidth ) != &#039;number&#039; ) { //Explorer doesn&#039;t recognize minHeight
			document.getElementById(&#039;col1&#039;).style.height=heightNeeded+&#039;px&#039;; //So, we use height (and explroer bug)
		}
		document.getElementById(&#039;col1&#039;).style.minHeight=heightNeeded+&#039;px&#039;; //For every other browser, we use minHeight
	}

	function windowHeight(){
		var alto= 0;
		if( typeof( window.innerWidth ) == &#039;number&#039; ) {
			alto= window.innerHeight;
		} else if( document.documentElement &amp;&amp; ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			alto= document.documentElement.clientHeight;
		} else if( document.body &amp;&amp; ( document.body.clientWidth || document.body.clientHeight ) ) {
			alto= document.body.clientHeight;
		}
		return alto;
	}
&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
body{
	margin:0 auto;
}
#container{
 overflow:hidden;
}
#col1{
 background-color:red;
 width:20%;
 float:left;
margin-bottom:-5000px;
 padding-bottom:5000px;
 }
#col2{
 background-color:blue;
 width:60%;
 float:left;
margin-bottom:-5000px;
 padding-bottom:5000px;
 }
#col3{
 background-color:yellow;
 width:20%;
 float:left;
margin-bottom:-5000px;
 padding-bottom:5000px;
 }
#footer{
	height:40px;
 clear:both;
 float:none;
 background-color:black;
 color:white;
 width:100%;
}

&lt;/style&gt;
&lt;/head&gt;

&lt;body onload=&quot;fillthescreen()&quot;&gt;
	&lt;div id=&quot;container&quot;&gt;
		&lt;div id=&quot;col1&quot;&gt;
			first column&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;
		&lt;/div&gt;
		&lt;div id=&quot;col2&quot;&gt;
			second column&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-&lt;br /&gt;-
		&lt;/div&gt;
		&lt;div id=&quot;col3&quot;&gt;
			third column&lt;br /&gt;-&lt;br /&gt;-
		&lt;/div&gt;
	&lt;/div&gt;
	 &lt;div id=&quot;footer&quot;&gt;footer&lt;br /&gt;-&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<p>If you use it in a production environment, it&#8217;d be wise to split the javascript and the CSS into their own files, and review all the code, since it&#8217;s done in a quick and dirty way.</p>
<p>You can download the code using <a href="/uploads/threecolumns/3.zip">this link</a>. People without javascript don&#8217;t get the full height but the site is equally usable and visually pleasant. I you have any further questions or just want to talk, drop me a line to <a href="maito:david@corunet.com">david@corunet.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.corunet.com/three-column-layout-with-full-page-height/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
