Last active
August 20, 2022 18:24
-
-
Save guylaor/31d859f91bd5e601fccb to your computer and use it in GitHub Desktop.
Using CSS transform: translate(...) with ReactJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render: function() { | |
var cleft = 100; | |
var ctop = 100; | |
var ctrans = 'translate('+cleft+'px, '+ctop+'px)'; | |
var css = { | |
transform: ctrans | |
} | |
return ( | |
<div style={css} /> | |
) | |
} |
Here's another way that worked for me:
<div style={{ transform: `translate(10px, 20px)` }}></div>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to https://facebook.github.io/react/tips/inline-styles.html
CSS styles need to be passes as an object to the component. However, if you are trying to use transform styles, you need to first concatenate the string and then pass it to the object. Otherwise, you will get an error.