CSS Position: static, relative, fixed, absolute

By Xah Lee. Date: . Last updated: .

CSS has a position property that can be used to:

The position property has 4 possible values:

div {position:static}

div {position:fixed}

div {position:relative}

div {position:absolute}
position ValueOwn LayerRelative To
staticno(Normal Flow) This is default.
relativenoRelative to its normal position
fixedyesRelative to window
absoluteyesRelative to its containing block. That is, the first parent that's not positioned static. If none found, then it's <html> tag.

To specify the offset, use the following:

Example:

div.x89647 {
position:fixed;
top: 1px;
left: 1px;
}

position:static

The position:static is the default for all elements.

The position of element of position:static is either below the previous element, or to the right of the previous element.

If the previous element is a block element, then next element is positioned below it. (block elements are div, p, img, h1 etc.)

If the previous element is a inline element, then next element is positioned to the right of it (or below if container box width is reached). (inline elements are span, a (link), b (bold), etc.)

position:relative

Use position:relative to adjust a element's position relative to its normal position.

AAA
BBB with relative position
CCC
<div id="box38155">
<div>AAA</div>
<div id="offset79397">BBB with relative position</div>
<div>CCC</div>
</div>
#box38155 {
 border:solid thin blue;
 width:200px;
}

#offset79397 {
 position:relative;
 left:10px;
 border:solid thin red;
}

More detail, see

CSS: Relative Position

position:fixed

Use position:fixed to specify the positioning of a element with respect to the window.

When a element has position:fixed, that element goes into its own layer. The normal flow of elements will flow as if that element doesn't exist.

Example:

CSS: Fix Element to Window

position:absolute

position:absolute make the element go into its own layer.

position:absolute's offset is relative to its “containing block”.

A “containing block” is effectively the first parent element that has a position value other than “static”. When no parent has any of “position” spec, then it is relative to the <html> element.

Example:

Overlapping and z-index

When a element goes into a layer, it may overlap with other elements. To control which goes on top, you can use the attribute “z-index”. Like this:

z-index:3

[see CSS: z-index]

Layer Example

For practical examples of using layers, see:

Reference

Visual formatting model#propdef-position

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog