0

I want to add a spinning animation to the middle of a button, like this:

.button {
  position: absolute;
  display: inline-block;
  border: 1px solid grey;
  padding: 20px;
}

.button::after {
    content: "";
    position: absolute;
    border: 8px solid #ddd;
    border-radius: 50%;
    border-top: 8px solid #008;
    border-bottom: 8px solid #008;
    width: 64px;
    height: 64px;
    animation: spin 2s linear infinite;  /* without the animation, everything is positioned perfectly */
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

The animation looks as expected, however the element is not centered. When the "animation" property is removed, the element is positioned properly. How can I position the loading animation to the center of the button?

https://jsfiddle.net/f9993ttm/

nagy.zsolt.hun
  • 6,292
  • 12
  • 56
  • 95
  • Consider adjusting the positioning properties in the following manner: https://jsfiddle.net/f9993ttm/3/ – UncaughtTypeError Jan 10 '18 at 22:39
  • Possible duplicate of [How to keep origin in center of image in scale animation?](https://stackoverflow.com/questions/47820827/how-to-keep-origin-in-center-of-image-in-scale-animation) – Temani Afif Jan 10 '18 at 22:41
  • https://stackoverflow.com/questions/47955244/endless-rotating-div-but-with-absolute-positioning/47955262#47955262 – Temani Afif Jan 10 '18 at 22:43

1 Answers1

0

Your animation is overriding your transform property.

Try something more like :

@keyframes spin {
    0% { transform: translate(-50%, -50%) rotate(0deg) }
    100% { transform: translate(-50%, -50%) rotate(360deg)  }
}
mcarter
  • 88
  • 6