[英]I would like to style JS file in react using CSS styling but I'm getting an error
import React from 'react'
import './myStyle.css'
function Stylesheet(){
return(
<div>
<h1>Stylesheet</h1>
</div>
)
}
export default Stylesheet
This I'm getting an error这我得到一个错误
./src/Components/stylesheet.js Module not found: Can't resolve './myStyle.css' in 'D:\Documents\WebProject\React\hello-world\src\Components'
./src/Components/stylesheet.js 未找到模块:无法解析 'D:\Documents\WebProject\React\hello-world\src\Components' 中的 './myStyle.css'
There is no need to create a component for styling a component.无需为组件样式创建组件。 Just write your css style and import it like
import './myStyle.css'
只需编写您的 css 样式并像
import './myStyle.css'
一样导入它
There are different options to style React components.I will explain few options here. React 组件的样式有不同的选项。我将在这里解释几个选项。
CSS Stylesheet: CSS 样式表:
Add a Stylesheet.css
file as below.添加一个
Stylesheet.css
文件,如下所示。
.boxColor {
background-color: red;
}
Add a component Stylesheet.js
.添加一个组件
Stylesheet.js
。 Simply import css file import './Stylesheet.css'
只需导入 css 文件
import './Stylesheet.css'
import React from 'react';
import './Stylesheet.css';
const Stylesheet = () => (
<div className="boxColor">
<p>Get started with CSS styling</p>
</div>
);
export default Stylesheet;
Inline styling : In React, inline styles are not specified as a string.内联样式:在 React 中,内联 styles 未指定为字符串。 Instead they are specified with an object.
相反,它们使用 object 指定。
Add Stylesheet.js
file.添加
Stylesheet.js
文件。
import React from 'react';
const boxColor = {
background-color: red;
};
const Stylesheet = () => (
<div style={boxColor}>
<p>Get started with CSS styling</p>
</div>
);
export default Stylesheet;
style={boxColor}
style={boxColor}
之类的元素style={{background-color: 'red'}}
style={{background-color: 'red'}}
There few more ways like Styled-component and CSS Modules to style react component.还有一些像Styled-component和CSS Modules这样的方式来设置 react 组件的样式。
for example,you have two files, first is practice.js and second is style.css then import your style.css in practice.js as following.例如,您有两个文件,第一个是practice.js ,第二个是style.css然后在practice.js中导入您的style.css ,如下所示。
practice.js file练习.js 文件
import React from 'react';
import './style.css'; //importing style sheet
export default function practice() {
return (
<div>
<h1 className="heading">Hellooooooo</h1>
</div>
)
}
and your style.css file is你的 style.css 文件是
.heading{
color: white;
background-color: black;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.