Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › position

CSS Reference

position

Controls how an element is positioned in the document. Five values, one foundational property.

Last updated: October 2026

Definition: The position property controls how an element is placed in the document. It has five values: static (default), relative, absolute, fixed, and sticky. Combined with top, right, bottom, and left, it lets you place elements anywhere.

🎬 Live Examples

The three most-used positioning values in action.

position: relative (offset from normal flow)
Normal position
Relative offset
position: absolute (inside relative parent)
Parent container
Absolute child
position: sticky (scroll the box below)
I'm sticky!

Scroll this container. When the blue bar reaches the top, it sticks. It stays fixed until its parent scrolls out of view.

More content to enable scrolling...

Keep scrolling...

Almost there...

Done.

💡 Relative, absolute, and sticky are the three positioning values you'll use most.

📝

Basic Syntax

.element {
    position: relative;
    top: 10px;
    left: 20px;
}

Position alone does nothing. You need top, right, bottom, or left to actually move the element — except for static.

🎯 All position Values

Value Behavior Common Use
staticDefault. Normal flow.Most elements
relativeStays in flow, can offsetParent for absolute children
absoluteRemoved from flow, positioned to nearest ancestorBadges, dropdowns, overlays
fixedRemoved from flow, positioned to viewportFixed navbars, chat widgets
stickyStays in flow until scroll thresholdSticky headers, table headers

🎯 Offset Properties

Property Purpose
topDistance from the top edge of the positioned ancestor.
rightDistance from the right edge.
bottomDistance from the bottom edge.
leftDistance from the left edge.
z-indexStacking order. Higher = on top.
1

relative — Offset From Normal

The element stays in the flow, but you can visually shift it.

.card {
    position: relative;
    top: 10px;
    left: 20px;
}

When to use:

  • To make an ancestor for absolute children (most common use).
  • For subtle visual offsets without breaking the layout.
  • To establish a stacking context for z-index.
2

absolute — Positioned to Ancestor

Removed from flow. Positions itself relative to the nearest positioned ancestor (any ancestor with position other than static).

.card {
    position: relative;  /* Establish the anchor */
}

.badge {
    position: absolute;
    top: 8px;
    right: 8px;
    background: red;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
}

💡 The rule: If a child has position: absolute, always make its parent position: relative (unless you want it positioned to the page).

3

fixed — Positioned to Viewport

Stays in the same screen position when scrolling.

.back-to-top {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: #3b82f6;
    color: white;
    padding: 1rem;
    border-radius: 50%;
    cursor: pointer;
}

.fixed-navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
}

The element is removed from flow — add padding or margin to other content to prevent overlap. On mobile, fixed can cause issues with the on-screen keyboard. Test carefully.

4

sticky — Sticks Until Parent Ends

Behaves like relative until the scroll position reaches a threshold, then behaves like fixed — but stays within its parent.

.sticky-header {
    position: sticky;
    top: 0;
    background: white;
    z-index: 10;
}

Requirements:

  • Must have a top, right, bottom, or left value.
  • Its parent must be taller than the sticky element.
  • No ancestor can have overflow: hidden or overflow: auto between the sticky element and the scroll container.
5

z-index — Stacking Order

Controls which elements appear on top when they overlap.

.modal {
    position: fixed;
    z-index: 1000;
}

.tooltip {
    position: absolute;
    z-index: 100;
}

.navbar {
    position: sticky;
    top: 0;
    z-index: 50;
}

Rules:

  • z-index only works on positioned elements (not static).
  • Higher numbers appear on top of lower numbers.
  • A new stacking context is created by position + z-index, transforms, filters, or opacity less than 1.

Common z-index scale:

  • Base content: 0
  • Dropdowns: 10–100
  • Sticky headers: 100–500
  • Modals: 1000+
  • Toasts: 9999

🎯 Real-World Patterns

Pattern Position
Navbar fixed to topfixed; top: 0
Back-to-top buttonfixed; bottom: 2rem; right: 2rem
Notification badge on iconabsolute; top: 0; right: 0
Dropdown menu below a buttonabsolute; top: 100%; left: 0
Modal overlay covering the pagefixed; inset: 0; z-index: 1000
Sticky table headersticky; top: 0
Tooltip on hoverabsolute; z-index: 100

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS position property?

The position property controls how an element is positioned in the document. It has five values: static (default), relative, absolute, fixed, and sticky. Combined with top, right, bottom, and left, it lets you place elements anywhere on the page.

What is the difference between relative and absolute?

position: relative keeps the element in the normal flow, but lets you offset it from its original position. position: absolute removes the element from the flow and positions it relative to the nearest positioned ancestor.

What is the difference between fixed and sticky?

position: fixed pins the element to the viewport — it stays in place when scrolling. position: sticky behaves like relative until the scroll reaches a threshold, then sticks. Sticky stays within its parent; fixed does not.

What is z-index in CSS?

z-index controls stacking order of positioned elements. Higher values appear on top of lower values. z-index only works on positioned elements (relative, absolute, fixed, or sticky).

How do I center a div with position absolute?

Set position: absolute, top: 50%, left: 50%, and transform: translate(-50%, -50%). The transform offsets by half the element's own width and height, achieving perfect centering.

🎯 What's Next?

← All CSS Reference