MODULE 03 · CSS FOUNDATIONS

Display, Position & Z-Index

How an element behaves in the flow of a page, where it sits when you take it out of that flow, and which element ends up on top when two overlap.

Day 20 of 30 Beginner ~19 min

Two Separate Questions

display answers "how does this element behave among its siblings?" position answers "where does this element sit, and relative to what?" They're often confused because both affect layout, but they control different things.

block, inline & inline-block

VALUE BEHAVIOR
block

Starts on a new line and takes the full available width. width/height apply fully.

inline

Flows within the text line. width/height are ignored; only horizontal padding/margin work reliably.

inline-block

Flows inline like text, but respects width/height and vertical spacing — the best of both.

none

Removes the element entirely — no space is reserved for it.

HOW EACH VALUE FLOWS
display: block
Item AItem B
display: inline
Item A Item B
display: inline-block
Item AItem B

static, relative, absolute & fixed

VALUE BEHAVIOR
static

The default. Normal document flow — top/left/right/bottom have no effect.

relative

Stays in normal flow, but top/left/etc. nudge it from where it would normally sit.

absolute

Removed from flow entirely; positioned relative to its nearest positioned ancestor.

fixed

Removed from flow; positioned relative to the browser viewport, stays put while scrolling.

style.css
/* Parent must be "positioned" for absolute children to anchor to it */
.card { position: relative; }

/* Badge pinned to the card's top-right corner */
.badge {
  position: absolute;
  top: 12px;
  right: 12px;
}

/* Header that stays visible while the page scrolls */
.site-header {
  position: fixed;
  top: 0;
  width: 100%;
}
ABSOLUTE POSITIONING WITHIN A PARENT
top: 16px; left: 16px;
bottom: 16px; right: 16px;
THE KEY RULE

An absolutely positioned element anchors to its nearest ancestor that has position set to anything other than static. Forgetting to set position: relative on the parent is the most common bug beginners hit here.

Controlling Stacking Order

When two positioned elements overlap, z-index decides which one appears on top. Higher values stack above lower ones. z-index only has an effect on elements that already have a position value other than static.

style.css
.card-1 { position: absolute; z-index: 1; }
.card-2 { position: absolute; z-index: 2; }
.card-3 { position: absolute; z-index: 3; }
/* card-3 renders above card-2, which renders above card-1 */
STACKING ORDER IN ACTION
z: 1
z: 2
z: 3

z-index Without position

DOESN'T WORK .box { z-index: 5; } — with position: static (the default), z-index is silently ignored.
WORKS .box { position: relative; z-index: 5; } — position is set, so z-index takes effect.

Breaking Down What You Just Learned

1

displayblock, inline, and inline-block control how an element flows among its siblings.

2

position: relativeStays in flow but can be nudged, and becomes an anchor for absolute children.

3

position: absoluteRemoved from flow, positioned relative to its nearest positioned ancestor.

4

position: fixedAnchored to the viewport, stays visible while the page scrolls.

5

z-indexControls stacking order between overlapping positioned elements — higher wins.

Try It Yourself

EXERCISE

Build a card with position: relative, then add a small "New" badge inside it using position: absolute anchored to the top-right corner. Add a second overlapping badge and use z-index to control which sits on top.

Build a Sticky Header With a Badge

HANDS-ON EXERCISE

Combining display, position, and z-index in one page

Create day20.html linked to a stylesheet and build a small page with a fixed header.

  1. Build a header with position: fixed; top: 0; that stays visible while scrolling.
  2. Add enough paragraph content below it to make the page scrollable.
  3. Build a product card with position: relative and an absolutely positioned "Sale" badge in one corner.
  4. Overlap two badges on the same card and use z-index to control which one shows on top.
  5. Switch a row of buttons from display: block to inline-block and compare how they lay out.

Recap

DISPLAY

block, inline, and inline-block control flow among sibling elements.

POSITION

static, relative, absolute, and fixed control placement and flow removal.

POSITIONED ANCESTOR

Absolute children anchor to the nearest non-static ancestor.

Z-INDEX

Only works on positioned elements; higher values stack above lower ones.

Key Takeaways

  • display controls how an element behaves in the flow of the page relative to its siblings.
  • inline-block combines inline flow with support for width, height, and vertical spacing.
  • position: relative keeps an element in flow while enabling it to anchor absolute children.
  • position: absolute and fixed remove an element from normal flow entirely.
  • z-index only affects positioned elements, and higher values render above lower ones.
Course Overview