CSS-in-JS is a family of approaches for writing CSS inside JavaScript files. In React, JSX already keeps a component’s markup close to its logic; keeping styles nearby can make changes to all three easier to coordinate. It is an option for teams encountering friction with separate CSS, not a requirement for every project. section 7.1 section 7.7
The choice turns on which problems matter in your application:
- Separate CSS files and class names are familiar and need little extra training, but provide no guarantee that a component uses the classes its stylesheet defines. Misspellings and unused classes can go unchecked. section 7.3.5 section 7.7
- Global class names can collide across components or with a host website. Dynamic JavaScript values may require inline CSS, and rule ordering can complicate specificity. section 7.3.5
- CSS Modules preprocess regular CSS into unique generated class names. They reduce collision risk without abandoning CSS files, but do not address every weakness of separate files. section 7.7
- Styled-components places CSS beside the components that need it, giving a useful at-a-glance view for small components. That advantage diminishes as components grow. section 7.7
A component and its stylesheet
Consider a Button.js component importing button.css. The import does not verify that the component’s class names match the stylesheet. A misspelling can leave a style unused, while a generic class such as .group can unexpectedly affect another component. Keeping styles closer to the component addresses the coordination problem, but the appropriate styling approach still depends on project and team needs. section 7.3.5 section 7.1
Choose for the costs you actually face. Plain CSS remains reasonable for smaller projects; CSS Modules offer a modest change; styled-components can improve the overview of medium-to-large projects, though it is not a universal recommendation. section 7.7


