Learn CSS from Scratch

Display, Position and Z-index

Lesson 5 of 9 2 min read Updated 10 October 2026

Display

Every element has a default display type.

  • block starts on a new line and fills the width (div, p, h1).
  • inline flows with text and ignores width and height (span, a).
  • inline-block flows inline but accepts width and height.
  • none hides the element and removes it from the layout.
.badge { display: inline-block; padding: 4px 10px; }
.hidden { display: none; }

Position

ValueBehaviour
staticDefault. Normal flow
relativeNormal flow, but can be shifted with top/left
absoluteRemoved from flow and placed relative to the nearest positioned parent
fixedFixed relative to the screen, stays when scrolling
stickyActs normal until a scroll point, then sticks
<style>
.parent { position: relative; height: 140px; background: #eff6ff; }
.tag { position: absolute; top: 10px; right: 10px; background: #2563eb; color: white; padding: 4px 10px; border-radius: 8px; }
.sticky { position: sticky; top: 0; background: #fde68a; padding: 8px; }
</style>
<div class="sticky">I stick to the top when you scroll</div>
<div class="parent"><span class="tag">New</span> Parent box</div>
<div style="height:300px"></div>

z-index

z-index controls which positioned element appears on top. A higher number sits above a lower one.

.modal { position: fixed; z-index: 1000; }

Tip: Use a small set of z-index values (for example 10, 100, 1000) so your stacking stays predictable.

Practice

Build a sticky navigation bar and a card with a badge in the top-right corner.

Practice tasks

Reading is not enough. Try these without copying code, then compare with the lesson.

  • Compare block, inline and inline-block with three elements.
  • Position a badge on a card using relative and absolute.
  • Use z-index to stack two overlapping boxes.

Common mistakes

  • Using absolute positioning for full-page layout.
  • Applying z-index to an element that is not positioned.

Quick check

Relative to what is an absolutely positioned element placed?

Its nearest positioned ancestor.

What does display: none do?

It removes the element from the layout.