DEV Community

Cover image for Understanding CSS in JS and CSS Modules: Modern Solutions for Styling in Web Development
Sharique Siddiqui
Sharique Siddiqui

Posted on

Understanding CSS in JS and CSS Modules: Modern Solutions for Styling in Web Development

As frontend development evolves, so do the techniques we use to style web applications. Traditional CSS files often fall short in complex, component-based systems, causing struggles with style encapsulation, global namespace pollution, and scalability. Enter CSS in JS and CSS Modules — two popular approaches that provide better modularity, maintainability, and developer experience when styling modern web apps.

What is CSS in JS?

CSS in JS means writing CSS styles directly inside JavaScript files, typically alongside component code. Instead of separate CSS files, styles become part of the JavaScript bundle, allowing you to leverage the full power of JavaScript logic for dynamic styling.

How Does CSS in JS Work?

Since browsers don't understand CSS inside JS files natively, CSS-in-JS libraries must convert JavaScript style objects or tagged template literals into actual CSS stylesheets. There are two main runtime strategies for this:

  • Runtime stylesheets: Styles are generated and injected into the DOM dynamically during app execution.
  • Static CSS extraction: During build time, CSS is extracted from JS and emitted as separate static CSS files.

Popular libraries like styled-components, Emotion, and JSS use these approaches to avoid global CSS collisions by generating unique class names scoped to components. This scoping prevents style conflicts without developers needing to come up with globally unique class names.

Benefits of CSS in JS
  • Scoped and encapsulated styles: No more global CSS namespace conflicts.
  • Dynamic styling: Easily use JavaScript variables and logic for conditional styles.
  • Co-located styles: Styles live next to component code, improving maintainability.
  • Theming and style composition: Easy to implement dynamic themes and compose styles with JavaScript functions.
Considerations
  • Can add overhead if styles are generated at runtime.
  • Requires build tools or runtime libraries.
  • May increase JavaScript bundle size.

What are CSS Modules?

CSS Modules are a way to write traditional CSS or preprocessors like SASS but with the benefit of locally scoped class names by default. When you import a CSS Module in a JavaScript file, the class names inside the CSS file get mapped to unique, hashed identifiers that prevent conflicts.

How CSS Modules Work

For example, if you write in Button.module.css:

css
.button {
  background-color: blue;
  color: white;
}
Enter fullscreen mode Exit fullscreen mode

and import in your component as:

js
import styles from './Button.module.css';

function Button() {
  return <button className={styles.button}>Click me</button>;
}
Enter fullscreen mode Exit fullscreen mode

The styles.button will correspond to a unique class name like Button_button__3X7we generated during build time, ensuring the styles don't clash globally.

Benefits of CSS Modules
  • Local scoping by default: Prevents style leakage and collisions.
  • Familiar CSS syntax: No need to learn new syntax or JS objects.
  • Static extraction: CSS is output as static files, improving performance.
  • Easy integration: Works well with most build tools like Webpack.
Considerations
  • Limited dynamic capabilities compared to CSS in JS.
  • Requires build configuration for module support.

CSS in JS vs CSS Modules: When to Use Which?

Feature CSS in JS CSS Modules
Styling format JavaScript objects or templates Standard CSS or preprocessor
Scoped styles Yes, automatically scoped Yes, with hashed class names
Dynamic styling Rich support via JS logic Limited, needs extra setup
Build/runtime overhead Runtime injection or extraction Static extraction
IDE/editor support Improving with plugins Native CSS support
Popular with React Very common Common, especially in larger apps

Final Thoughts

Both CSS in JS and CSS Modules offer excellent solutions to long-standing frontend challenges of CSS scoping and maintainability. CSS in JS excels when you want dynamic, JavaScript-driven styles tightly coupled with components. CSS Modules shine for teams wanting CSS-syntax familiarity with automatic scoping and static build-time extraction.

Check out the YouTube Playlist for great CSS content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)