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

5

6

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...

Dictatorship answered 11/5, 2017 at 14:0 Comment(0)
B
14

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

Barred answered 15/5, 2017 at 12:49 Comment(1)
As an aside, you can also concatenate different classes using <div className={“row “ + styles.myCustomClassName}.Kortneykoruna
E
10

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

<div className={`row ${styles.myCustomClassName}`} />
Encase answered 14/7, 2020 at 1:40 Comment(0)
L
1

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

Law answered 16/8, 2017 at 11:1 Comment(0)
S
0

You can use backticks like:

<div className={`row ${style.customCssClassName}`}></div>
Synesthesia answered 29/1 at 21:16 Comment(0)
A
0
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;

Antiphon answered 16/7 at 5:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.