Logo hdtoledo
hdtoledo.dev
Módulos_del_Sistema
MÓDULO_TEÓRICO

Positioning and Layers (Position & Z-Index)

Positioning and Layers

Controlling the Third Dimension on the Web

By default, HTML elements flow in natural document order (position: static). But in rich dashboard applications and login portals, we need:

  • Notification badges pinned directly to icon corners.
  • Navigation headers that stick during scrolling (sticky).
  • Floating modal windows hovering over the entire interface (fixed + z-index).

1. The 5 Position Values

  • static (Default): Standard document flow. Ignores top, bottom, left, right, and z-index.
  • relative: Retains original slot in flow, but allows offsets and acts as an anchor parent for absolute children.
  • absolute: Removed from natural flow and positioned relative to the nearest positioned ancestor (relative).
  • fixed: Pinned directly to the browser Viewport. Remains in place regardless of page scrolling.
  • sticky: Acts like relative until a scroll threshold is hit (e.g. top: 0), then acts fixed.

2. Creating an Absolute Notification Badge

css
/* Button acts as the reference anchor (relative parent) */ .btn-notifications { position: relative; display: inline-flex; padding: 10px; background: var(--bg-card); border: 1px solid var(--border-glow); border-radius: var(--radius-sm); } /* Glowing badge pinned to top-right corner */ .badge-count { position: absolute; top: -6px; right: -6px; width: 18px; height: 18px; background: var(--danger); color: #fff; font-size: 10px; font-weight: 700; border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 0 8px var(--danger); }

3. Floating Modals & Stacking Context (z-index)

To build a full-screen blurred modal dialog:

css
.modal-overlay { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(8, 9, 14, 0.85); backdrop-filter: blur(8px); display: flex; justify-content: center; align-items: center; z-index: 1000; /* Stays above all content */ } .modal-content { background: #121420; padding: 32px; border-radius: var(--radius-md); border: 1px solid var(--primary); box-shadow: var(--shadow-neon); z-index: 1001; }