Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to animate an SVG background on a DOM element with CSS?

I have used SMIL to animate an SVG image, then used that image as the background for a DOM element (e.g., a button).

For example, this SVG image (http://mattstuehler.com/spinner.svg)

<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="0 0 40 40">
  <g fill="none"  stroke="#1e81cc" stroke-width="4">
    <circle cx="50%" cy="50%" r="18" stroke-opacity="0.3"/>
    <path d="M20,2 A18,18 0 0,1 38,20">
      <animateTransform attributeName="transform" type="rotate" from="0 20 20" to="360 20 20" dur="1s" repeatCount="indefinite"/>
    </path>
  </g>
</svg>

However, now that SMIL is being deprecated, how would you do this with only CSS?

Thanks in advance!

button {
  width: 200px;
  height: 60px;
  background-color: #eee;
  background-image: url(http://mattstuehler.com/spinner.svg);
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 50px 50px;
  border: none;
  -webkit-appearance: none;
}
<button>Hello</button>
like image 733
mattstuehler Avatar asked Sep 23 '16 21:09

mattstuehler


People also ask

Can you animate an SVG background image?

Animated SVG as a Background ImageYou can use data URI to use an animated SVG as a background. In this way, you can use all of the background properties — such as background-repeat , background-color , background-size , and so on — to create the animation you need.

Can I use SVG as background image CSS?

SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. All the same awesomeness of SVG comes along for the ride, like flexibility while retaining sharpness. Plus you can do anything a raster graphic can do, like repeat.

Can CSS background image be animated?

CSS describes how html elements should be render on screen. We can move the background image using CSS3 animation property that gives an illusion of running video. CSS3 animation is supported by all modern browsers.


1 Answers

Update:

Chrome have suspended deprecation of SMIL for now. More here


One way could be to use CSS animations. Animate the path element and fix the transform-origin at the center of the spinner.

Since we can have a <style> tag within an SVG element, we embed this animation in the SVG itself.

CSS Animation / SVG:

<svg height="40" viewbox="0 0 40 40" width="40" xmlns="http://www.w3.org/2000/svg">
  <style>
    path {
      -webkit-animation: rotate 1s linear infinite;
      -moz-animation: rotate 1s linear infinite;
      animation: rotate 1s linear infinite;
      transform-origin: 20px 20px;
    }
    @-webkit-keyframes rotate {
      100% {
        transform: rotate(360deg)
      }
    }
    @keyframes rotate {
      100% {
        transform: rotate(360deg)
      }
    }
  </style>
  <g fill="none" stroke="#1e81cc" stroke-width="4">
    <circle cx="50%" cy="50%" r="18" stroke-opacity="0.3"></circle>
    <path d="M20,2 A18,18 0 0,1 38,20"></path>
  </g>
</svg>

Using the above SVG as a background:

button {
  width: 200px;
  height: 60px;
  background-color: #eee;
  background-image: url(https://dl.dropboxusercontent.com/s/8j3gd6jfujxz2xg/spinner.svg);
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 50px 50px;
  border: none;
}
<button>Hello</button>

Note:
Some browsers may render animated SVGs as a static image when used as a background. Refers to both the CSS method and the SMIL in the question.
Works fine on Chrome/Opera and Firefox from version 51.

like image 175
AA2992 Avatar answered Nov 03 '22 01:11

AA2992