在反应应用中导入CSS

时间:2018-05-31 16:05:03

标签: reactjs

create react app文档中,它说App.css是在App.js中导入的。是否还有一种从组件加载已编译的css文件的方法?

// MyViewComponent

import styles from '../../App.css';

2 个答案:

答案 0 :(得分:3)

您可以在组件中导入如下所示的css文件

  

导入' ./ header.css';

Imagine your header.css file looks like 

.header {
    background-color:  rgb(49, 118, 197);
    height:100px;
    border:10px solid red;
}

To use the style, you can use the class like ,

<div className="header">Hello World</div>

希望这会有所帮助:)

答案 1 :(得分:2)

对于css文件,您可以像

一样导入它们
import "../../App.css"

将导入所有选择器&amp;该文件中的CSS规则。

如果您尝试使用以下内容设置组件中的各个元素的样式:

<div style={myStyles.wrapper} />

然后你需要从文件中导出JS对象

例如:

export default {
    wrapper: {
       background: "red"
    }
}

然后你可以导入并使用它

import myStyles from "../myStyles.js"

<div style={myStyles.wrapper} />