Css-loader: [css-modules] Is it possible to preserve the local class name?

Created on 6 Oct 2016  路  3Comments  路  Source: webpack-contrib/css-loader

This is best demonstrated with an example.

1) Generated CSS files are only the hash or unique identifier (prevent style bleed)
2) Class name is preserved in the html structure to inherit from global styles

component.css (source)

.element {
  background-color: blue;
}

component.jsx (source)

import styles from './component.css';
...
return (<div className={styles.element}>

component.css (output)

.1vZzB {
  background-color: blue;
}

component.jsx (output)

<div class="node 1vZzB">  

Note the space. This allows global styles to target .node but the components styles to never bleed outside their intended scope.

Most helpful comment

u can use :global .node { color: black; } or concat things in className manually

All 3 comments

u can use :global .node { color: black; } or concat things in className manually

see @longlho comment

One neat thing is you can use local scope inside of global scope.

For example:

.node {
  // some styles
}

:global .mobile {
  :local .node {
    // some other styles
  }
}

in which case you can affect the .node compiled className with the non-compiled .mobile classname

Was this page helpful?
0 / 5 - 0 ratings