Search code examples
htmlcsscss-selectorspseudo-class

CSS Transform and Pseudo classes


Using :after, I'm trying to create brutalist pseudo 3D button. But the :after is showing above the intended element.

What I'm Trying to accomplish

What I'm getting

Edit: Obviously the transform is getting in the way, research has yet to yield a why or find a work around.

What am I missing? Or is there a better way?

nav li {
  position: relative;
  display:inline-block;
  transition: background 0.2s;
  transform: skewX(10deg);
}
nav li a {
  display:block;
  text-decoration:none;
  padding: 5px 10px;
  font: 30px/1 sans-serif;
  color: #0bf;
}
nav li.active,
nav li:hover{
  background:#000;
}

nav li.active:after {
  content: "";
  display: inline-block;
  top: 10px;
  left: 10px;
  width: 100%;
  height: 100%;
  background-color: red;
  
  position: absolute;
  z-index: -1;
}
<nav>
  <ul>
    
    <li class="active">
      <a href="#">Products</a>
    </li>
    
  </ul>
</nav>


Solution

  • Voila... Just edited this part:

    nav li.active,
    nav li:hover{
      background:#000;
    }
    

    ... which should be:

    nav li.active a,
    nav li:hover a{
      background:#000;
    }
    

    Here is the working code:

    nav li {
      position: relative;
      display:inline-block;
      transition: background 0.2s;
      transform: skewX(10deg);
    }
    nav li a {
      display:block;
      text-decoration:none;
      padding: 5px 10px;
      font: 30px/1 sans-serif;
      color: #0bf;
    }
    nav li.active a,
    nav li:hover a{
      background:#000;
    }
    
    nav li.active:after {
      content: "";
      display: inline-block;
      top: 10px;
      left: 10px;
      width: 100%;
      height: 100%;
      background-color: red;
      
      position: fixed;
      z-index: -1;
    }
    <nav>
      <ul>
        
        <li class="active">
          <a href="#">Products</a>
        </li>
        
      </ul>
    </nav>