44

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 will get an error.

3
  • 1
    Could you add a code example and error message? Commented Sep 17, 2015 at 15:57
  • React is apparently REACTing silently to faulty css, I was missing parathesis around my scale value Commented Jul 22, 2020 at 20:25
  • Same for me I had transform: "translate(-50%, -50%);" and noticed no difference in styling until I noticed this has been cause by the ; Commented Nov 18, 2020 at 1:27

6 Answers 6

67

Translate also wasn't working for me. I fixed it by adding 'px' behind the var.

ES6 code:

render() {
    const x = 100;
    const y = 100;
    const styles = { 
        transform: `translate(${x}px, ${y}px)` 
    };

    return (
        <div style={styles}></div>
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not awesome that React does not emit an error on this, and it just fails silently. Thanks for the correct answer.
but this it is not taking -webkit-transform
13

My solution was to first concatenate the string and then pass it to the object. Notice the use of 'px' here.

render: function() {

    var cleft = 100;
    var ctop = 100;
    var ctrans = 'translate('+cleft+'px, '+ctop+'px)';
    var css = {
        transform: ctrans 
    }

    return ( 
        <div style={css} />
    )
}

Comments

5

This worked for me:

 transform: 'translateX(-100%)',

Comments

3

If the solutions above aren't working, you can also try formatting the transform differently:

render() {
    const xPx = 50;
    const yPercent = 50;
    return <div style={{transform: `translateX(${xPx}px) translateY(${yPercent}%)`}}></div>
}

Comments

3

This can be done using string template literals ` as your value for the transform key:

const styles = {
logo: {
    transform: `translateX(20px)`
},

function Navbar() {
    return (
        <nav style={styles.nav}>
            <div style={styles.wrapper}>
                <p className="logo" style={styles.logo}>Hello</p>
            </div>

For more on template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Comments

2

You can use translate as a property react provides all translate without specifying transform

style={{
  position: "fixed",
  bottom: "0px",
  left: "50%",
  maxWidth:'450px',
  zIndex: 1,
  translateX:'-50%'
}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.