CSS Positioning: The Complete Visual Guide

Master the art of placing elements exactly where you want them on the page

Understanding the CSS Position Property

CSS positioning allows you to control the layout of elements on your webpage. The position property specifies the type of positioning method used for an element.

flowchart TD A[Position Property] --> B[Static] A --> C[Relative] A --> D[Absolute] A --> E[Fixed] A --> F[Sticky]

Each positioning type behaves differently in relation to the document flow and other elements. Let's explore each one in detail.

Parent
Child

Position Types Explained

1. Static Positioning (Default)

This is the default position value for all elements. Elements render in order, as they appear in the document flow.

  • Ignores top, right, bottom, left properties
  • No special positioning behavior
  • Elements stack according to normal document flow
.element {
  position: static; /* Default value */
}
Static 1
Static 2
Static 3

2. Relative Positioning

The element is positioned relative to its normal position in the document flow.

  • Maintains space in document flow where it would normally be
  • Can be offset using top, right, bottom, left
  • Creates a new stacking context
  • Often used as a container for absolutely positioned elements
.element {
  position: relative;
  top: 20px;
  left: 30px;
}
Element 1
Relative Element
Element 3

3. Absolute Positioning

The element is removed from the normal document flow and positioned relative to its nearest positioned ancestor.

  • No space is created for the element in the page layout
  • Positioned relative to its closest positioned ancestor (not static)
  • If no positioned ancestor, uses the document body
  • Can overlap other elements
.parent {
  position: relative; /* Creates positioning context */
}

.child {
  position: absolute;
  top: 0;
  right: 0;
}
Element 1
Element 2
Absolute
Element 3

4. Fixed Positioning

The element is positioned relative to the viewport (browser window). It stays in the same place even when the page is scrolled.

  • Removed from normal document flow
  • Positioned relative to the viewport
  • Doesn't move when scrolling
  • Commonly used for navigation bars, modals, or chat widgets
.fixed-element {
  position: fixed;
  bottom: 20px;
  right: 20px;
}
Scroll down
Content
More content
Fixed

5. Sticky Positioning

A hybrid of relative and fixed positioning. The element is treated as relative positioned until it crosses a specified threshold, then it becomes fixed.

  • Initially behaves like relative positioning
  • Becomes fixed when scroll position reaches threshold
  • Sticks within its parent container
  • Great for sticky headers or table column headers
.sticky-header {
  position: sticky;
  top: 0; /* Threshold where it becomes fixed */
}
Scroll down
Sticky Header
Content
More content
Even more
Keep scrolling

Interactive Positioning Playground

Adjust Position Values

0px
0px
0px
0px
0
Reference Element 1
Reference Element 2
Drag Me
Reference Element 3

Positioning Flowchart

flowchart TD A[Need to position an element?] --> B{Should it stay visible\nwhile scrolling?} B -->|Yes| C{Should it stick to\nviewport or container?} C -->|Viewport| D[Fixed] C -->|Container| E[Sticky] B -->|No| F{Should it be removed\nfrom normal flow?} F -->|Yes| G{Position relative to\nancestor or viewport?} G -->|Ancestor| H[Absolute] G -->|Viewport| I[Fixed] F -->|No| J{Need to offset from\nnormal position?} J -->|Yes| K[Relative] J -->|No| L[Static]

Common Use Cases

Dropdown Menus

Dropdowns typically use absolute positioning relative to their trigger button, with a higher z-index to appear above other content.

Modal Dialogs

Modals use fixed positioning to cover the entire viewport, often with a semi-transparent overlay.