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 “Back to Basics” tutorial below, I will describe how CSS is rendered using the “position” attribute, and the pitfalls of forgetting to use it.
HTML Setup
The HTML code has been setup as simply as possible. A “container” DIV which holds within it another DIV called “rectangle”.
<div id=”container”>
<div id=”rectangle”></div>
</div>
Nested DIVs – No Positioning

As you can see, the “rectangle” DIV is positioned relative to its container DIV. If you added a margin around the “container” DIV for example, the “rectangle” DIV would reposition itself as well – in the same spot as it is currently in.
#container
{
border: 3px solid #000000;
width: 430px;
height: 200px;
margin: 50px;
}
#rectangle
{
background-color: #000000;
width: 300px;
height: 150px;
}
Adding Positioning
In the previous section of code, the “rectangle” DIV was positioned relative to its container DIV. We didn’t specify any “position” attribute in the CSS, it is assumed that it should render inline. Now we’re going to add a position:absolute; attribute to the “rectangle” DIV and tell it to position a little more specifically at left: 10px;

What happens now is the “rectangle” DIV no longer sees that its container is valid. It actually positions itself left:10px from the page, not the DIV it sits in. There is one last step we need to take:
#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;
}

Now we’ve added a position:relative attribute to the “container” DIV, and hey presto! the “rectangle” now realises it should adhere to the rules and limits of its container (as best it can)… You can also use position:relative within the “rectangle” DIV if you so wished, you’d get the same result – however it all depends on what you want to achieve…
Round Up
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’s head against wall time
Tools to help you learn…
|
|
|
|
Similar Articles:
Text Embossing Technique With CSS
Custom 404 with jQuery and jQuery UI
Tutorial: From PHP to XML to jQuery and Ajax
Using jQuery and Ajax To Create PHP Sessions
From MySQL to jQuery, via PHP, XML & Ajax












It’s important to point out here that setting position:relative on the “container” div is *not* declaring anything about the divs inside it, it just changes the container div’s positioning method. It’s just very confusing that the default position has some limitations to it….
The default is “position: static”, which just means within the flow of the page… and it ignores “left”/”right”/”top”/”bottom” styles, PLUS those styles won’t work in reference to it either (so the rectangle div can’t use it as a point of reference).
Changing the container div to anything other than “position:static” fixes the problem — position fixed or absolute will work just as well, since those are all positioning systems that respect left/right/etc., for the element itself and sub elements.
Yupp absolutely agree. Setting the container DIV to position:relative actually isn’t meant to affect anything inside it! Like you say, the default position really is probably the wrong candidate to have the “default” badge. I would have expected (and assumed in the early days) that :relative was the default.
Hi Rob W,
I was looking for such answer, I have always been wondering why position: relative fixes this problem but now I know, thanks!
“This issue flummoxed me for hours when I began developing with Nested DIV layouts,” with which I fully (and tediously) relate, “yet it was so simple to fix.” Yes – when I finally came upon your explanation.
Thank you very much.
And I followed your advice to set the container div’s position to relative everywhere on the page – and it fixed other problems I’d had for a while, too!
This saved me so much time. Thank you.
This saved me a lot of time and frustration on a project I am working on! Thanks for posting this timely advice!