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?
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) }
}