CSS Positioning Techniques for Nested DIVs

Just Posted: Apple Likely to Give Education Rather than Resolution for iPhone 4 Antenna Issues

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

step-1

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;

step-2

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;
}

step-3

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…

Learning jQuery 1.3

Learning jQuery 1.3

CSS - The Missing Manual

CSS - The Missing Manual

jQuery UI 1.6

jQuery UI 1.6

10 Responses to “CSS Positioning Techniques for Nested DIVs”

  1. Rob W says:

    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.

  2. Steve Reynolds says:

    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.

  3. Yavor says:

    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!

  4. Carolize Jansen says:

    “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.

  5. Carolize Jansen says:

    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!

  6. Jamie says:

    This saved me so much time. Thank you.

  7. Jason says:

    This saved me a lot of time and frustration on a project I am working on! Thanks for posting this timely advice!

Trackbacks/Pingbacks

  1. [...] CSS Positioning Techniques for Nested DIVs | Steve Reynolds Blog [...]

  2. [...] CSS Positioning Techniques for Nested DIVs | Steve Reynolds Blog Nice CSS Technique for nested div positioning (tags: webdesign css layout technique div) [...]

  3. [...] CSS Positioning Techniques for Nested DIVs [...]

Leave a Reply