How to target CSS module classes after they are hashed?


Tags: css,reactjs,css-modules

Problem :

I'm working in react and have an element that uses CSS modules for styling, like so:

<div className={styles.book__title}>Title: {book.title}</div>

Because class names are hashed, the compiled output is something like:

<div class="book__title_adsfj4">Title: The Lord of the Rings</div>

I've added an event listener on a button that, when pressed, adds the 'title' class to this element to offer additional styling for that specific class, like so:

<div class="book__title_adsfj4 title">Title: The Lord of the Rings</div>

Is there a way where I can either:

  1. append a class to an element and within my css file not have that class be hashed or
  2. add a class to an element that exactly matches the final compiled hash name?

Thanks.



Solution :

I wrote about the best way I've found to do this here - https://github.com/css-modules/css-modules/issues/199

Basically, it's just importing those styles from the original css file. Because those styles are all hashed the same, no matter if they are imported within different files, the styling will carry over.


    CSS Howto..

    How Do I Add CSS Class to body Element in Meteor?

    How Can I Override Style Info from a CSS Class in the Body of a Page?

    How to position contents according to zoom level?

    Why do the images move when clicked in html and css? How to prevent it? [closed]

    How to change the indentation of all the lines except the first line in bulleted or non bulleted list?

    How to change the CSS of entire page/website on click?

    How to fix these jagged lines?

    How to keep a diagonal running background animation without rotating the div?

    How to keep floating labels floating after input. Pure CSS

    How to select nested UL element without class in JavaScript

    how to apply vertical-align: middle at div

    How to make a sliding menu similar to the Facebook Mobile side menu with html,css,jquery?

    How to change navbar background color after page has scrolled down

    How to insert HTML symbol after Bootstrap panel-heading in CSS?

    How to make the transitions in my css work

    How to Set an if in css for opera browser? [duplicate]

    How to hide a div on certain pages using CSS

    how to apply css to a specific element of a table?

    How Can I remove the injected CSS Resource in GWT?

    How to remove blue underline on text in Chrome - CSS?

    Css Sub menu needed to show on the left not on the right of the menu

    How can I have a transparent overlay and center with flex box?

    How to bump up text?

    How to make jquery autocomplete to not cover other content below it?

    how to call css and js files in webview?

    How to move text list items to align with an image list item

    How to set up fields position?

    How can I style a button to look the same as the home button in Google Chrome with CSS? [closed]

    How to insulate HTML widget from external CSS

    How to use jQueryUI tootip along with my custom css