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

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

CSS Basics

  1. Basics
  2. Tag Matching Tutorial
  3. Selector Syntax
  4. Units

Color

  1. Color Syntax
  2. Color Names
  3. Opacity
  4. Linear Gradient
  5. Radial Gradient

Font

  1. font-family
  2. Standard Web Fonts
  3. font-size
  4. Meaning of Font Size
  5. Font Weight
  6. Chinese Font Names

Box Model

  1. Border
  2. Outline
  3. Margin vs Padding
  4. Box Sizing

Layout

  1. Layout Tutorial
  2. Display Property
  3. Position: static, relative, fixed, absolute
  4. Pure CSS Table
  5. Tabbed Menu
  6. 3 Columns Page Layout
  7. Multi-Column Layout for Text Flow
  8. Fix Element to Window
  9. Centering
  10. Flowing List
  11. z-index
  12. Text Over Image
  13. Text Flow Around Image
  14. OverFlow, Scrollbar

Transform/Animation

  1. 2D Transform
  2. Transition
  3. Animation

Line Wrap, Justification

  1. Line Wrap
  2. Text Alignment, Justification
  3. Match first-letter and first-line
  4. “:before”, “:after”
  5. Text Decoration: Underline, Overline, Line-Through
  6. Letter Spacing, Word Spacing

Box Decoration

  1. Box Shadow
  2. Text Shadow
  3. Round Corners
  4. Background Image
  5. Image Rollover

Misc

  1. Computed Style
  2. Pseudo Class vs Pseudo Element
  3. Media Query
  4. Variable
  5. calc
  6. Reset
  7. user-select
  8. Data URI Scheme
  9. protocol-relative URL
  10. Default Unit
  11. Declaring Character Set in CSS File
  12. Jargons Explained: Tag, Element, Node, Object, Attribute, Property, Method
  13. Display Table on Small Screen
  14. Fixed Aspect Ratio
  15. Add Icon to Links
  16. Progress Bar Example

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

Web Dev Tutorials