dev.rean.me
css

CSS Positioning Explained: Static, Relative, Absolute, Fixed & Sticky (With Code & Live Results)

Master CSS position properties (static, relative, absolute, fixed, sticky) with step-by-step code examples, visual result previews, z-index rules, and practical layout tips.

2026-09-26 Β· 11 min read

Share:

CSS positioning is one of the most fundamental concepts every web developer must master. Whether you are building floating badges, sticky headers, centered modals, or dropdown menus, understanding how the position property works is key to creating precise, modern web layouts.

In this guide, we will break down all 5 CSS position values (static, relative, absolute, fixed, and sticky), explain how offset properties (top, right, bottom, left) work, dive into z-index, and provide interactive-style code examples with live visual results!


Quick Summary of CSS position Values

Position ValueReference PointLeaves Space in Document Flow?Offset (top/left...) Works?Supports z-index?Common Use Case
staticNormal document flowYesNoNoDefault value for standard page flow
relativeIts own natural positionYes (leaves ghost space)YesYesParent container for absolute elements
absoluteNearest positioned parentNo (removed from flow)YesYesBadges, tooltips, dropdowns, modals
fixedBrowser viewport (screen)No (removed from flow)YesYesFixed navbars, Back-to-Top buttons
stickyScroll container / ViewportYes (until scrolled)YesYesSticky headers, sticky sidebars

Offset Properties: top, right, bottom, left

Before diving into each position mode, remember that offset properties move an element from its reference point:

  • top: Distance from the top edge.
  • bottom: Distance from the bottom edge.
  • left: Distance from the left edge.
  • right: Distance from the right edge.

πŸ’‘ Important: Offset properties only work if position is set to relative, absolute, fixed, or sticky. They are completely ignored when position: static (the default).


1. position: static (Default Flow)

static is the default position for every HTML element. Elements are positioned according to the natural flow of the page: block elements stack vertically, and inline elements sit side-by-side.

Key Rules:

  • Elements render in the exact order they appear in HTML.
  • top, right, bottom, left, and z-index have zero effect.

Code Example:

<div class="box box-1">Box 1 (Static)</div>
<div class="box box-2">Box 2 (Static)</div>
<div class="box box-3">Box 3 (Static)</div>
.box {
  padding: 15px 20px;
  margin: 10px 0;
  border-radius: 8px;
  color: #ffffff;
  font-weight: bold;
}
.box-1 { background-color: #3b82f6; }
.box-2 { background-color: #10b981; }
.box-3 { background-color: #8b5cf6; }

⚑ Live Result Preview:

Live Result Preview
Box 1 (Static - Default Flow)
Box 2 (Static - Default Flow)
Box 3 (Static - Default Flow)

2. position: relative (Offset from Self)

An element with position: relative remains in the normal document flow, but you can offset it relative to where it would normally sit.

Key Rules:

  • Shifted using top, bottom, left, or right.
  • Leaves its original space empty (ghost space) in the layout; surrounding elements will NOT shift to fill the gap.
  • Acts as the anchor container (parent) for any child elements using position: absolute.

Code Example:

.box-normal {
  background-color: #3b82f6;
}

.box-relative {
  position: relative;
  top: 20px;
  left: 40px;
  background-color: #f59e0b;
}

⚑ Live Result Preview:

Live Result Preview
Box 1 (Normal Static Box)
πŸ‘» Ghost Space (Original position reserved here)
Box 2 (position: relative; top: 20px; left: 40px;)
Box 3 (Static - Sits right below the ghost space)

πŸ’‘ Best Practice: When creating card components with notification dots, icons, or badges, always set position: relative; on the card container!


3. position: absolute (Positioned Relative to Ancestor)

An element with position: absolute is completely removed from the normal document flow. It takes up no space in the page layout, and siblings behave as if it does not exist.

Key Rules:

  • Positioned relative to its nearest positioned ancestor (an ancestor with relative, absolute, or fixed).
  • If no positioned ancestor exists, it aligns relative to the root <html> element.
  • Overlaps other elements easily.

Example A: Notification Badge on a Card

<div class="card">
  <span class="badge">NEW</span>
  <h3>Product Feature</h3>
  <p>Learn how to use CSS absolute positioning like a pro.</p>
</div>
.card {
  position: relative; /* Anchor container */
  width: 300px;
  padding: 20px;
  background-color: #1e293b;
  border-radius: 12px;
}

.badge {
  position: absolute;
  top: -10px;
  right: -10px;
  background-color: #ef4444;
  color: white;
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 9999px;
  font-weight: bold;
}

⚑ Live Result Preview (Badge on Card):

πŸ”₯ NEW

Product Feature

This card has position: relative, making the position: absolute badge snap top-right cleanly!


Example B: Perfect Centering with position: absolute

You can center any element perfectly inside a container using top: 50%, left: 50%, and transform: translate(-50%, -50%).

.parent-container {
  position: relative;
  height: 200px;
}

.centered-modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #0284c7;
  color: white;
  padding: 16px 24px;
  border-radius: 8px;
}

⚑ Live Result Preview (Centered Modal Box):

Live Result Preview
🎯 Perfectly Centered Box

4. position: fixed (Pinned to Viewport)

An element with position: fixed is removed from document flow and anchored to the browser window (viewport).

Key Rules:

  • Does NOT scroll with the page. It stays pinned in the exact same spot on screen.
  • Useful for header navigation bars, floating action buttons (FAB), chat widgets, and cookie consent overlays.

Code Example (Floating Chat Button):

.floating-chat-btn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background-color: #2563eb;
  color: white;
  padding: 14px 20px;
  border-radius: 50px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
  z-index: 999;
}

⚑ Live Result Preview:

Simulated Viewport / Screen Window

Scroll content inside this window below...

Line 1: Scroll down to test fixed positioning element stay in place...

Line 2: Notice how the blue button at bottom-right never moves!

Line 3: Great for Back-to-Top buttons and Live Chat icons.

Line 4: More content scrolling under the fixed element...

Line 5: End of content box.

πŸ’¬ Chat with us

5. position: sticky (Hybrid Relative + Fixed)

sticky positioning is a hybrid between relative and fixed. It behaves as relative until the user scrolls past a designated threshold (such as top: 0), at which point it sticks to that position on screen.

Key Rules:

  • Must specify at least one offset property (e.g. top: 0;).
  • Remains sticky only within its parent container. Once the parent container scrolls out of view, the sticky element scrolls away too.
  • Will NOT work if any parent container has overflow: hidden, overflow: auto, or overflow: scroll.

Code Example:

.sticky-header {
  position: sticky;
  top: 0;
  background-color: #0f172a;
  color: #38bdf8;
  padding: 12px 16px;
  font-weight: bold;
  z-index: 10;
}

⚑ Live Result Preview (Scroll inside the container to see sticky behavior):

Live Result Preview (Scroll Down Inside Box Below)
Scroll down inside this box...
<div style="position: sticky; top: 0; background-color: #0284c7; color: white; padding: 10px 16px; font-weight: 700; font-size: 14px; border-bottom: 1px solid #0369a1; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.3);">
  πŸ“Œ Sticky Section Header (Sticks to Top when scrolling)
</div>

<div style="padding: 16px; color: #cbd5e1; font-size: 14px; line-height: 1.8;">
  <p style="margin: 0 0 12px 0;">Paragraph 1: Keep scrolling down! The blue header box stays pinned at top = 0 while reading content.</p>
  <p style="margin: 0 0 12px 0;">Paragraph 2: Sticky headers are perfect for category lists, table headers, and blog chapter navigation.</p>
  <p style="margin: 0 0 12px 0;">Paragraph 3: As long as the parent container is visible, the sticky element holds its place!</p>
  <p style="margin: 0 0 12px 0;">Paragraph 4: You reached the middle of the article text.</p>
  <p style="margin: 0;">Paragraph 5: End of sticky demo content box.</p>
</div>

6. Understanding z-index & Stacking Context

When elements overlap using relative, absolute, fixed, or sticky, z-index determines which element sits in front and which sits behind.

Key Rules:

  • Higher z-index values appear on top of lower values.
  • Default z-index is auto (evaluated as 0).
  • z-index only works on positioned elements (ignored on position: static).

Code Example:

.card-back {
  position: relative;
  z-index: 1;
  background-color: #475569;
}

.card-front {
  position: relative;
  top: -40px;
  left: 30px;
  z-index: 10; /* Higher number comes to front */
  background-color: #2563eb;
}

⚑ Live Result Preview:

Layer 1 (z-index: 1)
Layer 2 (z-index: 5)
Layer 3 (z-index: 10 - Front!)

7. Golden Rules for CSS Positioning

  1. Always set position: relative on parent cards when placing position: absolute badges, close buttons, or icons inside them.
  2. Don't forget top/bottom/left/right when using absolute, fixed, or stickyβ€”otherwise they won't shift properly.
  3. If sticky isn't sticking, check if any parent element has overflow: hidden, overflow: auto, or overflow: scroll.
  4. Use fixed sparingly on mobile devices so you don't take up valuable screen space for users.
  5. Remember z-index requires a position property (relative, absolute, fixed, or sticky). It will not work on static elements!

Conclusion

Understanding CSS positioning opens up endless possibilities for web layouts. Here is a quick reminder when choosing:

  • Use relative for subtle offsets or setting up an anchor parent.
  • Use absolute for popups, tooltips, badges, and exact placement within a card.
  • Use fixed for floating action buttons or headers that stay on screen during page scroll.
  • Use sticky for section titles and table headers that scroll naturally until reaching the edge.

Happy coding! If you found this post helpful, share it with fellow developers!

← Back to css
Share: