<?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>Garrett Murphey &#187; Cascading Style Sheets</title>
	<atom:link href="http://gmurphey.com/category/cascading-style-sheets/feed/" rel="self" type="application/rss+xml" />
	<link>http://gmurphey.com</link>
	<description>New Media Interactive Developer</description>
	<lastBuildDate>Fri, 23 Apr 2010 02:02:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>My Ideal Cascading Style Sheet: Organization</title>
		<link>http://gmurphey.com/2007/03/12/my-ideal-cascading-style-sheet-organization/</link>
		<comments>http://gmurphey.com/2007/03/12/my-ideal-cascading-style-sheet-organization/#comments</comments>
		<pubDate>Mon, 12 Mar 2007 05:13:37 +0000</pubDate>
		<dc:creator>Garrett Murphey</dc:creator>
				<category><![CDATA[Cascading Style Sheets]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://gmurphey.com/2007/03/12/my-ideal-cascading-style-sheet-organization/</guid>
		<description><![CDATA[I'm not the most organized person and I'm not afraid to admit it. It's one of the things that has drawn me to designing with HTML and programming with  Javascript and PHP. With HTML things are designed to fit into a hierarchy of nested tags, and most programming languages are organized by functions and methods. There's inherent organization. But one crucial piece of the web developer's toolkit is missing syntactical design &#8212; that being Cascading Style Sheets (CSS).]]></description>
			<content:encoded><![CDATA[<p>I am not the most organized person and I&#8217;m not afraid to admit it. It&#8217;s one of the things that has drawn me to designing with HTML and programming with  Javascript and PHP. With HTML things are designed to fit into a hierarchy of nested tags, and most programming languages are organized by functions and methods. There&#8217;s inherent organization. But one crucial piece of the web developer&#8217;s toolkit is missing syntactical design &mdash; that being Cascading Style Sheets (CSS).</p>
<p>Here&#8217;s how I write my most of my CSS now.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#menu</span> ul <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> ul li <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.2em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.2em</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> ul li a <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#006699</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> ul li a<span style="color: #6666ff;">.here</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#999999</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Now that&#8217;s not very hard to read, but amongst hundreds, if not thousands, of other lines of CSS, it&#8217;s quite easy to lose track of those 18 lines. And I, being the unorganized designer I am, tend to throw lines of CSS in the middle rules that should otherwise be grouped together. And those relationships are quite important when we need to debug a design issue caused by a child inheriting styles from a parent. The CSS syntax really lends nothing to natural organization.</p>
<p>One way I&#8217;ve tried to become a more efficient with the current CSS model is through rule and style separation by files. For instance, I may create files like design.css, typography.css, etc. But then I run into scenarios where I can&#8217;t decide which files to keep certain rules in, since they may apply to both, and eventually that system collapses upon itself.</p>
<p>I&#8217;ve thought long and hard about how CSS can improve (and hopefully make us more productive). One idea is illustrated below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#menu</span> <span style="color: #00AA00;">&#123;</span>
	ul <span style="color: #00AA00;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
		<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
		<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
		li <span style="color: #00AA00;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.2em</span><span style="color: #00AA00;">;</span>
			<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.2em</span><span style="color: #00AA00;">;</span>
		<span style="color: #00AA00;">&#125;</span>
	<span style="color: #00AA00;">&#125;</span>
	a <span style="color: #00AA00;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#06699</span><span style="color: #00AA00;">;</span>
		<span style="color: #6666ff;">.here</span> <span style="color: #00AA00;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#999999</span><span style="color: #00AA00;">;</span>
		<span style="color: #00AA00;">&#125;</span>
	<span style="color: #00AA00;">&#125;</span>	
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, I&#8217;ve nested my styles within parents to show relationships more efficiently. Nothing can come between any of the rules for the <code>#menu</code> element.</p>
<p>Another way this solution could help is by making it much easier to collaborate. My colleagues rarely organize their CSS files exactly like mine, and when we&#8217;re trading these files back and forth, there&#8217;s a lot of time spent familiarizing ourselves with each other&#8217;s personal style. I&#8217;m not saying that a syntax like this would completely cut out a &#8220;familiarization period&#8221;, but it would definitely reduce it by creating a rule-grouping standard.</p>
<p>I&#8217;m curious to know how you think CSS could be made more efficient and cleaner. It&#8217;s definitely a subject I want to discuss more.</p>
]]></content:encoded>
			<wfw:commentRss>http://gmurphey.com/2007/03/12/my-ideal-cascading-style-sheet-organization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Vertical Alignment with Cascading Style Sheets</title>
		<link>http://gmurphey.com/2006/09/27/vertical-alignment-with-cascading-stylesheets/</link>
		<comments>http://gmurphey.com/2006/09/27/vertical-alignment-with-cascading-stylesheets/#comments</comments>
		<pubDate>Thu, 28 Sep 2006 03:59:11 +0000</pubDate>
		<dc:creator>Garrett Murphey</dc:creator>
				<category><![CDATA[Cascading Style Sheets]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://gmurphey.com/2006/09/27/vertical-alignment-with-cascading-stylesheets/</guid>
		<description><![CDATA[It's not often that you'll hear me say I miss something about designing with tables. Cascading stylesheets have made my markup cleaner, slimmer, and much easier to maintain. And, for the most part, designing with CSS has been a blast. I can't believe I actually resisted moving to CSS for a while. But, there's one attribute from the good old <code>td</code> tag that I sorely miss -- <code>valign</code>. I used to find it very annoying that <code>valign</code> would always default to <code>middle</code> and that, for the most part, I'd have to go through every table cell and set <code>valign="top"</code>. Now that I've moved to a complete CSS workflow, there's no <code>valign</code> and, to be honest, I kind of missing the little guy. So, I decided to bring the power of <code>valign</code> to the twenty-first century.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not often that you&#8217;ll hear me say I miss something about designing with tables. Cascading style sheets have made my markup cleaner, slimmer, and much easier to maintain. And, for the most part, designing with CSS has been a blast. I can&#8217;t believe I actually resisted moving to CSS for a while. But, there&#8217;s one attribute from the good old <code>td</code> tag that I sorely miss &#8212; <code>valign</code>. I used to find it very annoying that <code>valign</code> would always default to <code>middle</code> and that, for the most part, I&#8217;d have to go through every table cell and set <code>valign="top"</code>. Now that I&#8217;ve moved to a complete CSS workflow, there&#8217;s no <code>valign</code> and, to be honest, I kind of missing the little guy. So, I decided to bring the power of <code>valign</code> to the twenty-first century.</p>
<p>It&#8217;s actually not as hard as you might think to set vertical alignment in CSS. Say we had some HTML that looked like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
	&lt;body&gt;
		&lt;div id=&quot;container&quot;&gt;
			&lt;div id=&quot;element&quot;&gt;valign me&lt;/div&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>And that HTML was styled like so.</p>

<div class="wp_syntax"><div class="code"><pre class="1" style="font-family:monospace;">#container {
	width: 410px;
	height: 50px;
	padding: 0 10px;
	background-color: #CCCCCC;
}
&nbsp;
#element {
	width: 150px;
	height: 20px;
	line-height: 20px;
}</pre></div></div>

<p>We&#8217;d get something that looks like this, right?</p>
<p><img id="image15" src="http://gmurphey.com/wp-content/uploads/2006/09/align_noscript.gif" alt="align_noscript.gif" /></p>
<p>That was a little anticlimactic. Let&#8217;s try that again with a few more styles.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#container</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">410px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#CCCCCC</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#element</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-10px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">150px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><img id="image16" src="http://gmurphey.com/wp-content/uploads/2006/09/align.gif" alt="align.gif" /></p>
<p>That&#8217;ll do it. See all that positioning going on in the stylesheet? If anyone doesn&#8217;t know, positioning an element absolutely within a relatively positioned container will position the element based on the container. Basically what&#8217;s happening is that we&#8217;re telling <code>element</code> to position itself halfway down from the top (<code>top: 50%</code>) of <code>container</code>. Where did the <code>margin-top: -10px</code> come from, though? To align the horizontal centers of the container and element, we have to pull the element up half of its height (<code>20px / 2 = 10px</code>).</p>
<p>And there you have it &#8212; a pretty painless method of vertically centering an element. But wait, there&#8217;s more. Let&#8217;s make our lives a bit easier.</p>
<h3>A Dynamic Solution (Javascript)</h3>
<p>Don&#8217;t worry, I hear you. You don&#8217;t want to have to calculate the margins for each individual element you want vertically align. Or there may be cases where you&#8217;re attempting to load content dynamically and you really have no way of to calculate the margin yourself. I ran into this problem a few times with both my gallery and slideshow applications (coming soon, I promise). The result was <a href="http://gmurphey.com/projects/js/align/align.js">a javascript align method I wrote</a>. It wasn&#8217;t always so complex. Until yesterday, it could only vertically and horizontally center. But now (with a little work on the original method) we can do things like the following.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// vertically center the element within its container</span>
align<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> vAlign<span style="color: #339933;">:</span> <span style="color: #3366CC;">'middle'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// horizontally align to the right and center vertically</span>
align<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> hAlign<span style="color: #339933;">:</span> <span style="color: #3366CC;">'right'</span><span style="color: #339933;">,</span> vAlign<span style="color: #339933;">:</span> <span style="color: #3366CC;">'middle'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// horizontally center and vertically align to the bottom</span>
align<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> hAlign<span style="color: #339933;">:</span> <span style="color: #3366CC;">'center'</span><span style="color: #339933;">,</span> vAlign<span style="color: #339933;">:</span> <span style="color: #3366CC;">'bottom'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The method takes every combination you can think of. If you&#8217;d like some help constructing the method with parameters, head over to the <a href="http://gmurphey.com/projects/js/align/sandbox.html" target="_blank">sandbox</a>. </p>
<h3>Resources</h3>
<p>The script in this post uses Sam Stephenson&#8217;s <a href="http://prototype.conio.net/" target="_blank">Prototype</a> library. There&#8217;s some very good documentation for Prototype written by <a href="http://www.sergiopereira.com/articles/prototype.js.html" target="_blank">Sergio Pereira</a> and over at <a href="http://wiki.script.aculo.us/scriptaculous/show/Prototype" target="_blank">script.aculo.us</a> (another great javascript library).</p>
<h3>Download the Source</h3>
<p>All source code is provided under the <a href="http://creativecommons.org/licenses/by-sa/2.5/" target="_blank">Creative Commons Attribution-Sharealike License</a>. If you agree to these terms, please <a href="http://gmurphey.com/projects/js/align/align.js">view and download the entire source now</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gmurphey.com/2006/09/27/vertical-alignment-with-cascading-stylesheets/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Distributing Elements Within a Container</title>
		<link>http://gmurphey.com/2006/09/06/distributing-elements-within-a-container/</link>
		<comments>http://gmurphey.com/2006/09/06/distributing-elements-within-a-container/#comments</comments>
		<pubDate>Wed, 06 Sep 2006 05:15:41 +0000</pubDate>
		<dc:creator>Garrett Murphey</dc:creator>
				<category><![CDATA[Cascading Style Sheets]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://gmurphey.com/2006/09/04/distributing-elements-within-a-container/</guid>
		<description><![CDATA[One aspect of HTML and CSS I've always felt is lacking is the ability to distribute elements evenly within its container. Using the CSS rule <code>text-align:'justify'</code>, you are able to justify lines of text within their container, but there's really no equivalent for non-text elements.]]></description>
			<content:encoded><![CDATA[<p>One aspect of HTML and CSS I&#8217;ve always felt is lacking is the ability to distribute elements evenly within its container. Using the CSS rule <code>text-align:'justify'</code>, you are able to justify lines of text within their container, but there&#8217;s really no equivalent for non-text elements.</p>
<p>This became a problem a few weeks ago when a client requested that their site menu span the entire width of the page. Before discovering CSS, I would accomplish this using tables and images. But since then, I&#8217;ve learned that images are quite inaccessible and can be very difficult to maintain when the menu changes, and since this site was being built using CSS, I wanted to keep tables out of the design as much as possible. So, for the time being, I created something like the following.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
	&lt;body&gt;
		&lt;ul id=&quot;container&quot;&gt;
			&lt;li&gt;Home&lt;/li&gt;
			&lt;li&gt;About Us&lt;/li&gt;
			&lt;li&gt;Services&lt;/li&gt;
			&lt;li&gt;Resources&lt;/li&gt;
			&lt;li&gt;Contact Us&lt;/li&gt;
		&lt;/ul&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><img id="image12" src="http://gmurphey.com/wp-content/uploads/2006/09/distribute_noscript.gif" alt="distribute_noscript.gif" /></p>
<p>That was all well and good, but it wasn&#8217;t going to fly with the client.</p>
<p>After digging through CSS documentation and finding little that addresses this problem, I decided the best way to accomplish this effect was to use javascript to dynamically generate CSS. I&#8217;m hoping a feature is added to CSS soon since I&#8217;m not a big fan of using javascript for design.</p>
<p>While scripting this method, I made a list of characteristics I&#8217;d like to see it have.</p>
<ol>
<li>Utility</li>
<li>Scalability</li>
<li>Simplicity</li>
</ol>
<p>With those goals in mind, I came up with this frame for the method.</p>
<p><code>distribute(container, elements, [direction = 'horizontal', className = null])</code></p>
<p><strong><em>Container</em> (required)</strong><br />
The id attribute or HTML object of the container.</p>
<p><strong><em>Elements</em> (required)</strong><br />
The tag name of the element(s) you want to distribute within <code>container</code> (e.g. &#8216;li&#8217;, &#8216;img&#8217;, &#8216;div&#8217;).</p>
<p><strong><em>Direction</em> (optional)</strong><br />
The direction you wish to distribute <code>elements</code>. This can either be &#8216;horizontal&#8217; (default) or &#8216;vertical&#8217;.</p>
<p><strong><em>ClassName</em> (optional)</strong><br />
If you have multiple instances of <code>elements</code> and only want to distribute certain <code>elements</code>, you may attach a class to those tags and pass that to the method. This property is <code>null</code> by default.</p>
<p>Let&#8217;s take a look at the script (the following script was written with Prototype).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> containerDimen <span style="color: #339933;">=</span> Element.<span style="color: #660066;">getDimensions</span><span style="color: #009900;">&#40;</span>container<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> $A<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>container<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span>elements<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'vertical'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> containerHeight <span style="color: #339933;">=</span> containerDimen.<span style="color: #660066;">height</span><span style="color: #339933;">;</span>
	containerHeight <span style="color: #339933;">-=</span> 
	<span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>Element.<span style="color: #660066;">getStyle</span><span style="color: #009900;">&#40;</span>container<span style="color: #339933;">,</span> <span style="color: #3366CC;">'padding-top'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #339933;">+</span> parseInt<span style="color: #009900;">&#40;</span>Element.<span style="color: #660066;">getStyle</span><span style="color: #009900;">&#40;</span>container<span style="color: #339933;">,</span> <span style="color: #3366CC;">'padding-bottom'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> containerWidth <span style="color: #339933;">=</span> containerDimen.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>
	containerWidth <span style="color: #339933;">-=</span> 
	<span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>Element.<span style="color: #660066;">getStyle</span><span style="color: #009900;">&#40;</span>container<span style="color: #339933;">,</span> <span style="color: #3366CC;">'padding-left'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #339933;">+</span> parseInt<span style="color: #009900;">&#40;</span>Element.<span style="color: #660066;">getStyle</span><span style="color: #009900;">&#40;</span>container<span style="color: #339933;">,</span> <span style="color: #3366CC;">'padding-right'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The first thing we need to do is collect all <code>elements</code> within the <code>container</code>. We&#8217;re using the <code>$A()</code> method to make the results <code>Enumberable</code> so we can run them through an <code>each()</code> loop a little later. After we&#8217;ve gathered all the <code>elements</code>, we need to calculate the container width or height (depending whether <code>direction</code> is set for &#8216;horizontal&#8217; or &#8216;vertical&#8217;). Since the box model calculates width as the sum of width and padding, we have to subtract the padding from the width to get the real width of the container.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elementWidth <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> elementHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
element.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>className <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>className <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>Element.<span style="color: #660066;">hasClassName</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> className<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		i<span style="color: #339933;">++;</span>
		<span style="color: #003366; font-weight: bold;">var</span> elemDimen <span style="color: #339933;">=</span> Element.<span style="color: #660066;">getDimensions</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'vertical'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			elementHeight <span style="color: #339933;">+=</span> elemDimen.<span style="color: #660066;">height</span><span style="color: #339933;">;</span>
			Element.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> 
				marginTop<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> 
				marginBottom<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> 
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			elementWidth <span style="color: #339933;">+=</span> elemDimen.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>
			Element.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> 
				marginLeft<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> 
				marginRight<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> 
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now that we have the <code>container</code> width (or height), we need to calculate the total width (<code>elementWidth</code>) of all the <code>elements</code>. If there were any margins applied to the <code>elements</code> for a noscript version, we remove them. <code>i</code> is used to count how many elements match the search criteria since <code>className</code> may or may not be null.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'vertical'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #003366; font-weight: bold;">var</span> leftover <span style="color: #339933;">=</span> containerHeight <span style="color: #339933;">-</span> elementHeight<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">else</span>
	<span style="color: #003366; font-weight: bold;">var</span> leftover <span style="color: #339933;">=</span> containerWidth <span style="color: #339933;">-</span> elementWidth<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> marginNum <span style="color: #339933;">=</span> i <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> margin <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>leftover <span style="color: #339933;">/</span> marginNum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
element.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>className <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>className <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>Element.<span style="color: #660066;">hasClassName</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> className<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'vertical'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e <span style="color: #339933;">!=</span> element<span style="color: #009900;">&#91;</span>element.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
				Element.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
					marginBottom<span style="color: #339933;">:</span> margin <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e <span style="color: #339933;">!=</span> element<span style="color: #009900;">&#91;</span>element.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
				Element.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
					marginRight<span style="color: #339933;">:</span> margin <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We&#8217;re just about done! With the <code>elements</code> and <code>container</code> width calculated, we figure out how much space is <code>leftover</code>. The <code>leftover</code> space is divided by <code>i</code> minus one (we&#8217;ll only be applying margins to the right side of elements) to figure out how much space should be between each element. All that&#8217;s left to do after that is to place those margins on each of the <code>elements</code>.</p>
<p>With the javascript written, all that&#8217;s left to do is make a little modification to the HTML.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
	&lt;body onload=&quot;distribute('container', 'li')&quot;&gt;
		&lt;ul id=&quot;container&quot;&gt;
			&lt;li&gt;Home&lt;/li&gt;
			&lt;li&gt;About Us&lt;/li&gt;
			&lt;li&gt;Services&lt;/li&gt;
			&lt;li&gt;Resources&lt;/li&gt;
			&lt;li&gt;Contact Us&lt;/li&gt;
		&lt;/ul&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><img id="image13" src="http://gmurphey.com/wp-content/uploads/2006/09/distribute.gif" alt="distribute.gif" /></p>
<p>That&#8217;s it. It&#8217;s probably a good idea to add default margins to the <code>elements</code> for users with javascript disabled (any default margins get removed by the script). Enjoy!</p>
<h3>Resources</h3>
<p>The script in this post uses Sam Stephenson&#8217;s <a href="http://prototype.conio.net/" target="_blank">Prototype</a> library. There&#8217;s some very good documentation for Prototype written by <a href="http://www.sergiopereira.com/articles/prototype.js.html" target="_blank">Sergio Pereira</a> and over at <a href="http://wiki.script.aculo.us/scriptaculous/show/Prototype" target="_blank">script.aculo.us</a> (another great javascript library).</p>
<h3>Download the Source</h3>
<p>All source code is provided under the <a href="http://creativecommons.org/licenses/by-sa/2.5/">Creative Commons Attribution-Sharealike License</a>. If you agree to these terms, please <a href="http://gmurphey.com/projects/js/distribute/distribute.js">view and download the entire source now</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gmurphey.com/2006/09/06/distributing-elements-within-a-container/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
