← Back to Examples | This header has position: fixed - it stays while you scroll!

CSS Positioning Demo

This page demonstrates different CSS position values. Scroll to see fixed and sticky in action!

1. Static (Default)

position: static - Elements flow normally. top/left/right/bottom have no effect.

Static Box 1
Static Box 2
Static Box 3

2. Relative

position: relative - Offset from normal position. Original space is preserved!

(Original position - dashed)
Relative: top: 30px; left: 50px

Notice the gap where the box originally was!

Key Point: The element moves, but its original space in the document flow is preserved.

3. Absolute

position: absolute - Positioned relative to nearest positioned ancestor (or viewport).

This container has position: relative (the positioned ancestor)

Absolute: top: 20px; right: 20px
Absolute: bottom: 10px; left: 10px
Key Point: Absolute elements are removed from the flow. They don't affect other elements.

4. Fixed

position: fixed - Positioned relative to viewport. Stays in place when scrolling.

Example: Look at the orange header at the top of this page!

.fixed-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 1000;
}
Common Uses: Fixed headers, floating action buttons, cookie banners, chat widgets.

5. Sticky

position: sticky - Acts like relative until scroll threshold, then sticks like fixed.

Scroll inside the box below to see sticky headers:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.

Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit.

Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.

Sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.

Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam.

6. Z-Index (Stacking Order)

z-index controls which positioned elements appear on top.

z-index: 1
z-index: 2
z-index: 3
Note: z-index only works on positioned elements (not static). Higher values appear on top.

Summary Table

Position Relative To In Document Flow? Use Case
static Normal flow Yes Default behavior
relative Its normal position Yes (space preserved) Minor offsets, parent for absolute
absolute Nearest positioned ancestor No Overlays, tooltips, dropdowns
fixed Viewport No Fixed headers, floating buttons
sticky Scroll container Yes (until stuck) Sticky headers, table headers

← Back to Examples