Frontend Master

JavaScript Curriculum

Display and Positioning
+60 XP

Display and Positioning

medium
~32 min·60 XP

The coffee shop page needs a sticky header that stays visible while scrolling, a badge overlaid on the menu card images, and a dropdown nav — all requiring precise positioning.

Display and Positioning

Two CSS properties control where elements live on the page and how they relate to each other: display and position. Together they unlock every layout pattern.

display — How Elements Participate in Flow

The display property determines how an element generates boxes and participates in the document flow:

display.css
display: block

Takes up the full width available. Starts on a new line. Width, height, margin, padding all work as expected. Default for: div, p, h1-h6, section, article, header, footer.

Default for
<div><p><h1><section><header><footer>
.element {
  display: block;
}
Layout behaviour
Box A
Box B
💡The golden rule of display
When an element isn't where you expect it — ask: what is its display value? Block elements stack vertically. Inline elements flow with text. If you need something to sit beside other elements but still respect width/height, reach for inline-block or flex.

position — Where Elements Are Placed

Position controls which coordinate system an element uses and whether it stays in normal document flow:

position.css
position: static
top/left/z-index are ignored

The default. Element flows in normal document order. top/right/bottom/left and z-index have no effect.

Best used for: Default — you never need to set this explicitly
Code pattern
.element {
  position: static; /* default, never needed */
}
ℹ️The absolute + relative pattern
The most common use of absolute positioning: place a child exactly within a parent. The parent gets position: relative (creates a positioning context), the child gets position: absolute with top/right/bottom/left values. This is how badges, close buttons, and overlays are built.

z-index — Controlling Stacking Order

When elements overlap, z-index controls which one appears on top. Higher numbers win — but only for positioned elements:

z-index.css
Stacking order (highest on top)
Base card (z:1)
Dropdown (z:10)
Modal (z:100)
Tooltip (z:200)
Adjust z-index
Base cardz: 1
Dropdownz: 10
Modalz: 100
Tooltipz: 200
z-index only works on positioned elements (relative, absolute, fixed, sticky).
⚠️z-index only works on positioned elements
Setting z-index on a position: static element does nothing. The element must have position: relative, absolute, fixed, or sticky. This is the most common z-index bug — always check position first.

Your Challenge

Build a fixed header with z-index layering, and add absolute-positioned badges to the menu cards.

Challenge

Make the header position: fixed at the top of the page. Add a position: absolute badge in the top-right corner of each .card (which needs position: relative). Give the header a z-index of 100 so it layers above everything. Add padding-top to body equal to the header height so content isn't hidden behind it.

displayblockinlineinline-blocknonepositionstaticrelativeabsolutefixedstickyz-indexstacking-contextflow
Display and Positioning | Nexus Learn