简体   繁体   English

chrome和Firefox的内联样式

[英]Inline style for chrome and firefox in react

I am using some inline styles for div in react. 我在div中使用一些内联样式作为div。 So, I've used width: max-content for the div. 因此,我已将width:max-content用于div。 It's working fine in chrome, but in firefox, I have to use -moz-max-content. 在chrome中工作正常,但是在Firefox中,我必须使用-moz-max-content。 So, is there any way to specify styles for both, chrome and firefox, using inline styles in react? 那么,有没有办法在React中使用内联样式为chrome和firefox指定样式?

const divStyle = {
    margin: 'auto',
    width: 'max-content',
    fontSize: 24
  }

I can't use another width key for firefox, it throws me an error. 我不能将另一个宽度键用于Firefox,这会引发错误。

My react version is 15.6.1 我的反应版本是15.6.1

If you will be doing inline-styling in reactjs, then you better use libraries like Radium . 如果您将在reactjs中进行内联样式化,那么最好使用Radium之类的库。 It supports automatic vendor prefixing. 它支持自动供应商前缀。

Other examples are glamorous css and glamor Both these libraries support vendor prefixing. 其他示例是迷人的CSSGlamour这两个库都支持供应商前缀。

This (adding multiple fallback values for the same property using inline styles) is not officially supported, and probably will never be. 官方不支持此功能(使用内联样式为同一属性添加多个后备值),可能永远不会。 See this comment by Dan Abramov (and read the whole discussion on the github issue for some history): 请参阅Dan Abramov的评论(并阅读有关github问题的全部讨论以了解历史):

https://github.com/facebook/react/issues/6467#issuecomment-354781011 https://github.com/facebook/react/issues/6467#issuecomment-354781011

The solution he suggests: 他建议的解决方案:

For example you can use document.createElement('div') to create a DOM node, and then assign .style.myPrefixedProperty and see if it “sticks” by reading from it. 例如,您可以使用document.createElement('div')创建一个DOM节点,然后分配.style.myPrefixedProperty并通过对其进行读取来查看它是否“粘住”。

Simply you could use [Autoprefixer][1] for Object Style 只需使用[Autoprefixer] [1]作为对象样式

If you're still using npm, you may run npm i --save inline-style-prefixer . 如果仍在使用npm,则可以运行npm i --save inline-style-prefixer

import Prefixer from 'inline-style-prefixer'

const divStyle = {
    margin: 'auto',
    width: 'max-content',
    fontSize: 24
  }

const prefixer = new Prefixer()
const prefixedStyle = prefixer.prefix(divStyle)

They also provide UMD links 他们还提供UMD链接

<!-- Unminified versions -->
<script src="https://unpkg.com/inline-style-prefixer@3.0.1/dist/inline-style-prefixer.js"></script>
<script src="https://unpkg.com/inline-style-prefixer@3.0.1/dist/inline-style-prefix-all.js"></script>
<!-- Minified versions -->
<script src="https://unpkg.com/inline-style-prefixer@3.0.1/dist/inline-style-prefixer.min.js"></script>
<script src="https://unpkg.com/inline-style-prefixer@3.0.1/dist/inline-style-prefix-all.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM