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:
positionis notstaticANDz-indexis setopacity< 1transformis appliedfilteris appliedisolation: isolateis 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)
- Background/border of root element
- Descendant elements with negative z-index
- Non-positioned block elements
- Non-positioned float elements
- Non-positioned inline elements
- Descendant elements with z-index: 0 or auto
- 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
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 neededWrite 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 testsOptimize 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 analysisQuiz
1. Which position value removes an element from the document flow?
2. What is a stacking context?
3. When does position: sticky become fixed?
4. What does position: relative do?
5. How do you center an element absolutely positioned?
Flashcards
Question
What is the difference between absolute and fixed positioning?
Click to reveal answer
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?
Click to reveal answer
Answer
static - the element follows normal document flow.
Question
When does position: sticky actually stick?
Click to reveal answer
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?
Click to reveal answer
Answer
Setting position (not static) with z-index, opacity < 1, transform, filter, or isolation: isolate.
Question
How do you center an absolutely positioned element?
Click to reveal answer
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 spaceabsolute- Relative to positioned ancestor, no spacefixed- Relative to viewport, no spacesticky- 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;
}