For Chromium-based browsers, you can use the new
@property
CSS rule to animate the gradient without needing to use Javascript:
@property --a {
syntax: '<percentage>';
inherits: false;
initial-value: 0%;
}
@property --b {
syntax: '<percentage>';
inherits: false;
initial-value: 0%;
}
button {
font-size: 3rem;
}
.coloring {
transition: --a 1.5s, --b 1.5s;
background: linear-gradient(109.6deg, rgb(135, 168, 254) var(--a), rgb(254, 165, 197) var(--b), #eee var(--b), #eee 101%);
}
.coloring:hover {
--a: 11.2%;
--b: 100.2%;
}
Demo[
^]
@property - CSS: Cascading Style Sheets | MDN[
^]
This is not supported in Firefox; the background will not animate, but simply "snap" between the two states.