I am facing an issue with my component's CSS structure and import method.
About/style.css
.AboutContainer {
# Some style
}
p > code {
# Some style
}
When importing the CSS in the component:
About/index.js
import './style.css';
export default class About extends Component {
render() {
# Return some component
}
}
The problem is that the CSS is being imported globally in the <header>
section.
I expected the CSS to be:
- Scoped to the component so that it only applies to elements rendered within this specific component.
- Disappearing when the component is unmounted.
However, upon inspection, I found that the styles are specified in the <header>
section and applied to all components.
<header>
// Stuff
<style type="text/css">style for component About</style>
<style type="text/css">style for component B</style>
<style type="text/css">style for component C</style>
// Stuff
</header>
How can I import CSS to be scoped to the component? It seems like I may not fully understand how CSS imports work in React ES6.
I initially followed this tutorial
Edit
Brett's response was helpful, but it turns out that my issue lies elsewhere. I created my app using create-react-app, which simplifies the initial setup for React projects by including WebPack, Babel, and other tools. The default WebPack configuration did not enable the module option for the css-loader
, causing local scoping to be disabled.
For those seeking additional information, it appears that create-react-app does not provide a straightforward way to customize the WebPack config, but there are various workarounds available online.