CSS Positioning Properties

The position property takes one of four values (of which static is the default) and allows the stylist to place any element anywhere in the layout.

More importantly, the assignment of any position value other than static alters the positioning context of elements, which is briefly described in Chapter 3.

How Positioning Works

Consider the following values:

#someDiv { ... left: 160px; top: 96px; }

Suppose that those styles are applied to the following markup:

... <body><div id="main"> ... <div id="someDiv">The quick
  red fox jumps over the lazy brown dog.</div> ... </div></body>

The four position values that can be applied to #someDiv will yield the following results:

static

left and top values are not applied; the element retains its expected position in the layout flow of the document.

absolute

The upper-left corner of #someDiv appears 96 pixels below and 160 pixels to the right of the upper-left corner of the browser canvas. The margin applied to body is disregarded. The element is removed from the layout flow of the document.

fixed

Yields the same result as position: absolute, except that the element will retain its position on the browser canvas regardless of any content scrolling. The element is removed from the layout flow of the document.

relative

Instead of being offset from the upper-left corner of the browser canvas, #someDiv is offset from where it would normally appear in the document layout. The element retains its expected position in the layout flow of ...

Get HTML & CSS: The Good Parts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.