<?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>Steve Reynolds Blog &#187; DIV</title>
	<atom:link href="http://www.reynoldsftw.com/tag/div/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reynoldsftw.com</link>
	<description>Being Generalist.</description>
	<lastBuildDate>Fri, 03 Sep 2010 19:00:35 +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>CSS Positioning Techniques for Nested DIVs</title>
		<link>http://www.reynoldsftw.com/2009/03/css-positioning-techniques-for-nested-divs/</link>
		<comments>http://www.reynoldsftw.com/2009/03/css-positioning-techniques-for-nested-divs/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 19:59:42 +0000</pubDate>
		<dc:creator>Steve Reynolds</dc:creator>
				<category><![CDATA[Back to Basics]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[DIV]]></category>

		<guid isPermaLink="false">http://www.reynoldsftw.com/?p=1029</guid>
		<description><![CDATA[Positioning elements with CSS can be quite tricky to the untrained user. To be honest, a lot of CSS ends up being trial and error simply because what works in one browser, probably looks different in another (thank you IE!). Setting up page layouts with nested DIVs can be easy, but you need to make [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Fcss-positioning-techniques-for-nested-divs%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Fcss-positioning-techniques-for-nested-divs%2F&amp;source=SteveReynolds&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Positioning elements with CSS can be quite tricky to the untrained user. To be honest, a lot of CSS ends up being trial and error simply because what works in one browser, probably looks different in another (thank you IE!). Setting up page layouts with nested DIVs can be easy, but you need to make sure you specify the right attributes at the right time to get the desired results. In the quick <strong>&#8220;Back to Basics&#8221;</strong> tutorial below, I will describe how CSS is rendered using the &#8220;position&#8221; attribute, and the pitfalls of forgetting to use it.</p>
<h3>HTML Setup</h3>
<p>The HTML code has been setup as simply as possible. A &#8220;container&#8221; DIV which holds within it another DIV called &#8220;rectangle&#8221;.</p>
<p>&lt;div id=&#8221;container&#8221;&gt;</p>
<p>&lt;div id=&#8221;rectangle&#8221;&gt;&lt;/div&gt;</p>
<p>&lt;/div&gt;</p>
<h3>Nested DIVs &#8211; No Positioning</h3>
<p><img class="aligncenter size-full wp-image-1026" title="step-1" src="http://www.reynoldsftw.com/wp-content/uploads/2009/03/step-1.png" alt="step-1" width="440" height="210" /></p>
<p>As you can see, the &#8220;rectangle&#8221; DIV is positioned relative to its container DIV. If you added a margin around the &#8220;container&#8221; DIV for example, the &#8220;rectangle&#8221; DIV would reposition itself as well &#8211; in the same spot as it is currently in.</p>
<pre class="brush: css;">#container
{
border: 3px solid #000000;
width: 430px;
height: 200px;
margin: 50px;
}

#rectangle
{
background-color: #000000;
width: 300px;
height: 150px;
}</pre>
<p><center><script type="text/javascript"><!--
google_ad_client = "pub-6415056921376217";
google_ad_channel = "in-ad-unit-hor";
google_ui_features = "rc:0";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0066CC";
google_color_text = "";
google_color_url = "0066CC";

//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</center></p>
<h3>Adding Positioning</h3>
<p>In the previous section of code, the &#8220;rectangle&#8221; DIV was positioned relative to its container DIV. We didn&#8217;t specify any <em>&#8220;position&#8221;</em> attribute in the CSS, it is assumed that it should render inline. Now we&#8217;re going to add a <em>position:absolute;</em> attribute to the &#8220;rectangle&#8221; DIV and tell it to position a little more specifically at <em>left: 10px;</em></p>
<p><em><img class="aligncenter size-full wp-image-1027" title="step-2" src="http://www.reynoldsftw.com/wp-content/uploads/2009/03/step-2.png" alt="step-2" width="489" height="211" /></em></p>
<p>What happens now is the &#8220;rectangle&#8221; DIV no longer sees that its container is valid. It actually positions itself <em>left:10px</em> from the page, not the DIV it sits in. There is one last step we need to take:</p>
<pre class="brush: css;">#container
{
border: 3px solid #000000;
width: 430px;
height: 200px;
margin: 50px;
position: relative;
}

#rectangle
{
background-color: #000000;
width: 300px;
height: 150px;
position: absolute;
left: 10px;
}</pre>
<p><img class="aligncenter size-full wp-image-1028" title="step-3" src="http://www.reynoldsftw.com/wp-content/uploads/2009/03/step-3.png" alt="step-3" width="440" height="210" /></p>
<p>Now we&#8217;ve added a <em>position:relative</em> attribute to the &#8220;container&#8221; DIV, and hey presto! the &#8220;rectangle&#8221; now realises it should adhere to the rules and limits of its container (as best it can)&#8230; You can also use <em>position:relative</em> within the &#8220;rectangle&#8221; DIV if you so wished, you&#8217;d get the same result &#8211; however it all depends on what you want to achieve&#8230;</p>
<h3>Round Up</h3>
<p>This issue flummoxed me for hours when I began developing with Nested DIV layouts, yet it was so simple to fix. Always remember to set the parent DIV position to relative if you want to position DIVs inside is absolutely, otherwise, it&#8217;s head against wall time <img src='http://www.reynoldsftw.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Tools to help you learn&#8230;</h3>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<thead></thead>
<tbody>
<tr>
<td width="33%" align="center" valign="top">
<div id="attachment_613" class="wp-caption aligncenter" style="width: 111px"><a href="http://www.amazon.com/gp/product/1847196705?ie=UTF8&amp;tag=stereyblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1847196705"><img class="size-full wp-image-613" title="jquery13" src="http://www.reynoldsftw.com/wp-content/uploads/2009/02/jquery13.jpg" alt="Learning jQuery 1.3" width="101" height="132" /></a><p class="wp-caption-text">Learning jQuery 1.3</p></div>
<p><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=stereyblo-20&amp;l=as2&amp;o=1&amp;a=1847196705" border="0" alt="" width="1" height="1" /></td>
<td width="33%" align="center" valign="top">
<div id="attachment_1030" class="wp-caption aligncenter" style="width: 111px"><a href="http://www.amazon.com/gp/product/0596526873?ie=UTF8&amp;tag=stereyblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596526873"><img class="size-full wp-image-1030" title="css-missing-manual" src="http://www.reynoldsftw.com/wp-content/uploads/2009/03/css-missing-manual.jpeg" alt="CSS - The Missing Manual" width="101" height="132" /></a><p class="wp-caption-text">CSS - The Missing Manual</p></div>
<p><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=stereyblo-20&amp;l=as2&amp;o=1&amp;a=0596526873" border="0" alt="" width="1" height="1" /></td>
<td width="33%" align="center" valign="top">
<div id="attachment_612" class="wp-caption aligncenter" style="width: 111px"><a href="http://www.amazon.com/gp/product/1847195121?ie=UTF8&amp;tag=stereyblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1847195121"><img class="size-full wp-image-612" title="jquery-ui" src="http://www.reynoldsftw.com/wp-content/uploads/2009/02/jquery-ui.jpg" alt="jQuery UI 1.6" width="101" height="132" /></a><p class="wp-caption-text">jQuery UI 1.6</p></div>
<p><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=stereyblo-20&amp;l=as2&amp;o=1&amp;a=1847195121" border="0" alt="" width="1" height="1" /></td>
</tr>
</tbody>
</table>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Fcss-positioning-techniques-for-nested-divs%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.reynoldsftw.com%2F2009%2F03%2Fcss-positioning-techniques-for-nested-divs%2F&amp;source=SteveReynolds&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.reynoldsftw.com/2009/03/css-positioning-techniques-for-nested-divs/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
