CSS Element Positioning - Static & Relative

Position is perhaps the most important of all CSS properties since it determines the layout of page elements. It can have one of four values:


<html>
   <head>
      <style>
         .posS{
            background-color:blue;
            width:4em;
            height:4em}
         .posR1{
            position:relative;
            background-color:red;
            left:3em;
            top:-1em;
            z-index:1}      
         .posR2{
            position:relative;
            background-color:green;
            left:6em;
            top:-8em}
         .posR3{
            position:relative;
            background-color:gray;
            left:9em;
            top:-9em;
            opacity:0.5;
            filter:alpha(opacity=50)}      
      </style>
   </head>
   <body>
       <div class="posS"></div>
       <div class="posS posR1"></div>
       <div class="posS posR2"></div>
       <div class="posS posR3"></div>      
    </body>
<html>

It should be noted that since positioning of relative boxes is done under normal flow rules the calculated height requirement for the container is excessive. This can lead to the browser displaying an unrequired vertical scrollbar if the overflow:auto CSS property is specified for the containing box. As is often the case, IE turns out to be the most recalcitrant browser. In the present example it ignores the style settings of height:9em; and overflow:none and insists on drawing a container box that is 16em in height. Another instance of deviant behavior is when the text-align:center or text-align:right property settings are applied to the style of the container box. Strictly speaking the text alignment property should be applied only to contained text, not to child boxes. IE insists on doing so with the result that relatively positioned child boxes end up with the wrong placement.

Jump To...

Colophon