I had this same problem and solved it by renaming my css file to:
myName.module.css
and also importing like this:
import styles from './myName.module.css'
There is no need to follow the steps of updating the webpack files any more.
Answer from pflevy on Stack OverflowI had this same problem and solved it by renaming my css file to:
myName.module.css
and also importing like this:
import styles from './myName.module.css'
There is no need to follow the steps of updating the webpack files any more.
in react 16.13 and [email protected] and higher you don't need to eject your project.
Your CSS files must be named camelCase + .modules.css and import to your projects like this:
import React, { Component } from 'react';
import styles from './app.module.css'; // Import css modules stylesheet as styles
class App extends Component {
render() {
return <button className={styles.test}>Error Button</button>;
}
}
export default App;
and in app.module.css
.test{
color: blue;
}
if you eject projects, in webpack.config.js file, in css modules change like this:
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
modules: true,
Right now I made another component that is on a new window called Blog.js. In-line styling is convenient but eventually it will get crowded and annoying to edit. I want to make another stylesheet using CSS module called Blog.module.css but when I style in it and import in Blog.js, it clashes with App.js component instead. Here's my code on stackoverflow: [https://stackoverflow.com/questions/69792331/how-do-i-make-a-new-css-file-for-a-new-component-in-react-js]
Videos
i am facing issue when i am trying to scope css file to specific component in case of class component its working fine by following this article https://create-react-app.dev/docs/adding-a-css-modules-stylesheet
but in functional component this approach is not working
here is some my application details
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-scripts": "3.4.0"
here is my component
import React, { Component } from 'react';
import LS from './Layout.module.css';
// import './Layout.css'
import Aux from '../../hoc/Aux';
// const layout = (props) => (
//
// Toolbar Sidebar BackDrop
//
// {props.children}
//
//
// );
class layout extends Component {
render() {
return (
Toolbar Sidebar BackDrop
{this.children}
);
}
}
export default layout;
when i apply class approach the css is only scope to the layout file but i want to use finctional component but the scope css is not working when i use functional component
If you intend to create a CSS module then use it in your components for scoping purposes, according to CRA you should define your CSS files with module extension.
When you don't use .module extension for your stylesheet files, you should import them like usual and you can't scope them as well.
So it will be something like this:
import "./BurgerIngredients.css";
... // other parts of your component
render(){
return <div className="BreadBottom"></div>;
}
But if you want to use the CSS module and scope your styles, your stylesheet file should be BurgerIngredients.module.css instead of BurgerIngredients.css then you can import it without any scoping concern just like you did earlier.
import classes from "./BurgerIngredients.module.css";
... // other parts of your component
render(){
return <div className={classes.BreadBottom}></div>;
}
The mistake you are doing here is your are calling your css with a name
Example:
import classes from "./BurgerIngredients.css";
And then you are using inline to append the class name like this
Example:
<div className={classes.Meat}></div>;
Your css class names are not object so you cannot call it like this.
Instead all you have to do is just import the css like this (Make sure css file is in the same folder as the js file because here we are calling the file by ./ )
Example:
import "./BurgerIngredients.css";
And use the className just like you would do in a normal html like this
Example:
<div className="BreadBottom"></div>;
rename
addTask.cssto
addTask.module.cssand change the import to
import styles from './addTask.module.css'
In order for react to understand your css file is a css-module, you need to add the suffix .module to the file name while using a react-script version newer than 2.0.0.
Simply rename addTask.css to addTask.module.css and change the import to:
import styles from './addTask.module.css'
» npm install react-css-modules