81

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?

zealoushacker
  • 6,766
  • 5
  • 35
  • 44

4 Answers4

130

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',
    ]
  }
};
Brigand
  • 84,529
  • 20
  • 165
  • 173
zealoushacker
  • 6,766
  • 5
  • 35
  • 44
  • 6
    Note that the ms prefixes should be lowercase, unlike the capitalized webkit prefixes: https://zhenyong.github.io/react/tips/inline-styles.html – Bryan Downing Oct 29 '17 at 01:31
2

I actually faced the same problem, and none of the react prefixing libraries out there did the job I wanted. So I built one myself:

react-prefixer

It's still pretty young (built it this morning), but I will be maintaining it. Hopefully it helps.

  • Could you please create sandbox with usage examples? I try with MUI components like: import prefix from 'react-prefixer'; const stylesFixed = prefix({ webkitScrollbar: { width: 0, background: 'transparent' }, }); ... But it doesn't work (I try to hide scrollbar in this case). – Vlado Jun 07 '22 at 16:48
1

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/

Samuell
  • 215
  • 2
  • 5
  • prefixfree is awesome, just ensure to re-call window.StyleFix.process() if your page content updates (e.g. router navigation callback) – Nick Oct 29 '15 at 05:18
1

You can use something like this:

https://github.com/cgarvis/react-vendor-prefix

to automatically vendor-prefix your style objects.

fabio.sussetto
  • 6,964
  • 3
  • 25
  • 37