Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 transform rotate without overriding transform translate [duplicate]

Tags:

css

transform

Let's say I have an element that already has a translate applied to it:

<div class="elem"></div>

.elem { transform: translate(50%,0); }

And now I want to rotate the same element using an additional class. So now the HTML and CSS looks like this:

<div class="elem rotate"></div>

.elem { transform: translate(50%,0); }
.rotate { transform: rotateZ(20deg); }

The problem occurs when the rotation is not added to the existing translate, but rather overrides and negates it.

How do I preserve the original translate without having to bloat up my code like I did below:

.elem { transform: translate(50%,0); }
.rotate { transform: translate(50%,0) rotateZ(20deg); }

Here's a fiddle: https://jsfiddle.net/timothyzhu/vm84vsj7/

Thanks guys.

like image 897
Tim Avatar asked May 25 '26 12:05

Tim


1 Answers

Setting a transform property on an element will override any other transform property set previously on the same element.

One common pattern in this situation is use JS to store the value and then apply transformation taking into account the previous transform property(also because the sequence of transformations is important!!).

In your scenario, a workaround could be creating a wrapping div just for rotation (here's a complicated example look at it with dev-tools),

fiddle

like image 69
maioman Avatar answered May 27 '26 23:05

maioman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!