<?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>Big Red Tin &#187; layout</title>
	<atom:link href="http://bigredtin.com/tag/layout/feed/" rel="self" type="application/rss+xml" />
	<link>http://bigredtin.com</link>
	<description>Thoughts about the web and business from the large pantry</description>
	<lastBuildDate>Tue, 15 Nov 2011 23:15:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>JavaScript Equal Height Columns</title>
		<link>http://bigredtin.com/2008/js-equal-height-cols/</link>
		<comments>http://bigredtin.com/2008/js-equal-height-cols/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 02:32:16 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[layout]]></category>

		<guid isPermaLink="false">http://peterwilson.cc/?p=94</guid>
		<description><![CDATA[The desire for equal height columns in a CSS layout is nothing new; there are many solutions available, some use JavaScript, others use CSS with negative margins, and then, there's the faux columns method using background images. All of these methods have their place as perfectly valid solutions, and, depending on the situation, may be the best solution available.]]></description>
			<content:encoded><![CDATA[<p>The desire for equal height columns in a CSS layout is nothing new; there are many solutions available, some use JavaScript, others use <a title="One True Layout" href="http://www.positioniseverything.net/articles/onetruelayout/equalheight">CSS with negative margins</a>, and then, there&#8217;s the <a title="Faux Columns" href="http://www.alistapart.com/articles/fauxcolumns/">faux columns</a> method using background images. All of these methods have their place as perfectly valid solutions, and, depending on the situation, may be the best solution available.  <span id="more-94"></span>It&#8217;s the JavaScript methods available that are the subject this article; most of the solutions available &#8211; certainly those on the first page when <a title="Google 'equal height javascript'" href="http://www.google.com.au/search?q=equal+height+javascript">googling &#8216;equal height javascript&#8217;</a> &#8211; include a variation of either of the lines below:</p>
<pre><code>column[i].style.height = maxHeight + 'px';
// or
column[i].style.minHeight = maxHeight + 'px';</code></pre>
<p>The use of inline styles creates an edge case that can result in a large, ugly, gap between the equalized columns and the footer when printing the page; if footer is pushed on to an otherwise blank page it becomes wasteful.  Example 1: <a title="Layout without equalised columns" href="http://code.peterwilson.cc/js-equal-height-cols/eg1/">Layout without equalised columns</a> Example 2: <a title="Equalised columns with traditional JavaScript" href="http://code.peterwilson.cc/js-equal-height-cols/eg2/">Equalised columns with traditional JavaScript</a> (<a title="Print view of equalised columns with traditional JavaScript" href="http://code.peterwilson.cc/js-equal-height-cols/eg2/print-version.pdf">pdf of print view</a></p>
<p>)</p>
<h4>Basic requirements</h4>
<p>When deciding to write my own version of the script, rather than sending an array, or limiting the script to a certain number of columns, I decided to send the requirements as an expression, ie</p>
<pre><code>PWCC.equalHeights("content=sidebar");</code></pre>
<p>To avoid the printing problem in example 2, CSS is created dynamically and assigned to the relevant media type &#8211; in this case &#8216;screen&#8217; or &#8216;screen, handheld&#8217;. I&#8217;ve used <a title="Using dynamic CSS to hide content before page load" href="http://www.bobbyvandersluis.com/articles/dynamic_css/index.html">a script by Bobby van der Sluis</a> &#8211; modified to allow the specification of the media type and moved it into my namespace &#8211; to create the CSS.</p>
<pre><code>function createStyleRule(selector, declaration) {
//becomes
PWCC.createStyleRule = function (selector, declaration, media) {
if (media == null) {
  media = 'screen';
}

style_node.setAttribute("media", "screen");
//becomes
style_node.setAttribute("media", media);</code></pre>
<h4>The function</h4>
<p>Using the traditional version in example two, the inline styles make it necessary to loop through each column twice, first to get maximum height, and, subsequently to set the height on each element.  In the function below the use of dynamic CSS allows a class name to be created at the start of the script and applied to each column in the initial loop &#8211; very slightly reducing processing power; in the script below a random number is appended to the class name to help ensure there are no clashes with the static CSS &#8211; with a bit of care it can be removed.</p>
<pre><code>PWCC.equalHeight = function (expr, className, media) {
  if (className == null) {
    className = 'equalHeight';
  }
  if (media == null) {
    media = 'screen'; //default
  }

  //split expr at = signs
  var sections = expr.split("=");

  //generate class name
  var classRandom = Math.floor(Math.random()*999999);
  className = className + '-' + classRandom;

  //get column heights, add class name to html elemenets
  var objects = new Array();
  var maxHeight = 0;
  for(var i=0; i &lt; sections.length; i++) {
    objects[i] = document.getElementById(sections[i]);
    PWCC.addClass(objects[i], className);
    //addClass is in base.js - it adds a class to an element.

    if (objects[i].offsetHeight &gt; maxHeight) {
      maxHeight = objects[i].offsetHeight;
    }
  }

  //create css declaration
  var classDeclaration = 'min-height: ' + maxHeight + 'px;'
  var ie6Declaration = 'height: ' + maxHeight + 'px;'
  PWCC.createStyleRule('.' + className, classDeclaration, media);
  PWCC.createStyleRule('* html .' + className, ie6Declaration, media);
}</code></pre>
<p>The IE6 declaration has been made using a * html hack; I considered this a better option than the others available, browser sniffing, or, JavaScript conditional comments; others&#8217; opinions may differ and prefer to avoid the * html hack.  Finally to call the script once the page loads, add a function call to the load event</p>
<pre><code>PWCC.addLoadEvent(function() {
  //addLoadEvent is in base.js - adds code to window.onload
  PWCC.equalHeight('content=sidebar');
});</code></pre>
<p>The <a title="Final version of equal height columns" href="http://code.peterwilson.cc/js-equal-height-cols/eg3/">final working version can be seen in example 3</a>, along with the <a title="Source javascript for example 3" href="http://code.peterwilson.cc/js-equal-height-cols/eg3/assets/j/custom.js.txt">source JavaScript</a>; the function above makes calls to my <a title="Base JavaScript File" href="http://bigredtin.com/index.php?p=82">base.js</a> file.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/2008/js-equal-height-cols/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

