4

I have a css file named App.css which has css like

.abcd {
    margin-top: 50px !important;
    color: red !important;
}

I am importing this and using in my react app like

import styles from './App.css'
console.log("style", styles) // it gives object. 

<div className={"ui equal width grid " + styles.abcd}>
</div>

Here styles.abcd is showing undefined but when I console styles it gives object with that classname on array.. I dont know what is missing here.

My webpack conf looks like:

{
    test: /\.css$/,
    loader: 'css-loader',
    options: {
        modules: true,
        localIdentName: '[path][name]__[local]--[hash:base64:5]',
    },
},

I have also tried

{
    test: /\.css$/,
    use: ['style-loader', 'css-loader'],
},

Nothing is working. Why is it giving undefined ?

7
  • Have you looked in your bundle to see what webpack is producing? Commented Dec 11, 2018 at 14:37
  • Maybe it's a problem with "ui equal width grid " Commented Dec 11, 2018 at 15:20
  • you should import your css like this if its a file import styles from './App.css' Commented Dec 11, 2018 at 15:54
  • what if <div className={"ui equal width grid abcd"}> </div> ? Commented Dec 11, 2018 at 16:22
  • @sdgluck I get an object with class name when I console my styles variable. but when I try to use its attribute styles.abcd , its giving me undefined Commented Dec 11, 2018 at 16:33

2 Answers 2

0

Similar problem answered here: CSS Modules class names undefined

In the docs, it's mentioned that you have to name the file as follow :

[name].module.css

Try renaming the file to : Catalog.module.css and import it like this :

import styles from './Catalog.module.css';

In your case you should name it App.module.css and import as such.

Sign up to request clarification or add additional context in comments.

1 Comment

[name].module.css is just one way to implement modules, such as test: /module.css$/i. However, the question has already forcibly enabled modules for the .css file.
-1

Your code is :

import styles from './App.css'
console.log("style", styles) // it gives object. 

<div className={"ui equal width grid " + styles.abcd}>
</div>

Here it should have been :

<div className={`ui equal width grid ${styles.abcd}`}>

1 Comment

Both represent the same string in a different format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.