2

I'm trying to import CSS as modules to a react app. I have added this to my webpack.config.js file

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

This is the implementation

app.tsx

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

class App extends React.Component {
  render() {
    return (
      <div className={styles.mainContent}>
        <h1> Hello, World! </h1>
        <Button type="primary">Antd works!</Button>
      </div>
    );
  }
}

styles.module.css

.mainContent {
    color: limegreen;
}

But this CSS class has no effect on the output.

1
  • Have you tried this without the options: { modules: true } statement? It should work fine as is without that clause. Commented Jul 30, 2020 at 15:24

2 Answers 2

1

If you want to change the global design of antd you can use a less loader as described in the antd docs for custom theme with webpack config.

If you only want to change the button color you can use plain css. E.g.:

.ant-btn-primary {
  background: red;
  border-color: red;
}

Here is a CodeSandbox with a custom button color.

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

Comments

0

to avoid duplicated class names, css module transforms the class names at compilation.

so you will not be able to override css classes using css modules, because the class names will always be different from each other.

in order to override css class you need to use plain css, eg.

import './styles.css';

class App extends React.Component {
  render() {
    return (
      <div className="mainContent">
        <h1> Hello, World! </h1>
        <Button type="primary">Antd works!</Button>
      </div>
    );
  }
}

Comments

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.