0
Challenge · 76Architecture & State

CSS Modules

Toolused in ~50% of apps

Two teammates both write a .button class in two different files — in plain global CSS the last one loaded silently overwrites the other. CSS Modules end that whole class of bug by scoping every class name to the file it lives in.

Core idea. A file named *.module.css gives every class a locally-scoped, build-time-hashed name, so styles can never leak between components or collide — you write readable names, the bundler guarantees uniqueness.
Playground, this isn't a screenshot
Global CSSCSS Modules
Card.css
.button {
color: white;
background: blue;
}
COMPILED CLASS
.button
Dialog.css
.button {
color: white;
background: orange;
}
COMPILED CLASS
.button
WHAT RENDERS ON THE PAGE
class="button"
class="button"

Collision. Both files declare .buttonin the global bucket, so the last one loaded wins — Card's blue is overwritten by Dialog's orange.

↑ play a little first, then dig in