CSS Modules, my dream CSS-in-JS solution

webinar: Amazee Webinar #3, Zürich, online

I ❤️'d Sass — John Albin, 2017

So you are thinking about styling a decoupled Drupal website? As CSS developers, it can be daunting to look at the JavaScript community; there seems to be a new best practice every week. And determining how best to style a decoupled application appears to be a nightmare of infinite choices. What to use? Styled Components? Glamorous? Inline CSS?

During the past year of building decoupled websites, my team has tried several different CSS-in-JS solutions and made lots of mistakes. But we have finally decided on using CSS Modules.

CSS Modules is a mature project with a syntax that is a superset of CSS, similar to Sass. It makes it easy for you to “think in components” without having to worry about BEM class naming. It automatically generates locally-scoped CSS class names, so you can use “.wrapper” in multiple files without conflict. It also allows integration of “global” class names from other code (like JS libraries or 3rd party CSS). With CSS Modules you get automatic dead-code elimination as only the CSS used on the page is ever sent to the browsers. Best of all CSS Modules can be used with any JavaScript framework, including React, Angular and Vue.js.

In this webinar, we will briefly compare the most popular CSS-in-JS projects, discuss why CSS Modules is an excellent choice, and show how to use CSS Modules in your decoupled project.

Some of the CSS Modules features we will cover include:

  • Nested rulesets.
  • Handling local and global class names.
  • Composition of local, global or external rulesets.
  • Sharing variables between your JavaScript and your CSS.

Video for this webinar was formerly available at https://www.youtube.com/watch?v=4J-RlNKZ8Qs.