← Back to Bootstrap Course | Chapter 9: Layout Utilities | Lesson 6 of 7

Z-index

Z-index utilities control which overlapping element appears on top.

In this page:

  1. Z-index
Syntax
markup
<div class="position-relative z-1">above</div>
<div class="position-relative z-n1">below</div>

Z-index

Utilities .z-n1, .z-0, .z-1, .z-2 and .z-3 set the stacking order for positioned elements. Bootstrap's own components use higher values so dropdowns, modals and tooltips appear above content. z-index only works on positioned elements or flex and grid items.

Note: z-index needs a position other than static to take effect.

Example: Z-index

markup
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="container position-relative" style="height: 7rem">
  <div class="position-absolute top-0 start-0 bg-primary text-white p-4 z-1">z-1 (bottom)</div>
  <div class="position-absolute top-0 start-0 bg-danger text-white p-4 z-3" style="margin:1.5rem 0 0 2.5rem">z-3 (top)</div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Using z-index on static elements
  2. Huge custom values
  3. Fighting Bootstrap components stacking
Chapter Summary
  • z-n1, z-0, z-1, z-2, z-3
  • Works on positioned elements
  • Components use higher built-in values
  • Avoid large custom values
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.