Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I apply vendor prefixes to inline styles in reactjs?

CSS properties in React are not automatically added with their vendor prefixes.

For example, with:

<div style={{
    transform: 'rotate(90deg)'
}}>Hello World</div>

In Safari, the rotation wouldn't be applied.

How do I get that accomplished?

like image 287
zealoushacker Avatar asked Aug 19 '15 16:08

zealoushacker


People also ask

How do you change the style of inline React?

To set inline styles in React: Set the style prop on the element to an object. Set the specific CSS properties and values to style the element. For example, <div style={{backgroundColor: 'salmon', color: 'white'}}> .

Does React use inline styles?

The official React documentation frowns upon the use of inline styling as a primary means of styling projects and recommends the use of the className attribute instead.

Which vendor specific prefix do you have to use to target opera?

The major browsers use the following prefixes: -webkit- (Chrome, Safari, newer versions of Opera, almost all iOS browsers including Firefox for iOS; basically, any WebKit based browser) -moz- (Firefox)


Video Answer


2 Answers

React does not apply vendor prefixes automatically.

In order to add vendor prefixes, name the vendor prefix as per the following pattern, and add it as a separate prop:

-vendor-specific-prop: 'value'

becomes:

VendorSpecificProp: 'value'

So, in the example in the question, it needs to become:

<div style={{
    transform: 'rotate(90deg)',
    WebkitTransform: 'rotate(90deg)'
}}>Hello World</div>

Value prefixes can't be done in this way. For example this CSS:

background: black;
background: -webkit-linear-gradient(90deg, black, #111);
background: linear-gradient(90deg, black, #111);

Because objects can't have duplicate keys, this can only be done by knowing which of these the browser supports.

An alternative would be to use Radium for the styling toolchain. One of its features is automatic vendor prefixing.

Our background example in radium looks like this:

var styles = {
  thing: {
    background: [
      'linear-gradient(90deg, black, #111)',

      // fallback
      'black',
    ]
  }
};
like image 139
zealoushacker Avatar answered Oct 07 '22 03:10

zealoushacker


I don't have experience with react.js, but look at this js library from Lea Verou. It prefixes style direct in DOM.

http://leaverou.github.io/prefixfree/

like image 1
Samuell Avatar answered Oct 07 '22 03:10

Samuell