tags:

views:

78

answers:

4

I have this code that I don't really understand why it works:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
        <style type="text/css">    
             #divWraper
             {                              
                position: relative;
             }                          
             #divBig
             {      
                width: 500px;
                height: 300px;              
                background: Green;
                border: 1px dashed yellow;              
            }

            #divSmall
            {
            position: absolute;
                bottom: 0;
                left: 0px;
                width: 500px;
                height: 100px;
                background: Red;                              
                z-index: 200;         
                opacity: 0.5;
                filter: alpha(opacity = 50);                    
        }
        </style>

    </head>
    <body> 
      <div id="divWraper">       
        <div id="divBig"></div>    
        <div id="divSmall"></div>       
      </div>
    </body>
</html>

How does, this style makes the layout "work" (if I omit it, it brakes the layout):

#divWraper
{                              
     position: relative;
}

I thought this is just for positioning the outer div , but it also effects the children...

Please provide a clear explanation :)

+1  A: 

This is actually a css trick. It goes like this:

If you set the parent div position to relative and the child divs position to absolute then child divs will remain inside the parent div even though their position is absolute.

See: Absolute Positioning Inside Relative Positioning for more explanation.

Thanks

Sarfraz
+1  A: 

When you use position:absolute on an element, the top, left, right and bottom properties are based on the nearest positioned parent element, or to document body if there are none.

Take the following example:

<div id="parent" style="position:relative">
  <div>blah</div>
  <div id="wrapper">
    <div style="position:absolute">Here is some more content<div>
  </div>
</div>

The absolute positioned div here will be positioned relative to the div with the parent id, because that is the nearest positioned element. If I add style="position:relative" to the id="wrapper" div, it will be positioned relative to that div instead.

Andy E
+1  A: 

For the child divs to base their positioning relative to the parent div, the parent div must have positioning set to something other than static (which it is by default). Making the parent relative makes all children who are not fixed position themselves relative to the parent.

NickLarsen
A: 

absolute positioned element are relative to the next positioned parent element. If none of the parent elements are positioned than relative to the body tag, so if you take out the position: relative, the div.small is positioned relative to the body with bottom: 0

harpax