0

Am trying to use keyframes to change the background color of an a tag

My code:

.button-container {
  width: 200px;
  height: 40px;
  display: flex;
  margin: 0 auto;

}
.button-container a{
  width: inherit;
  height: inherit;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: linear-gradient(to right, #000, #000);
}

.button-container:hover a{
  animation: colorswitch 5s linear;
}

@keyframes colorswitch {
  from { 
    background-image: linear-gradient(to right, #ddd , #ddd);
  }
  to { 
    background-image: linear-gradient(to right, #ddd , #ddd);
  }
}
 <div class="button-container">
   <a href="#">View here</a>
 </div>

How can I change the background color of the a tag from #000 to #ddd moving from left to right when I hover over it?

Jérôme
  • 978
  • 1
  • 9
  • 22
Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99

1 Answers1

1

Here's a pattern using linear gradent, background position, and css transition.

Right to left:

button {
  background-image: linear-gradient(to right, red 40%,  blue 60%);
    
 background-size: 300% 100%;
  transition: background-position .5s;
  background-position: 0;
  
  border: none;
  padding: 1em;
}


button:hover {
   background-position: 100%;
}
<button>A button</button>

Left to right:

button {
  background-image: linear-gradient(to right, red 40%,  blue 60%);
    
 background-size: 300% 100%;
  transition: background-position .5s;
  background-position: 100%;
  
  border: none;
  padding: 1em;
}


button:hover {
   background-position: 0%;
}
<button> A button</button>
WillD
  • 5,170
  • 6
  • 27
  • 56