CSS Position

The position property in CSS specifies how an element is positioned in the document. It controls whether an element is placed relative to its normal flow, its parent, or the viewport.

Position Values

Common position property values include:

Static Position

Static is the default positioning; elements appear in normal flow.

div {
  position: static;
}

Relative Position

Relative positioning moves an element relative to its normal position.

div {
  position: relative;
  top: 10px;
  left: 20px;
}

Absolute Position

Absolute positioning removes the element from normal flow and positions it relative to the nearest positioned ancestor.

div {
  position: absolute;
  top: 50px;
  right: 20px;
}

Fixed Position

Fixed elements are positioned relative to the viewport and remain in place when scrolling.

header {
  position: fixed;
  top: 0;
  width: 100%;
}

Sticky Position

Sticky elements behave like relative until a certain scroll position is reached, then they become fixed.

nav {
  position: sticky;
  top: 0;
}

Best Practices for Position

Common Mistakes with Position

Tip: Combining position with z-index can control element stacking order for better layout control.