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 Overflow
🌐
Reddit
reddit.com › r/reactjs › css module not working, maybe because of newwindowcomponent.js
r/reactjs on Reddit: CSS module not working, maybe because of NewWindowComponent.js
November 2, 2021 -

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]

🌐
Reddit
reddit.com › r/reactjs › css modules is not working in functional component help?????
r/reactjs on Reddit: CSS Modules is not working in functional component Help?????
March 19, 2020 -

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

Top answer
1 of 2
2

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>;
}
2 of 2
2

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>;
🌐
Create React App
create-react-app.dev › docs › adding-a-css-modules-stylesheet
Adding a CSS Modules Stylesheet | Create React App
January 4, 2019 - import React, { Component } from 'react'; import styles from './Button.module.css'; // Import css modules stylesheet as styles import './another-stylesheet.css'; // Import regular stylesheet class Button extends Component { render() { // reference as a js object return <button className={styles.error}>Error Button</button>; } } Copy ... <!-- This button has red background but not red text --> <button class="Button_error_ax7yz">Error Button</button> Copy
🌐
OpenReplay
blog.openreplay.com › using-css-modules-in-react
Using CSS Modules in React
September 2, 2022 - You only need to change the CSS file name to “[filename].Modules.css”; you can substitute any other name for “[filename]”. You must use the import keyword when using CSS Modules to import the file to a specific component. You must specify the classes when integrating CSS Modules into your React projects, like how you would access an object’s property in standard Javascript using the dot notation or the bracket syntax.
🌐
Medium
medium.com › @ralph1786 › using-css-modules-in-react-app-c2079eadbb87
Using CSS Modules In React App. Welcome back reader to another blog… | by Rafael Cruz | Medium
February 23, 2022 - When using the Create-React-App tool you are provided with a handy command eject that allows you to configure Webpack among other features at your heart content. This is one way to use CSS Modules in a React application. Obviously there are a few more steps you need to complete after you have ejected, but I will not go over them here.
🌐
GitHub
github.com › react-static › react-static › issues › 601
CSS Modules no longer work · Issue #601 · react-static/react-static
May 7, 2018 - After more time spending on the problem and tests, looks like this issue not related to version @5.9.7, at least for me. This is more global vs local install issue, so I solved this by installing react-static locally and using it via node_modules/.bin/react-static ... Regarding my css loader extract-css-chunks-webpack-plugin which is part of react static. Im about to release a next tag build on npm which should get HMR and CSS modules working again.
Find elsewhere
🌐
GitHub
github.com › wojtekmaj › react-calendar › issues › 100
Default CSS not loaded when using CSS modules · Issue #100 · wojtekmaj/react-calendar
August 7, 2018 - import React from 'react'; import Calendar from 'react-calendar'; import classes from './Filter.css'; const filter = (props) => ( <div className={classes.Filter}> <Calendar /> </div> ); Note: I also ejected my CRA and enabled CSS modules · Reactions are currently unavailable ·
Author   myasul
🌐
GitHub
github.com › facebook › create-react-app › issues › 11800
In v5 can't use import * as styles with css modules, bug? · Issue #11800 · facebook/create-react-app
December 18, 2021 - Hello, after upgrading to v5 I can not use anymore named import such as import * as styles from './Table.module.css'; This results in the following error export 'table' (imported as 'styles') was not found in './Table.module.css' (possib...
Author   mikoleg
🌐
Stack Overflow
stackoverflow.com › questions › 69645584 › react-css-modules-is-not-working-correctly
react-css-modules is not working correctly
November 27, 2021 - import React from "react"; import styles from "./myBtn.module.css"; const Component=()=>{ return( <div> <button className={styles.myBtn}>1234</button> </div> ) } export default Component;
🌐
Stack Overflow
stackoverflow.com › questions › tagged › react-css-modules
Newest 'react-css-modules' Questions - Stack Overflow
I'm having trouble with CSS Modules in React when trying to override base styles with external ones. The styles passed via className don't seem to take precedence as expected.
🌐
DhiWise
dhiwise.com › post › troubleshooting-guide-how-to-fix-react-css-not-applying
Troubleshooting Guide: Resolving React CSS Not Applying
May 30, 2024 - CSS specificity conflicts can cause custom CSS to be overridden. Ensure that your CSS rules are specific enough to apply as intended. Verify that your CSS file is being loaded correctly in your React application.
🌐
DEV Community
dev.to › tumee › how-to-use-css-modules-with-create-react-app-27eh
How To Use CSS Modules With create-react-app - DEV Community
June 5, 2020 - If you want create-react-app to treat a CSS file as CSS Module file, you need to use a specific naming convention when naming your CSS file. You need to name your CSS files with .module.css extension. This way create-react-app knows that the file should be treated as CSS Module. After this you can import the CSS file normally and use the class names from it to style your components.
🌐
GitHub
github.com › facebook › create-react-app › issues › 11155
CSS class names with an hyphen(-) dont work in CSS Modules · Issue #11155 · facebook/create-react-app
June 29, 2021 - I am using the latest build of Create React App with the latest versions of react, react-scripts etc. When i went ahead using the CSS Modules (by appending the .module.css to the css file) by having a CSS class with the name Event-Entries. When I imported the css modules as import styles from './Event.module.css and then when i used it in my JSX like <div className={styles.Event-Entries}></div>, I got a compilation error saying "Entries is not defined".
Author   vikramuvs
Top answer
1 of 11
126

You do not need to eject.

Create React App supports CSS Modules right out of the box as of version 2.

Upgrade to the latest (react-scripts@latest) version.

If you use yarn:

Copyyarn upgrade react-scripts@latest

If you use npm:

Copynpm install --save react-scripts@latest

Then you just have to create a file with the extension .module.css

For example:

Copy.myStyle {
  color: #fff
}

Then you can use it like so:

Copyimport React from 'react'
import styles from './mycssmodule.module.css'

export default () => <div className={styles.myStyle}>We are styled!</div>
2 of 11
11

To enable CSS module in to your app, you don't need to eject create-react-app. You can follow these simple steps described in this link to use CSS module in your project.

But in case if you ejected your create-react-app, then you must find file named

"webpack.config.js"

open this file and find this {test:cssRegex....etc} in line no. 391 and replace to this changes:

Copy            {
              test: cssRegex,
              exclude: cssModuleRegex,
              use: getStyleLoaders({
              importLoaders: 1,
              modules: true,
              localIdentName: '[name]__[local]__[hash:base64:5]'
            }),
            },

Open .js file and import statement like this

Copyimport cssStyleClassName from './MyApp.css';

Then this import statement allow you to do following changes in component tags like this

Copy<div className={cssStyleClassName.styleName}></div>

styleName is what you defined in your MyAppStyle.css file

Copy.styleName{
your properties here...
}

After eject your app, you must restart your local server, sometime changes don't get reflect.

Note In earlier version there were two generated file for production and dev separately. Now in current version one file named "webpack.config.js" file for which you need to concerned for changes. Thank you.

🌐
GitHub
github.com › vercel › next.js › issues › 36873
module.css not loading styles · Issue #36873 · vercel/next.js
May 12, 2022 - The only place on the whole project that a css references .live-stream is on App.module.css, I also tried chaging the main element to a div with class 'main' and the changes were not picked up anymore. Its like main tag works but not class names.
Published   May 12, 2022
Author   andres-dejesus
🌐
Stack Overflow
stackoverflow.com › questions › 68071522 › css-modules-in-react-js-not-working-i-have-tried-literally-everything-but-it-di
CSS Modules in React JS not working, i have tried literally everything but it didn't solved
June 21, 2021 - { "name": "burger-builder", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^5.14.1", "@testing-library/react": "^11.2.7", "@testing-library/user-event": "^12.8.3", "react": "^17.0.2", "react-dom": "^17.0.2", "react-scripts": "^1.1.5", "web-vitals": "^1.1.2" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } } ... CSS modules is available with react-scripts@2.0.0 and higher.