O'Reilly logo

HTML & CSS: The Good Parts by Ben Henick

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required