0

I made a side panel on my website by embedding divs inside a div that formed an area of the grid. I wanted them to change colour when I hovered over them, but nothing happened. I later solved the problem by changing the z-index of the grid area. It was originally set to to -1 but I deleted this from the CSS and it solved the problem. Although I have solved the problem, I want to understand why the z-index had this effect. Below is an extract of the original code.

#box-b {
    background: blue;
    grid-area: b;
    z-index: -1
    }
    
.main {
      display: grid;
      grid-template-areas: "a a"
                           "b c";
      grid-template-rows: 40px 800px;
      grid-template-columns: 1fr 4fr;
      }

.side-tile {
            height: 35px;
            border-right: 2px solid black;
            border-left: 2px solid black;
            border-bottom: 1px solid black;
            border-top: 1px solid black;
            text-align: center;
            padding-top: 15px;
            }
            
.side-tile:hover {
                  background: #00007f;
                  border-right: 2px solid red;
                  border-left: 2px solid red;
                  border-top: 1px solid red;
                  border-bottom: 1px solid red;
                  cursor: pointer;
                  }      
<div class="main">
    <div id=box-b>
      <div class=side-tile>
      A
      </div>
      <div class=side-tile>
      B
      </div>
      <div class=side-tile>
      C
      </div>
      <div class=side-tile>
      D
      </div>
      <div class=side-tile>
      E
      </div>
      <div class=side-tile>
      F
      </div>
    </div>
  </div>
Urthona26
  • 109
  • 5
  • 2
    add a backgroud to main and you will understand – Temani Afif Jun 04 '21 at 11:41
  • Because by applying `z-index: -1`, you moved it “behind” `.main` - any mouse movement will now be “captured” by that element, and not even reach your boxes any more. – CBroe Jun 04 '21 at 11:42

0 Answers0