XML technologies / CSS / CSS Cookbook / General topics / Relative positioning

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to place an element relative to its original position in the document. Rather than displaying it at its default position in the flow, you want to shift it a certain distance up, down, left or right.

Solution

Again, use the property position, but this time with the value relative. Now use the values of top, left, bottom and right to specify how far the element should move from its original position.

The following CSS rule makes the image overlap the paragraph content in the next figure:

.relative {
  position: relative;
  top: 100px;
  left: 20px;
}

Relative positioning places an element relative to its original position in the document

Figure: Relative positioning places an element relative to its original position in the document flow.

Discussion

Unlike absolute positioning, relative positioning does not remove the element from the normal document flow. Instead, its actual position in the flow is the starting point. The example instructs the browser to move the image 20 pixels to the right and 100 pixels down from its original position. Had you used the value absolute, the element would instead be positioned relative to the containing block’s edges, without regard for its original position.

See also

The W3C 2.1 specification on relative positioning.

  

 

 

 

back to top