<?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>Chromic Design &#187; Javascript</title>
	<atom:link href="http://www.chromicdesign.com/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.chromicdesign.com</link>
	<description>Design With A Chrome</description>
	<lastBuildDate>Fri, 06 Aug 2010 05:59:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Equal Height Division Using Javascript And CSS</title>
		<link>http://www.chromicdesign.com/2009/03/equal-height-division-using-javascript-and-css.html</link>
		<comments>http://www.chromicdesign.com/2009/03/equal-height-division-using-javascript-and-css.html#comments</comments>
		<pubDate>Fri, 27 Mar 2009 08:47:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[XHTML & CSS]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Equal Height Sidebar]]></category>

		<guid isPermaLink="false">http://www.chromicdesign.com/?p=199</guid>
		<description><![CDATA[This technique is really simple. Having equal height division in your design is sometimes necessary to maintain consistent look and feel on your layout. You can use this technique to make your sidebars equal height, or to make your content area and the sidebars equal height. Get The Source Code:&#160;&#160;Without Jquery&#160;&#160;With Jquery Step 1: Create [...]]]></description>
			<content:encoded><![CDATA[<p>This technique is really simple. Having equal height division in your design is sometimes necessary to maintain consistent look and feel on your layout. You can use this technique to make your sidebars equal height, or to make your content area and the sidebars equal height.</p>
<p><strong>Get The Source Code:&nbsp;&nbsp;</strong><a title="Download Source Code" href="downloads/equal_height_div.rar">Without Jquery</a>&nbsp;&nbsp;<a title="Download Source Code" href="downloads/equal_height_div_jquery.rar">With Jquery</a></p>
<p><strong>Step 1:</strong><br />
Create an index.html file for your layout. I create an example html layout for this article.</p>
<pre name="code" class="xhtml">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;title&gt;Equal Height Division Using Javascript and CSS&lt;/title&gt;
    &lt;link type="text/css" rel="stylesheet" href="style.css" /&gt;
    &lt;script type="text/javascript" language="javascript" src="javascript.js"&gt;
    &lt;/script&gt;
&lt;/head&gt;

&lt;body onload="grabtheclass()"&gt;
    &lt;div class="outer"&gt;
        &lt;div class="inner"&gt;
            &lt;div class="main"&gt;
                &lt;p&gt;The Main Content&lt;/p&gt;
                &lt;p&gt;The Main Content&lt;/p&gt;
                &lt;p&gt;The Main Content&lt;/p&gt;
                &lt;p&gt;The Main Content&lt;/p&gt;
                &lt;p&gt;The Main Content&lt;/p&gt;
            &lt;/div&gt;
            &lt;div class="sidebar"&gt;
                &lt;p&gt;The Sidebar Content&lt;/p&gt;
                &lt;p&gt;The Sidebar Content&lt;/p&gt;
                &lt;p&gt;The Sidebar Content&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p><span id="more-199"></span><br />
<strong>STEP 2:</strong><br />
Now we will create a stylesheet for the layout we just created. Let&#8217;s name it as style.css.</p>
<pre name="code" class="css">/*The Styles*/
p {
    text-align:center;
    font-family:Georgia;
    font-size:18px;
    font-variant:small-caps;
    color:#555555;
    padding:0;
    margin:20px 10px 20px 10px;
}
a {
    color:#FFFFFF;
    font-size:11px;
}
.outer {
    background:#FFFFFF;
    margin:0 auto;
    width:800px;
}
.inner {
    width:100%;
    float:left;
}
.main {
    background:#00A2FF;
    width:500px;
    float:left;
    border-right:1px solid #000;
    margin-left:-1px;
}
.sidebar {
    background:#EEEEEE;
    width:300px;
    float:right;
}</pre>
<p><strong>STEP 3:</strong><br />
Finally, here comes the javascript code that controls the height of the divisions and makes them equal height.</p>
<pre name="code" class="javascript">var DivOneClassArr = new Array();
var DivTwoClassArr = new Array();

function getElementsByClassName( ClassName1, ClassName2, obj ) {
    if ( obj.className == ClassName1 ) {
        DivOneClassArr[DivOneClassArr.length] = obj;
    }
    if ( obj.className == ClassName2 ) {
        DivTwoClassArr[DivTwoClassArr.length] = obj;
    }
    for ( var i = 0; i &lt; obj.childNodes.length; i++ )
        getElementsByClassName( ClassName1, ClassName2, obj.childNodes[i] );
}

function grabtheclass() {
    DivOneClassArr.length = 0;
    aryClassElements2.length = 0;
    getElementsByClassName( 'main', 'sidebar', document.body );
    for ( var i = 0; i &lt; DivOneClassArr.length || i &lt; DivTwoClassArr.length; i++ ) {
        if(DivOneClassArr[i].offsetHeight &gt; DivTwoClassArr[i].offsetHeight) {
            DivTwoClassArr[i].style.height = DivOneClassArr[i].offsetHeight +'px';
        }
        else if (DivOneClassArr[i].offsetHeight &lt; DivTwoClassArr[i].offsetHeight) {
            DivOneClassArr[i].style.height = DivTwoClassArr[i].offsetHeight +'px';
        }
    }
}</pre>
<p><strong>Analysis:</strong> This script calculate the height of the divisions taking their class name in the function <strong>grabtheclass()</strong>. The division with more height is selected by comparing the height of the two divisions. If division div1 for instance is having more height, then the other division say div2 with shorter height will be assigned a new height equal to the height of div1. This is what the code does to make them equal height.</p>
<p><a href="http://demo.chromicdesign.com/post/equal_height_div/" title="demo">Check the demo here</a></p>
<p><strong>STEP 4 (Optional):</strong><br />
This step is optional only. Here instead of using the above javascript code, jquery library is used. Then the code below can be used to get similar result. To use this javascript code, you need to import jquery library. You can get jquery library here <a title="Download Jquery Library" href="http://docs.jquery.com/Downloading_jQuery" target="_blank">Jquery.com</a>.</p>
<p>Add the jquery library file in the head section of your html.</p>
<pre name="code" class="xhtml">
&lt;script type="text/javascript" language="javascript" src="jquery-1.3.2.min.js"&gt;&lt;/script&gt;
</pre>
<p>and you can remove the onload attribute in the &lt;body&gt; since this is not necessary anymore if we use jquery.</p>
<p>Now create a file named javascript.js or use the one you created before and use the code below.</p>
<pre name="code" class="javascript">
$(document).ready(function(){
    if($('div.main').height() &gt; $('div.sidebar').height())
    {
        $('div.sidebar').css({
            'height':$('div.main').height()
        });
    }
    else if($('div.sidebar').height() &gt; $('div.main').height())
    {
        $('div.main').css({
            'height':$('div.sidebar').height()
        });
    }
});
</pre>
<p><a href="http://demo.chromicdesign.com/post/equal_height_div_jquery/" title="Demo with jquery">Check the demo here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.chromicdesign.com/2009/03/equal-height-division-using-javascript-and-css.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

