0

So I am trying to add some css style to my react components but failed. My webpack.config.js looks like:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, './build');
var APP_DIR = path.resolve(__dirname, './src/client');

const config = {
   entry: {
     main: APP_DIR + '/index.js'
   },
   output: {
     filename: 'bundle.js',
     path: BUILD_DIR,
   },
   module: {
    rules: [
     {
       test: /\.css$/,
       use: [{
           loader: "style-loader"
       }, {
           loader: "css-loader?modules=true&camelCase=true"
       }]
     },
     {
       test: /\.(jsx|js)?$/,
       use: [{
         loader: "babel-loader",
         options: {
           cacheDirectory: true,
           presets: ['@babel/preset-react']
         }
       }]
     }
    ],

  }
};

module.exports = config;

My client code folder looks like:

Client
--style
----index.css
--index.js

index.css looks like:

body{
    color: #555;
    background: #f85032;
    margin: 10px 30px;
}

Inside index.js, I am loading the css file using

import css from './style/index.css';

Then I do:

npx webpack
npm start

There's no error message in console output. The webpage shows up but there's no css style. Can someone please help me with this? Thanks!

It appears that if I do some inline css in index.html then it works? Any suggestion why this happens? Thanks!

2 Answers 2

1

Change to import './style/index.css'; and see if that works

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

2 Comments

Try changing body{} to :global(body) {} in index.css file @Rockingchief
It appears that if I do some inline css in index.html then it works? Any suggestion why this happens? Thanks!
0

I am just guessing here, since i can not see your index.js

From your webpack file i can see that you are using css modules. This means that you can not just assign classes as you would usually do it, but you must get them from the css you imported.

Hence

'<div className="className">'

Becomes

'<div class=Name"' + css.className + '">'

The reason is thay css modules is doing some clever naming to always make the imported css unique to ensure you are not having any global scoping and conflicts (which is what you want with css modules)

UPDATE

I have tried to create a local setup with your webpack config. You can download it here (It is just a zip file).

Unzip and enter folder then run npm install and npm run webpack. You can now open build/index.html and see the result.

Maybe then you can see what you are doing differently?

9 Comments

not sure what you mean. But right now inside my css file I only have a body selector and don't have any id or class selectors. Thanks!
It appears that if I do some inline css in index.html then it works? Any suggestion why this happens? Thanks!
What happens if you replace loader: "css-loader?modules=true&camelCase=true" with loader: "css-loader" in your webpack config
So I just did that but it does not fix the problem and nothing changed.
Just to make sure, you did also restart webpack right, just to make sure. However css modules might not be the problem if the other answer by Vu did not work. Inline styles work since that is not using your css files or classes. The problem here seems to be that the css is not being loaded
|

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.