Kevin Powell - CommunityKP-C
Kevin Powell - Community16mo ago
6 replies
Faker

Nested stacking context using z-index

Hello guys, sorry to disturb you all; normally, when we have a nested stacking context, the child element shouldn't be always below the parent element stacking order even though it has a greater z-index?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles/test.css" />
    <title>Stacking context</title>
  </head>
  <body>
    <div class="box1"></div>
    <div class="box2">
      <div class="box3"></div>
    </div>
  </body>
</html>

*, *::after, *::before{
    box-sizing: border-box;
}

body{
    margin: 0;
    min-height: 100svh;
    height: 100vh;
}

.box1 {
    width: 200px;
    height: 100px;
    background-color: violet;
    position: relative;
    top: 20px;
    z-index: 1;
}

.box2 {
    width: 200px;
    height: 100px;
    background-color: hsl(240, 66%, 63%);
    position: relative;
    bottom: 10px;
    z-index: 2;
}

.box3 {
    width: 200px;
    height: 100px;
    background-color: hsl(0, 84%, 66%);
    position: relative;
    left: 20px;
    bottom: 30px;
    z-index: 3;
}

In the uploaded picture, why is box3 (the box with the tomato-like color) stacked over its parent element, the blue-like color
AA2208C9-B35D-459C-ADB9-28A1D6428225.png
Was this page helpful?