admin管理员组文章数量:1323336
I would like to apply fallback style properties to a ponent. For instance:
var inlineStyle = {
display: '-webkit-box',
display: '-webkit-flex',
display: '-moz-box',
display: '-moz-flex',
display: '-ms-flexbox',
display: 'flex'
}
return <div style={inlineStyle}>{divContent}</div>;
Is there any way to do this? At the moment they are being ignored.
I would like to apply fallback style properties to a ponent. For instance:
var inlineStyle = {
display: '-webkit-box',
display: '-webkit-flex',
display: '-moz-box',
display: '-moz-flex',
display: '-ms-flexbox',
display: 'flex'
}
return <div style={inlineStyle}>{divContent}</div>;
Is there any way to do this? At the moment they are being ignored.
Share Improve this question asked Nov 10, 2014 at 8:11 jossmacjossmac 2183 silver badges8 bronze badges 1- Since asking this question dozens of CSS in JS solutions, some with vendor-prefixing, have popped up. Michele Bertoli has piled a great list here github./MicheleBertoli/css-in-js. At the moment I favour Khan Academy's "Aphrodite" github./Khan/aphrodite – jossmac Commented Sep 8, 2016 at 12:42
2 Answers
Reset to default 3There is this issue (https://github./facebook/react/issues/2020) on React Github page where Paul O’Shannessy states that this is not possible for now.
You should either:
- feature/browser detect before specifying the style value or
- use regular CSS
Each declaration overrites the last since they all have the same key name. Object literal syntax is problematic when the property value needs prefixing, even today (9/6/2016) there is no baked in solution.
Myself, needed to solve this problem, wanted to stay in CSS-in-JS land (hurray for fully self contained ponents) and not add a build step so made a package that keeps things simple called called Style It that simply lets you write CSS.
npm install style-it --save
Functional Syntax (JSFIDDLE)
import React from 'react';
import Style from 'style-it';
class Intro extends React.Component {
render() {
return Style.it(`
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: space-around;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
`,
<ul class="flex-container">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
</ul>
);
}
}
export default Intro;
JSX Syntax (JSFIDDLE)
import React from 'react';
import Style from 'style-it';
class Intro extends React.Component {
render() {
return (
<Style>
{`
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: space-around;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
`}
<ul class="flex-container">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
</ul>
</Style>
}
}
export default Intro;
HTML / CSS pulled from Chris Coyier's A Complete Guide to Flexbox post.
本文标签: javascriptHow can I apply fallback style properties to a React JS componentStack Overflow
版权声明:本文标题:javascript - How can I apply fallback style properties to a React JS component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742137208a2422422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论