1

I have implemented a basic animated gradient on text using pure CSS (and some extra DOM elements), as seen here:

.container {
  position: relative;
  display: block;
  width: 300px;
  height: 300px;
  font-size: 60px;
}

.base {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  left: 0px;
  right: 0px;
  top: 0px;
  bottom: 0px;
}

.overlay {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  left: 0px;
  right: 0px;
  top: 0px;
  bottom: 0px;
  background-color: red;
  background: linear-gradient(315deg, red 0%, green 74%);
  background-size: 200% auto;
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: shine 2s linear infinite;
  opacity: 0;
  transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -webkit-transition: opacity 1s ease-in-out;
}

.overlay:hover {
  opacity: 1;
  transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -webkit-transition: opacity 1s ease-in-out;
}


@keyframes shine {
  to {
    background-position: 200% center;
  }
}
<span class="container">
  <span class="base">a</span>
  <span class="overlay">a</span>
</span>

I tried replacing the parts with SVG and using the CSS background-clip property, but it doesn't seem to do anything on SVGs. How can I do this same text gradient effect on SVGs (on hover)?

The SVG I have is a complex hand-drawn owl exported as SVG (looks like a sketch), so it uses lots of complex paths.

Ideally this could be done on a CSS-loaded SVG, rather than using something like this.

Lance
  • 75,200
  • 93
  • 289
  • 503
  • Please take a look at this: https://stackoverflow.com/questions/52811786/how-to-add-animated-gradient-to-an-svg-path – enxaneta Jun 10 '22 at 05:59

0 Answers0