How to change color of text from left to right along with the background?

3.9k Views Asked by At

I have an element that, when hovered, changes the background color gradually from left to right. However, I wanted to change not only the background color, but also the color of the text, exactly at the same time/pace. How can I achieve this?

This is my relevant CSS:

.left-to-right {
  color: white;
  background-size: 200% 100%;
  background-image: linear-gradient(to right, #011228 50%, #FF7800 50%);
  transition: background-position 1s;
}

.left-to-right:hover {
    background-position: -100% 0;
    color: #011228;
}

Here's the fiddle.

1

There are 1 best solutions below

1
On BEST ANSWER

One posibility is to use background-clip: text. But unfortunately the support is very poor.

.test {
  height: 66px;
  width: 200px;
  background-size: 200% 100%;
  background-image: linear-gradient(to right, lightblue 50%, blue 50%), linear-gradient(to right, green 50%, lightgreen 50%);
  transition: background-position 6s;
    text-indent: 28px;
    font-size: 60px;
    background-size: 200% 100%;
    background-repeat: no-repeat;
    background-position: 100% top, 100% top;
    -webkit-background-clip: text, border-box;
    background-clip: text, border-box;
    color: transparent;
}

.test:hover {
 background-position: 0% top, 0% top;
}
<div class="test">TEST</div>