Here's a quick intro to how you can make use of CSS Modules today, using Webpack's awesome css-loader.

First off, let's make sure we're on the same page. We want to colocate our CSS styles with our components, because they are already coupled anyway. I'll be using less today, but this applies to raw CSS, LESS, SCSS, etc.

Folder Structure


The Styles

First off, let's write some LESS:

.foo {
    padding: 10px; {
        color: black;
        font-size: 200%;

... pretty standard LESS. The classNames are super generic, which is normally a horrible idea, but CSS modules are going to namespace everything so that collisions are extremely unlikely.

Importing CSS Modules

In order to use modular CSS, we need to "import" the styles. This process transforms the CSS rules, namespacing all classes. As with normal imports, css-loader then injects your stylesheet into the document. The big difference here is that there is an actual value returned from our import, which is an object mapping of local CSS class names to their namespaced versions.

import style from './style.less';

export default class Foo extends Component {
    render() {
        return (
            <div class={ }>
                <header class={ }>Sup</header>

If you were to console.log(style), you would see that in addition to "injecting" your CSS into the document, the import actually returns a mapping of class names to local class names:

{ foo:"foo_foo_abcde", bar:"foo_bar_abcde" }

How this works: When you import the less/css/scss file, it gets added as a stylesheet (somewhere, handled internally by css-loader). Your classnames will be transformed to "local" (namespaced) classnames. You can control how this works, but the most common way is to transform .class into filename_class_[hash:0:5]. This keeps things namespaced (by file), versioned (via the truncated hash), and readable (the original class name is still there).

All of this means that when you set class={} in your JSX (or any other form of markup), you're setting it to the local version of that named class, class="foo_foo_abcde".

In order for all of this to work, you need to tell css-loader that you want to use CSS Modules:

module: {
  loaders: [
      test: /\.(less|css)$/,
      loader: [
        'less'   // if you want .less support

If you want to customize the imported "local" names, you can supply a simple template string for the localIdentName option:


Think this seems worth it?