Sovan Jana
Sovan Jana

Reputation: 93

How to use css-modules and bootstrap at same time?

I'm using electron-react-boilerplate for my project and this boilerplate is using css-modules for styling purpose. I'm having trouble with using bootstrap and custom style at the same place. Suppose i've a code snippet like,

<div className="container">
  <div className="row custom-css">
    // other codes...
</div>

in that case 'row' is one bootstrap className and 'custom-css' is my own style className. please help me to find some solution for these problem so that i can use css-modules and bootstrap together...

Upvotes: 6

Views: 10096

Answers (5)

MD AKRAM
MD AKRAM

Reputation: 1

import styles from "[path of css module]"

<div className="container">
  <div className={`row ${styles.custom-css}}`>
    // other codes...
</div>

Sometimes there may a situation occurs : Bootstrap overlapping your custom css (for eg. background color) => use !important;

Upvotes: 0

Vytautas
Vytautas

Reputation: 1

You can use backticks like:

<div className={`row ${style.customCssClassName}`}></div>

Upvotes: 0

alechill
alechill

Reputation: 4512

You need to import your CSS module styles from a module file specific to this component, then interpolate them into the classname via the returned object...

MyComponent.css

.myCustomClassName {
  color: #fff;
}

MyComponent.js

import styles from './MyComponent.css';

<div className={`row ${styles.myCustomClassName}`} />

When output as HTML this would become something like...

<div class="row MyComponent__myCustomClassName___n1cC4ge}` />

So as long as you are loading the bootstrap CSS somewhere that should pick up on both

Upvotes: 14

Kan Dhillon
Kan Dhillon

Reputation: 109

thanks guys i find it working by adding {} around that

<div className={`row ${styles.myCustomClassName}`} />

Upvotes: 10

Krikor Boghossian
Krikor Boghossian

Reputation: 11

I was kinda stuck with this (as to how to load Bootstrap). I created this rough edit in my webpack config file.

       {
        test: /(\.bootstrap\.css$|bootstrap-theme.css|bootstrap.css)/,
        use: [
            {
                loader: 'style-loader',
            },
            {
                loader: 'css-loader',
                options: {
                    minimize: true || {/* CSSNano Options */}
                }
            },
        ],
    },
    {
        test: /^((?!\.bootstrap|bootstrap-theme).)*\.css$/,
        use: [
            {
                loader: 'style-loader',
            },
            {
                loader: 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
            },
            {
                loader: require.resolve('postcss-loader'),
                options: {
                    // Necessary for external CSS imports to work
                    // https://github.com/facebookincubator/create-react-app/issues/2677
                    ident: 'postcss',
                    plugins: () => [
                        require('postcss-flexbugs-fixes'),
                        autoprefixer({
                            browsers: [
                                '>1%',
                                'last 4 versions',
                                'Firefox ESR',
                                'not ie < 9', // React doesn't support IE8 anyway
                            ],
                            flexbox: 'no-2009',
                        }),
                    ],
                },
            }
        ]
    },

The rest are covered perfectly by alechill

Upvotes: 1

Related Questions