4

I have a button but when I hover over it the text doesn't want to change color (color change class---- .active:hover). For some odd reason it doesn't want to change. Also, as you can see in my code, I want to change the text color to #fff (white). I use the :hover selector quite often, and I don't know what the problem is!

body {
  font-family: "Exo 2", sans-serif;
  margin: 0;
  padding: 0;
  justify-content: center;
  height: 100vh;
  align-items: center;
}
.active {
  text-decoration: none;
  color: #000;
  position: relative;
  padding: 8px 30px;
  background: #fff;
  text-transform: uppercase;
  font-size: 15px;
  box-sizing: border-box;
  transition: 0.5s;
  border: 2px solid #000;
}
.active:before {
  content: '' ;
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  background: #000;
  z-index: 1;
  transform: scaleX(0);
  transition: transform 0.5s;
  transform-origin: left;
}
.active:hover {
  color: #fff;
}
.active:hover:before {
  transform: scaleX(1);
  transition: transform 0.5s;
  transform-origin: right;
}
.active:after {
  content: '' ;
  position: absolute;
  top: 0;
  right: 0;
  width: 50%;
  height: 100%;
  background: #000;
  z-index: 1;
  transform: scaleX(0);
  transition: transform 0.5s;
  transform-origin: right;
}

.active:hover:after {
  transform: scaleX(1);
  transition: transform 0.5s;
  transform-origin: left;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
       <a class="active" href="#">Login</a>
      
 </body>
</html>
Jodast
  • 1,279
  • 2
  • 18
  • 33
Ethical38
  • 77
  • 9

2 Answers2

4

You are placing your :before element over the text with that z-index value. Based on all the variants you have, included the background of the button as well, I will recommend you to use a <span> for the text and use a higher z-index on that element :

body {
  font-family: sans-serif;
  margin: 30px;
  text-align: center;
}

.active {
  text-decoration: none;
  color: #000;
  position: relative;
  padding: 8px 30px;
  background: #fff;
  font-size: 15px;
  transition: 0.5s;
  border: 2px solid #000;
}

.active span {
  position: relative;
  transition: color .3s linear;
  z-index: 2
}

.active:before,
.active:after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  background: #000;
  z-index: 1;
  transform: scaleX(0);
  transition: transform 0.5s;
  transform-origin: left;
}

.active:after {
  left: auto;
  right: 0;
  transform-origin: right;
}

.active:hover span {
  color: #fff;
}

.active:hover:before,
.active:hover:after {
  transform: scaleX(1);
  transition: transform 0.5s;
}
<a class="active" href="#"><span>LOGIN</span></a>
DaniP
  • 37,813
  • 8
  • 65
  • 74
2

You can achieve the same using only background coloration and no more need pseudo element

body {
  font-family: "Exo 2", sans-serif;
}
.active {
  text-decoration: none;
  color: #000;
  position: relative;
  padding: 8px 30px;
  display:inline-block;
  margin:10px;
  background-image:
    linear-gradient(#fff,#fff),
    linear-gradient(#000,#000);
  background-size:100% 200%,0% 200%;
  background-position:left 50% top -100%,50% 50%;
  background-repeat:no-repeat;
  text-transform: uppercase;
  font-size: 15px;
  box-sizing: border-box;
  transition:background-size 0.5s,background-position 0s 0.5s;
  border: 2px solid #000;
}
.active:hover {
  color:#fff;
  background-size:0% 200%,800% 200%;
  background-position:50% 50%;
  transition:background-size 1s,background-position 0s 1s;
}
<a class="active" href="#">Login</a>

Also like this:

body {
  font-family: "Exo 2", sans-serif;
}
.active {
  text-decoration: none;
  color: #000;
  position: relative;
  padding: 8px 30px;
  display:inline-block;
  margin:10px;
  background-image:
    linear-gradient(#000,#000),
    linear-gradient(#000,#000);
  background-size:0% 100%;
  background-position:left,right;
  background-repeat:no-repeat;
  text-transform: uppercase;
  font-size: 15px;
  box-sizing: border-box;
  transition:0.5s;
  border: 2px solid #000;
}
.active:hover {
  color:#fff;
  background-size:50.5% 100%;
}
<a class="active" href="#">Login</a>

Like this too:

body {
  font-family: "Exo 2", sans-serif;
}
.active {
  text-decoration: none;
  color: #000;
  position: relative;
  padding: 8px 30px;
  display:inline-block;
  margin:10px;
  background-image:
    linear-gradient(#000,#000);
  background-size:0% 100%;
  background-position:center;
  background-repeat:no-repeat;
  text-transform: uppercase;
  font-size: 15px;
  box-sizing: border-box;
  transition:0.5s;
  border: 2px solid #000;
}
.active:hover {
  color:#fff;
  background-size:100% 100%;
}
<a class="active" href="#">Login</a>

More details about the value calculation here: https://stackoverflow.com/a/51734530/8620333

Temani Afif
  • 245,468
  • 26
  • 309
  • 415