On this page11 sections
Lecture 016: Position & z-index
| Category | 4: Layout Basics |
| Lecture | 016 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Normally elements flow one after another down the page.
positionlets you break out of that flow: nudge something a little, pin a badge to a card's corner, keep a header on screen while scrolling, or fix a chat button to the bottom of the window.
Learning Objectives
- Use
position: static,relative,absolute,fixedandsticky - Place positioned elements with
top,right,bottomandleft - Stack overlapping elements with
z-index
1. The Five Values
| Value | Placed relative to | Leaves its space in the flow? |
|---|---|---|
static | Normal flow (the default) | Yes |
relative | Its own normal spot | Yes |
absolute | The nearest positioned ancestor | No |
fixed | The browser window | No |
sticky | Normal flow, until you scroll past a point, then it sticks | Yes |
top, right, bottom and left only work when position is not static.
2. relative: Nudge It
Example
<style>
.box { width: 120px; padding: 12px; background: #d0ebff; margin-bottom: 6px; }
.moved { position: relative; top: 10px; left: 30px; background: #ffc9c9; }
</style>
<div class="box">Normal</div>
<div class="box moved">Moved 30px right, 10px down</div>
<div class="box">Normal</div>The moved box's original space stays empty, so nothing else shifts.
3. absolute: Pin It Inside a Parent
An absolute element is placed inside its nearest ancestor that has a position set. The
classic pattern: parent relative, child absolute.
Example: a "SALE" badge in a card's corner:
<style>
.card { position: relative; width: 220px; padding: 16px; border: 1px solid #ddd; border-radius: 10px; }
.badge { position: absolute; top: -10px; right: -10px; background: #e03131; color: white;
padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: bold; }
</style>
<div class="card">
<span class="badge">SALE</span>
<h3>Sports shoes</h3>
<p>Now Rs 4,999</p>
</div>Without position: relative on the card, the badge would be placed relative to the whole page.
4. fixed: Stay on Screen
Example: scroll inside the preview; the button doesn't move:
<style>
.chat { position: fixed; bottom: 16px; right: 16px; background: #2f9e44; color: white;
padding: 10px 16px; border-radius: 999px; }
</style>
<p>Scroll down…</p>
<div style="height: 800px; background: linear-gradient(#f8f9fa, #dee2e6);"></div>
<a class="chat" href="#">💬 Chat with us</a>5. sticky: Stick When You Reach It
Example
<style>
h3 { position: sticky; top: 0; background: #4c6ef5; color: white; padding: 8px; margin: 0; }
p { height: 220px; margin: 0; padding: 8px; }
</style>
<h3>Section A</h3><p>Scroll: this heading sticks to the top until Section B pushes it away.</p>
<h3>Section B</h3><p>Sticky headers are common in long lists and tables.</p>
<h3>Section C</h3><p>The end.</p>6. Layers With z-index
When positioned elements overlap, the one with the higher z-index is drawn on top.
Example
<style>
.wrap { position: relative; height: 130px; }
.sq { position: absolute; width: 90px; height: 90px; opacity: 0.9; }
.a { background: #ff8787; top: 0; left: 0; z-index: 1; }
.b { background: #74c0fc; top: 30px; left: 40px; z-index: 3; }
.c { background: #8ce99a; top: 15px; left: 80px; z-index: 2; }
</style>
<div class="wrap">
<div class="sq a">1</div>
<div class="sq b">3 (on top)</div>
<div class="sq c">2</div>
</div>Common Beginner Mistakes
1. Absolute child, unpositioned parent. The child jumps to the page corner. Add
position: relative to the parent.
2. top/left on static elements. They do nothing until position is set.
3. Fixed elements covering content. A fixed header hides the top of the page. Add matching
padding-top to the body.
4. Sticky that doesn't stick. It needs a top value, and a parent with overflow: hidden
stops it from working.
Practice Questions
- Which position value is the default?
- Which value keeps an element on screen while the page scrolls?
- You want a badge in the top-right corner of a card. What position do the card and the badge need?
- Does a
position: absoluteelement keep its space in the page flow? - Two overlapping elements have
z-index: 5andz-index: 2. Which is on top? - What two things does
position: stickyneed to work?
Practice Answers
staticfixed- The card
position: relative; the badgeposition: absolute; top: 0; right: 0; - No. It's taken out of the flow.
- The one with
z-index: 5. - A
top(orbottom) value, and no ancestor withoverflow: hiddenin the way.
Key Takeaways
relativenudges;absolutepins inside the nearest positioned parent.fixedstays on the window;stickysticks once you scroll to it.- Parent
relative+ childabsoluteis the pattern you'll use most. z-indexdecides which overlapping element is on top.