k.cornett
k.cornett

Reputation: 189

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>

Upvotes: 2

Views: 105

Answers (2)

Mr. Hugo
Mr. Hugo

Reputation: 12590

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>

Upvotes: 1

Mr Lister
Mr Lister

Reputation: 46589

Instead of an :after, you can use a box-shadow.

nav li {
  position: relative; z-index:0;
  display:inline-block;
  transition: background 0.2s;
  transform: skewX(10deg);
  box-shadow: 10px 10px 0 red;
}
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>
  <ul>
    
    <li class="active">
      <a href="#">Products</a>
    </li>
    
  </ul>
</nav>

Upvotes: 2

Related Questions