Skip to content
beginnerPhase 31 · CSS

CSS Positioning

Master static, relative, absolute, fixed, and sticky positioning.

45m
0 problems
Topic Progress0%

Position Values

Position Values

position: static (Default)

Normal document flow:

.element {
  position: static;
  /* top, right, bottom, left have no effect */
}

position: relative

Relative to normal position, keeps space in flow:

.element {
  position: relative;
  top: 20px;   /* Moves 20px down */
  left: 10px;  /* Moves 10px right */
}
/* Element keeps its original space */

position: absolute

Relative to nearest positioned ancestor (or body):

.parent {
  position: relative; /* Positioned ancestor */
}

.child {
  position: absolute;
  top: 0;
  right: 0;
}

Key points:

  • Removed from document flow
  • No space reserved
  • Siblings collapse into its space

position: fixed

Relative to the viewport:

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

Key points:

  • Always stays in view
  • Removed from document flow
  • Doesn't scroll with page

position: sticky

Hybrid of relative and fixed:

.header {
  position: sticky;
  top: 0;
}
/* Sticks when scrolling, but only within parent */

Key points:

  • Relative until scroll threshold
  • Then becomes fixed
  • Parent must have scroll overflow

Z-index

Z-index

Controls stacking order of positioned elements.

Basic Usage

.box-1 {
  position: absolute;
  z-index: 1;
}

.box-2 {
  position: absolute;
  z-index: 2; /* On top */
}

Stacking Context

A stacking context is created when:

  1. position is not static AND z-index is set
  2. opacity < 1
  3. transform is applied
  4. filter is applied
  5. isolation: isolate is set
.parent {
  position: relative;
  z-index: 1; /* Creates stacking context */
}

.parent .child {
  z-index: 999; /* Only within parent's context */
}

Stacking Order (bottom to top)

  1. Background/border of root element
  2. Descendant elements with negative z-index
  3. Non-positioned block elements
  4. Non-positioned float elements
  5. Non-positioned inline elements
  6. Descendant elements with z-index: 0 or auto
  7. Descendant elements with positive z-index

Common Gotcha

/* This WON'T work as expected */
.parent { z-index: 1; }
.parent .child { z-index: 1000; }

/* The child can't escape parent's stacking context */

Practical Positioning

Practical Positioning

Sticky Header

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

Tooltip

.tooltip-trigger {
  position: relative;
}

.tooltip {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: black;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  white-space: nowrap;
}

Overlay/Modal

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

Centering with Absolute

.parent {
  position: relative;
}

.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Fixed Sidebar

.sidebar {
  position: fixed;
  left: 0;
  top: 60px;
  width: 250px;
  height: calc(100vh - 60px);
}

.main-content {
  margin-left: 250px;
}

Back to Top Button

.back-to-top {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  z-index: 99;
}

Practice Problems

0/3solved
Build CSS Positioning Component

Create a reusable React component implementing CSS Positioning. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
CSS Positioning Testing

Write unit and integration tests for CSS Positioning using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
CSS Positioning Performance

Optimize CSS Positioning for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. Which position value removes an element from the document flow?

Question 1 options

2. What is a stacking context?

Question 2 options

3. When does position: sticky become fixed?

Question 3 options

4. What does position: relative do?

Question 4 options

5. How do you center an element absolutely positioned?

Question 5 options

Flashcards

Question

What is the difference between absolute and fixed positioning?

Answer

absolute positions relative to nearest positioned ancestor. fixed positions relative to the viewport and stays when scrolling.

Question

What is the default position value?

Answer

static - the element follows normal document flow.

Question

When does position: sticky actually stick?

Answer

When the user scrolls past the threshold set by top/left/right/bottom. It stays within its parent container.

Question

What creates a stacking context?

Answer

Setting position (not static) with z-index, opacity < 1, transform, filter, or isolation: isolate.

Question

How do you center an absolutely positioned element?

Answer

top: 50%; left: 50%; transform: translate(-50%, -50%);

Revision Notes

Key Takeaways

  • 1.static is the default and ignores top/right/bottom/left
  • 2.absolute removes element from flow and positions relative to ancestor
  • 3.fixed positions relative to viewport and stays during scroll
  • 4.sticky is a hybrid that sticks when scrolling past threshold
  • 5.z-index only works on positioned elements and within stacking contexts

Interview Tips

  • Explain the difference between all position values
  • Describe what a stacking context is and what creates one
  • Know how to center elements using different techniques
  • Understand the practical use cases for each position value

Cheat Sheet

CSS Positioning Cheat Sheet

Position Values

  • static - Normal flow (default)
  • relative - Relative to normal position, keeps space
  • absolute - Relative to positioned ancestor, no space
  • fixed - Relative to viewport, no space
  • sticky - Hybrid, sticks on scroll

Z-index

  • Only works on positioned elements (not static)
  • Higher z-index = higher in stack
  • Compare within same stacking context

Common Patterns

/* Tooltip */
.tooltip { position: absolute; bottom: 100%; }

/* Sticky header */
.header { position: sticky; top: 0; }

/* Center absolute */
.center {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
}

/* Overlay */
.overlay {
  position: fixed;
  inset: 0;
}