CSS Modules and Tailwind
Use Tailwind utilities inside CSS Modules to combine scoped component styles with the utility-first approach in React and Next.js apps.
What Are CSS Modules
CSS Modules are CSS files where class names are locally scoped by default. When you import a CSS Module into a JavaScript component, the class names are transformed into unique identifiers like Button_root__Xk2p1, preventing style leakage between components. CSS Modules are built into Next.js and available in most React setups via webpack or Vite.
/* Button.module.css */
.root {
display: flex;
align-items: center;
}
// Button.tsx
import styles from './Button.module.css';
const Button = () => (
// styles.root resolves to 'Button_root__Xk2p1' at runtime
<button className={styles.root}>Click me</button>
);Why Combine CSS Modules With Tailwind
Tailwind and CSS Modules seem philosophically opposed — Tailwind avoids naming things while CSS Modules is all about naming things locally. But they complement each other in practice. CSS Modules handle complex selectors, :focus-within chains, and pseudo-element styling that Tailwind cannot express easily. Tailwind handles the bulk of visual styling. Together they cover 100% of styling needs.
All lessons in this course
- File and Folder Organization
- Coexisting With Legacy CSS
- CSS Modules and Tailwind
- Scaling Tailwind Across Monorepos