CSS Modules keep CSS classes and animations local to a module. In a React component, a .module.css file provides styles consumed as a JavaScript object. Class names are made unique, so a class in one component does not collide with a similarly named class elsewhere. section 2.2 section 5.2
Interview question: which styling approach fits?
Suppose several React components need a class named title. A standard imported CSS file can keep styles beside each component, but CSS Modules add local scoping to avoid naming conflicts. If the styles must change extensively with props or theme context, explain the trade-off: CSS Modules have limited dynamic styling and may need external logic or inline styles. section 2.2 section 5.2
Table 5.4 Comparing Emotion, CSS Modules, and Tailwind CSS
|
Aspect
|
Emotion
|
CSS Modules
|
Tailwind CSS
|
|
Definition
|
A CSS-in-JS library that allows writing scoped styles directly within JavaScript and TypeScript files using tagged template literals.
|
A framework for writing locally scoped CSS classes and animations, consumed as JavaScript objects.
|
A utility-first CSS framework offering pre-defined classes for rapid UI development.
|
|
Scope management
|
Fully scoped styles tied to specific React components, preventing global style conflicts.
|
Styles are locally scoped by default, avoiding global collisions through unique hashed class names.
|
Classes are global but modular by design, allowing reuse across components without collisions.
|
|
Dynamic styling
|
Supports dynamic styling via props and theme context; ideal for interactive or themeable apps.
|
Limited dynamic styling. Requires external logic or inline styles for conditional rendering.
|
Dynamic styling achieved by combining utility classes. No direct support for props-based styling.
|
|
Ease of use
|
Integrates seamlessly with React; supports styled components and css prop.
|
Familiar syntax for developers used to plain CSS. Requires importing styles into components.
|
Requires memorizing utility class names. Can lead to verbose HTML markup.
|
|
Performance
|
Highly optimized with zero-runtime extraction (when configured); SSR support.
|
Generates unique class names during build time, minimizing unused CSS and improving performance.
|
Lightweight and fast due to pre-defined utility classes and tree-shaking capabilities.
|
|
Use Cases
|
Ideal for React applications requiring scoped, dynamic styling tied to components.
|
Perfect for projects needing modular, maintainable CSS without global conflicts.
|
Best suited for rapid prototyping or projects requiring consistent design systems across components.
|
Compare CSS Modules with Emotion and Tailwind CSS by scope management and support for dynamic styling. section 5.2
A strong choice is contextual: use local scoping when preventing collisions and keeping component styles maintainable matter; weigh alternatives against the team’s size, project complexity, and need for design flexibility. CSS Modules solve a naming problem, not every styling requirement. section 2.2 section 5.2

