0

In the following example I create a simple grid using the class row and cell and would like to render the element with class task spanning over two grid cells. I use z-index: 2 style to bring the task to the top but this does not seem to work as expected and i still see the vertical grid between two cells. What is my mistake?

<!DOCTYPE html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <style>
    .row {
      height: 34px;
      display: flex;
      flex-direction: row;
    }
    .cell {
      border-style: solid;
      border-width: 1px 0 1px 1px;
      width: 60px;
      min-width: 60px;
      z-index: 1;
    }
    .last-cell {
      border-right-width: 1px;
    }
    .task {
      background-color: #3db9d3;
      height: 30px;
      width: 119px;
      margin: 1px 0 0 1px;
      z-index: 2;
    }
  </style>
</head>
<body>
  <div class="row">
    <div class="cell">
      <div class="task">Task</div>
    </div>
    <div class="cell last-cell"></div>
  </div>
</body>
doberkofler
  • 9,511
  • 18
  • 74
  • 126
  • Works as it should. Your mistake is misunderstanding of the [stacking context](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context). – Kosh Feb 24 '20 at 20:26
  • related: https://stackoverflow.com/a/54903621/8620333 – Temani Afif Feb 24 '20 at 20:31

1 Answers1

3

You need to establish a new stacking context by removing the z-index on the parent element. Also, the z-index only applies to positioned elements so you need to set position: relative on the Task div:

.row {
  height: 34px;
  display: flex;
  flex-direction: row;
}

.cell {
  border-style: solid;
  border-width: 1px 0 1px 1px;
  width: 60px;
  min-width: 60px;
}

.last-cell {
  border-right-width: 1px;
}

.task {
  background-color: #3db9d3;
  height: 30px;
  width: 119px;
  margin: 1px 0 0 1px;
  z-index: 2;
  position: relative;
}
<div class="row">
  <div class="cell">
    <div class="task">Task</div>
  </div>
  <div class="cell last-cell"></div>
</div>

See also How to make child element higher z-index than parent?

Community
  • 1
  • 1
j08691
  • 204,283
  • 31
  • 260
  • 272