skip to Main Content

I am using translateY to give the button a depth effect using CSS keyframes and then using that keyframe into :hover pseudo state with the help of the animate property. But, it’s kind of shifting behind the ::after element.

Any Possible solutions that could help me achieve the desired animation?

enter image description here

When the button is pressed or hovered (on Desktop) the white part should be pressing in a way that it gets dissolved with the white border.

.container {
  display: flex;
  align-items: center;
  justify-content: center;
}

.abstract-btn {
  text-decoration: none;
  font-family: 'Poppins', sans-serif;
  background: #000000;
  color: #fff;
  letter-spacing: 2px;
  position: relative;
  width: 195px;
  height: 46px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
}

.abstract-btn::after {
  content: '';
  display: block;
  position: absolute;
  height: 46px;
  width: 195px;
  top: 3px;
  left: 3px;
  background: #fff;
  border: 1px solid #000;
  z-index: -1
}

.abstract-btn::before {
  content: '';
  position: absolute;
  right: -5px;
  top: 0;
  width: 0;
  height: 0;
  border-top: 25px solid white;
  border-right: 30px solid transparent;
  transform: rotateY(180deg);
}

.abstract-btn:hover {
  animation: pressButton 1s ease both;
}

@keyframes pressButton {
  from {
    transform: translateZ(0px);
  }
  to {
    transform: translateZ(3px);
  }
}
<div class="container">
  <a href="#" class="abstract-btn">
    Return Home
  </a>
</div>

Here, I am using the ::after to create the white border and ::before to add the triangle on the top right corner of the button.

2

Answers


  1. I don’t know if it’s that style of button but I made one that looks like what you describe on my Codepen. Let me know if that’s right.

    https://codepen.io/ArthurCottey/pen/OJoMgZW

    HTML:

    <a class="top-left" target="_blank" href="https://twitter.com/iamArthurCottey"></a>
    

    CSS:

    .top-left {
        position: relative;
        width: 200px;
        height: 48px;
        border: 2px solid #000;
        background-color: #85a00d;
        color: black;
    }
    
    .top-left::after {
        content: 'Click here!';
        font-family: 'Advent Pro', sans-serif;
        font-size: 1rem;
        border: 2px solid #000;
        background-color: #d4f34a;
        position: absolute;
        width: 100%;
        height: 100%;
        top: -6px;
        left: -6px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.1s;
     }
    
    .top-left:hover::after {
        top: -1.5px;
        left: -1px;
     }
    
    Login or Signup to reply.
  2. This can be done with transitions on the top and left properties of both the .abstract-btn and .abstract-btn::after. The .abstract-btn having display: flex; was causing the problem of the ::after showing in front. I changed it to display: block; then wrapped the link text in a span positioned in the center.

    .container {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .abstract-btn {
      text-decoration: none;
      font-family: 'Poppins', sans-serif;
      background: #000000;
      color: #fff;
      letter-spacing: 2px;
      position: relative;
      width: 195px;
      height: 46px;
      text-align: center;
      display: block;
      align-items: center;
      justify-content: center;
      font-weight: 700;
      top: 0;
      left: 0;
      transition: all 1s;
    }
    
    .abstract-btn::after {
      content: '';
      display: block;
      position: absolute;
      height: 46px;
      width: 195px;
      top: 3px;
      left: 3px;
      background: #fff;
      border: 1px solid #000;
      z-index: -1;
      transition: all 1s;
    }
    
    .abstract-btn::before {
      content: '';
      position: absolute;
      right: -5px;
      top: 0;
      width: 0;
      height: 0;
      border-top: 25px solid white;
      border-right: 30px solid transparent;
      transform: rotateY(180deg);
    }
    
    .abstract-btn:hover {
      top: 3px;
      left: 3px;
    }
    
    .abstract-btn:hover::after {
      top: 0;
      left: 0;
    }
    
    .abstract-btn span {
      position: absolute;
      left: 0;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
    }
    <div class="container">
      <a href="#" class="abstract-btn">
        <span>Return Home</span>
      </a>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search