On this page11 sections
  1. Learning Objectives
  2. 1. The Five Values
  3. 2. relative: Nudge It
  4. 3. absolute: Pin It Inside a Parent
  5. 4. fixed: Stay on Screen
  6. 5. sticky: Stick When You Reach It
  7. 6. Layers With z-index
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 016: Position & z-index

Category4: Layout Basics
Lecture016 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelIntermediate

Normally elements flow one after another down the page. position lets 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, fixed and sticky
  • Place positioned elements with top, right, bottom and left
  • Stack overlapping elements with z-index

1. The Five Values

ValuePlaced relative toLeaves its space in the flow?
staticNormal flow (the default)Yes
relativeIts own normal spotYes
absoluteThe nearest positioned ancestorNo
fixedThe browser windowNo
stickyNormal flow, until you scroll past a point, then it sticksYes

top, right, bottom and left only work when position is not static.


2. relative: Nudge It

Example

html
<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:

html
<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:

html
<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

html
<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

html
<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

  1. Which position value is the default?
  2. Which value keeps an element on screen while the page scrolls?
  3. You want a badge in the top-right corner of a card. What position do the card and the badge need?
  4. Does a position: absolute element keep its space in the page flow?
  5. Two overlapping elements have z-index: 5 and z-index: 2. Which is on top?
  6. What two things does position: sticky need to work?

Practice Answers

  1. static
  2. fixed
  3. The card position: relative; the badge position: absolute; top: 0; right: 0;
  4. No. It's taken out of the flow.
  5. The one with z-index: 5.
  6. A top (or bottom) value, and no ancestor with overflow: hidden in the way.

Key Takeaways

  • relative nudges; absolute pins inside the nearest positioned parent.
  • fixed stays on the window; sticky sticks once you scroll to it.
  • Parent relative + child absolute is the pattern you'll use most.
  • z-index decides which overlapping element is on top.