Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

z-index doesn't work with ::after/::before element?

Tags:

css

z-index doesn't work with its ::before/::after element. Here I'm sharing code below.

.or {
  border: 2px solid #8fc300;
  border-radius: 5px;
  color: #333;
  font-size: 17px;
  font-weight: 600;
  height: 34px;
  line-height: 26px;
  text-align: center;
  width: 34px;
  margin-top: 64px;
  margin-left: 20px;
  margin-right: 20px;
  background: #fff;
  /*For z-index - keep the green area on top*/
  position: relative;
  z-index: 11;
}
.or::after {
  background: red;
  content: "";
  display: block;
  height: 116px;
  margin-left: 15px;
  margin-top: -68px;
  width: 4px;
  /*For z-index - keep the green area on top*/
  position: relative;
  z-index: 9;
}
<div class="or"></div>
like image 485
Jim Fahad Avatar asked Sep 14 '16 11:09

Jim Fahad


1 Answers

You can remove z-index from parent element and use negative z-index: -1 on pseudo element. If you want only green line on red line you need to remove white background from parent also DEMO

.or {
  border: 2px solid #8fc300;
  border-radius: 5px;
  color: #333;
  font-size: 17px;
  font-weight: 600;
  height: 34px;
  line-height: 26px;
  text-align: center;
  width: 34px;
  margin-top: 64px;
  margin-left: 20px;
  margin-right: 20px;
  background: #fff;
  /*For z-index - keep the green area on top*/
  position: relative;
}
.or::after {
  background: red;
  content: "";
  display: block;
  height: 116px;
  margin-left: 15px;
  margin-top: -68px;
  width: 4px;
  /*For z-index - keep the green area on top*/
  position: absolute;
  z-index: -1;
}
<div class="or"></div>
like image 152
Nenad Vracar Avatar answered Oct 17 '22 15:10

Nenad Vracar