css - React 组件中的内联 CSS 问题

标签 css reactjs

我有一个 react 组件,我正在尝试使用 margin-left 属性对齐下面的 div 。我收到控制台错误:

Unexpected token

指向 margin left 属性中的连字符。谁能帮忙解决这个问题吗?

<div id="loadingDiv" style = {{display:'block'}}>
    <img src={Loading} style = {{width:150,height:150,margin-left:370}} />
</div>

最佳答案

这样写:

style = {{ width : 150, height : 150, marginLeft : 370 }}

不要使用 margin-left,而是使用 marginLeft

原因:

In React, inline styles are not specified as a string. Instead they are specified with an object whose key is the camelCased version of the style name, and whose value is the style's value, usually a string.

margin-left      -->   marginLeft 
padding-top      -->   paddingTop 
background-color -->   backgroundColor

检查DOC .

关于css - React 组件中的内联 CSS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43534384/

相关文章:

reactjs - 打开 Chrome 开发者工具后,React 应用程序速度会慢得像爬行一样。在隐身模式下工作正常

javascript - 在 React.js 中改变复杂状态的最佳方式是什么?

css - Divs 在调整大小时彼此重叠(HTML CSS)

css - Safari 的不同行为和样式

javascript - 查询 : show div on clicking div

html - 如何使用 CSS 在 Google Keep 中制作类似笔记的布局?

javascript - 在 SlateJS 工具栏中看不到图标

javascript - 使用状态切换 CSS 类

php - 为 wordpress 设计主题的最佳方法,需要一些建议

javascript - CSS 转换不适用于 jQuery 的 .toggleClass()