Era
Era

Reputation: 3

Problem with pseudo elements after and before in css

Currently, I am trying to make an animated border-box that goes around a link but once I hover over my mouse the animation works only for the one instance of the pseudo-elements, not both. The instance that is written above the other works.

I do not know what's wrong with that as I can't find the problem either. Both pseudo-elements got the Content set and I don't really know what is wrong.

Here is the code

nav a {
    text-decoration: none;
    color: white;
    width: 100px;
    height: 50px;
    padding: 25px;
    position: relative;
}

nav a::before {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    padding: 0;
    background: transparent;
    border: 2px transparent solid;
}

nav a:hover::before {
    animation: animate 0.5s linear forwards;
}

@keyframes animate 
{
    0% {
        width: 0;
        height: 0;
        border-top-color: white;
        border-right-color: transparent;
        border-bottom-color: transparent;
        border-left-color: transparent;
    }

    50% {
        width: 100%;
        height: 0;
        border-top-color: white;
        border-right-color: white;
        border-bottom-color: transparent;
        border-left-color: transparent;
    }

    100% {
        width: 100%;
        height: 100%;
        border-top-color: white;
        border-right-color: white;
        border-bottom-color: transparent;
        border-left-color: transparent;

} 

nav a::after {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    padding: 0;
    background: transparent;
    border: 2px transparent solid;
} 

nav a:hover::after {
    animation: animate2 0.5s linear forwards;
}

@keyframes animate2 
{
    0% {
        width: 0;
        height: 0;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: transparent;
        border-left-color: white;
    }

    50% {
        width: 0%;
        height: 100%;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: white;
        border-left-color: white;
    }

    100% {
        width: 100%;
        height: 100%;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: white;
        border-left-color: white;
}

Upvotes: 0

Views: 657

Answers (1)

drinking-code
drinking-code

Reputation: 190

For both animate and animate2 you are missing the closing brackets after the 100% code block.

Placing them makes the css work for me.

For example:

html {
  background: #333;
}

nav a {
  text-decoration: none;
  color: white;
  width: 100px;
  height: 50px;
  padding: 25px;
  position: relative;
}

nav a::before {
  content: "";
  position: absolute;
  top: -2px;
  left: -2px;
  width: 0;
  height: 0;
  padding: 0;
  background: transparent;
  border: 2px transparent solid;
}

nav a:hover::before {
  animation: animate 0.5s linear forwards;
}

@keyframes animate {
  0% {
    width: 0;
    height: 0;
    border-top-color: white;
    border-right-color: transparent;
    border-bottom-color: transparent;
    border-left-color: transparent;
  }
  50% {
    width: 100%;
    height: 0;
    border-top-color: white;
    border-right-color: white;
    border-bottom-color: transparent;
    border-left-color: transparent;
  }
  100% {
    width: 100%;
    height: 100%;
    border-top-color: white;
    border-right-color: white;
    border-bottom-color: transparent;
    border-left-color: transparent;
  }
}
  nav a::after {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    padding: 0;
    background: transparent;
    border: 2px transparent solid;
  }
  nav a:hover::after {
    animation: animate2 0.5s linear forwards;
  }
  @keyframes animate2 {
    0% {
      width: 0;
      height: 0;
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: transparent;
      border-left-color: white;
    }
    50% {
      width: 0%;
      height: 100%;
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: white;
      border-left-color: white;
    }
    100% {
      width: 100%;
      height: 100%;
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: white;
      border-left-color: white;
    }
    }
<nav>
  <a>TEST</a>
</nav>

Upvotes: 1

Related Questions