0

I tried different things described here: Is it possible to set the stacking order of pseudo-elements below their parent element? And even though the parent element appeared in the front, when i hover over - it goes in the background. It seems that when i use trasfrom on parent element it creates a new stacking context and everything goes brrrrrrrr. I already thought just to create 3 separate divs with different z-indexes and translate them, when i hover over, but is there nice a way to do the this effect with pseudo elements?

Here is a basic example illustrating the issue:

.card_container {
  width: 33.333%;
  padding: 1rem;
}

.card {
  display: block;
  height: 100%;
  text-align: center;
  background-color: #f18e9b;
  position: relative;
  will-change: transform;
}

.card::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: -1;
  background-color: #e84a5f;
}

.card::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: -2;
  background-color: #cc1a32;
}

.card:hover {
  -webkit-transform: translate(-5px, 0);
  transform: translate(-5px, 0);
}

.card:hover:before {
  -webkit-transform: translate(5px, 0);
  transform: translate(5px, 0);
}

.card:hover:after {
  -webkit-transform: translate(10px, 0);
  transform: translate(10px, 0);
}

.content {
  padding: 4rem 1rem;
}

.title {
  font-size: 1.25rem;
  background-color: gray;
  border-radius: 3px;
  padding: 0.5rem;
}
<div class="card_container">
      <div class="card">
        <div class="content">
          <div class="title">Title</div>
          <div class="subtitle">Subitle</div>
          <div class="text">Random random random text</div>
        </div>
      </div>
    </div>

If you want to see the version with sass go here

  • I see two things. First you haven't set z-index for `.card`. Second you are accessing pseudo-element with only one `:` like `&:before` you should use two `&::before` and `&::after`. As you haven't provide a minimal reproducible example, it is difficult to help you more – SeeoX Oct 12 '20 at 14:46
  • 1
    here is a thing [link](https://jsfiddle.net/Rising_Tide/3pu7q8b5/2/) maybe there you can figure out smth adding z-index of 1000 to ```.card``` won't work because pseudo elements are in different stacking context and yes ```&::before``` was incorrect but sass compiled it in css with ```::``` – Rising_Tide Oct 12 '20 at 15:25
  • Using an **external** snippet is a bad way to complement your question. It is strongly recommended to use stackoverflow snippet instead. You can edit your question using the stackoverflow snippet. – SeeoX Oct 13 '20 at 07:37

0 Answers0