Skip to content

Instantly share code, notes, and snippets.

@chenglou
Last active December 22, 2015 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenglou/6431330 to your computer and use it in GitHub Desktop.
Save chenglou/6431330 to your computer and use it in GitHub Desktop.

Q&A format

Problem

You want to put inline style to an element.

Solution

Instead of writing a string, create an object whose key is the camelCased version of the style name, and whose value is the style's value, in string:

/** @jsx React.DOM */

var divStyle = {
  color: 'white',
  backgroundColor: 'lightblue',
  WebkitTransition: 'all' // note the capital 'W' here
};

React.renderComponent(<div style={divStyle}>Hello World!</div>, mountNode);

Discussion

Style keys are camelCased in order to be consistent with accessing the properties using node.style.___ in DOM. This also explains why WebkitTransition has an uppercase 'W'.

Tips format

In React, inline styles are nto specified as a string, but as an object whose key is the camelCased version of the style name, and whose value is the style's value in string:

/** @jsx React.DOM */

var divStyle = {
  color: 'white',
  backgroundColor: 'lightblue',
  WebkitTransition: 'all' // note the capital 'W' here
};

React.renderComponent(<div style={divStyle}>Hello World!</div>, mountNode);

Style keys are camelCased in order to be consistent with accessing the properties using node.style.___ in DOM. This also explains why WebkitTransition has an uppercase 'W'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment