Using tailwind css, I can center an absolutely positioned element in its relative container: <svg class="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2">...</svg>
Using tailwind css, I can make an absolutely positoined element spin: <svg class="absolute animate-spin">...</svg>
But when I try to center an absolutely positioned spinning element, <svg class="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 animate-spin">...</svg>
, instead of spinning, the non-spinning element just moves down and to the right at a -45deg angle.
What am I doing wrong?
3
Answers
'-translate-x-1/2 -translate-y-1/2' include an associated 'transform', but 'animate-spin' also includes a 'transform' within a @keyframe.
It seems that these two transforms collide without being merged, possibly depending on the order of the keywords.
My workaround: a wrapper element
As I understand, when you use both translation classes, i.e.,
-translate-x-1/2
and-translate-y-1/2
with the classanimate-spin
, it is very likely to cause this behavior.Solution: A solution approach would be to also use
transform
class along with the above 3 mentioned classes.Reason: By adding
transform
class, it ensures that the two translational classes are grouped for performing transformation rather than changing position.In this way, the element will have the properties of both center and spin as per your requirement.
tailwind uses this css:
and also
Solution: I think this behavior is because spin and translate both manipulate transform property.
so you can wrap the svg in a div and set position related classes to that div and for svg only use animation class: