Styling Techniques: CSS Modules, Styled-Components, Tailwind CSS

Styling React components is one of the core aspects of building user interfaces. While traditional CSS styles can be applied globally, the modern approach focuses on component-scoped styles to improve maintainability and scalability. In this module, we will explore three popular styling techniques for React applications: CSS Modules, Styled-Components, and Tailwind CSS. Each of these approaches has its own strengths and can be chosen based on the needs of your project.


What Are CSS Modules?

CSS Modules are a way to scope CSS at the component level. Unlike traditional CSS where styles are applied globally, CSS Modules ensure that styles are scoped to the component that imports them. This prevents class name conflicts and makes styling more modular and maintainable.

How CSS Modules Work

  • When using CSS Modules, each class name is locally scoped to the component that imports the CSS file. The class names are transformed into unique identifiers during the build process.
  • This ensures that styles defined for one component don’t accidentally override or conflict with styles in another component.

Setting Up CSS Modules

  1. Create a .module.css file: Create a CSS file with the .module.css extension.
/* styles.module.css */
.button {
background-color: blue;
color: white;
padding: 10px 20px;
border-radius: 5px;
}
  1. Import and use the styles in a React component:
import React from 'react';
import styles from './styles.module.css';

const Button = () => {
return <button className={styles.button}>Click Me</button>;
};

export default Button;

Advantages of CSS Modules:

  • Local scoping: Styles are scoped to the component, reducing the risk of global conflicts.
  • No global namespace pollution: Since the styles are scoped, there are no issues with styles leaking into other parts of the application.
  • Familiar syntax: If you’re already familiar with traditional CSS, CSS Modules are easy to learn and use.

What Are Styled-Components?

Styled-components is a popular library for styling React components using tagged template literals in JavaScript. It allows you to define styles inside JavaScript files, which means your styles are scoped to the component and written in JavaScript.

How Styled-Components Work

  • Styled-components allows you to define styled elements directly within your JavaScript code.
  • You can use props and theme values within styled components to make styles dynamic.

Setting Up Styled-Components

  1. Install styled-components:
npm install styled-components
  1. Define styled components:
import React from 'react';
import styled from 'styled-components';

// Create a styled button
const Button = styled.button`
background-color: blue;
color: white;
padding: 10px 20px;
border-radius: 5px;

&:hover {
background-color: darkblue;
}
`;

const App = () => {
return <Button>Click Me</Button>;
};

export default App;

Advantages of Styled-Components:

  • CSS-in-JS: Styles are written directly in JavaScript, making it easy to dynamically apply styles based on props or state.
  • Component scoping: Just like CSS Modules, styles are scoped to the component, preventing conflicts with other styles.
  • Support for theming: Styled-components allows you to easily manage themes for your application by using the ThemeProvider component.

What Is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that provides low-level utility classes to style elements directly in your JSX. Instead of writing traditional CSS or creating separate styled components, you apply utility classes like text-center, bg-blue-500, p-4, and more directly to your JSX elements.

How Tailwind CSS Works

  • Tailwind CSS is based on utility classes that can be applied to HTML elements. It doesn’t require creating separate component files for styling; instead, you directly apply classes to the HTML elements in your JSX.
  • This approach speeds up development by reducing the need to manage separate CSS files or styled components.

Setting Up Tailwind CSS

  1. Install Tailwind CSS:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
  1. Configure Tailwind:

In your tailwind.config.js file:

module.exports = {
content: [
'./src/**/*.{html,js,jsx,ts,tsx}', // Add your React file extensions here
],
theme: {
extend: {},
},
plugins: [],
};
  1. Use Tailwind classes in JSX:
import React from 'react';
import './App.css'; // Import Tailwind CSS in your main CSS file

const App = () => {
return (
<div className="flex justify-center items-center min-h-screen bg-gray-100">
<button className="bg-blue-500 text-white p-4 rounded-lg hover:bg-blue-600">
Click Me
</button>
</div>
);
};

export default App;

Advantages of Tailwind CSS:

  • Utility-first: Tailwind encourages the use of utility classes to quickly style components without writing custom CSS.
  • Highly customizable: Tailwind’s configuration allows you to create a custom design system, adjusting the spacing, colors, typography, etc.
  • No naming conflicts: Since you’re using utility classes, there’s no risk of class name collisions.
  • Faster development: Tailwind speeds up styling by allowing you to write fewer lines of code and apply styles directly in the JSX.

Comparison of the Three Techniques

FeatureCSS ModulesStyled-ComponentsTailwind CSS
ApproachComponent-level CSS with local scopingCSS-in-JS using template literalsUtility-first, class-based styling
Learning CurveEasy to learn if familiar with CSSRequires learning CSS-in-JS syntaxRequires understanding utility classes
Styling ApproachTraditional CSS with scoped namesDynamic styling with JavaScriptApply utility classes directly in JSX
Theming SupportNo built-in theming supportBuilt-in theming support using ThemeProviderCustomizable via configuration
Dynamic StylesLimited (mainly through className)Full support (props-based)Limited (utility classes don’t accept props)
PerformanceGenerally good (no runtime overhead)Some runtime overhead due to CSS-in-JSFast, no runtime overhead
UsageBest for traditional React projectsBest for React projects with dynamic styling needsBest for rapid prototyping and utility-based design

Choosing the Right Styling Approach

Each of the three styling techniques—CSS Modules, Styled-Components, and Tailwind CSS—has its strengths. Here are some guidelines for choosing the right approach:

  • Use CSS Modules if you prefer traditional CSS but want to scope styles to components to avoid global conflicts.
  • Use Styled-Components if you prefer to have your styles written in JavaScript and want dynamic styling that can change based on props and state.
  • Use Tailwind CSS if you want to quickly prototype or prefer a utility-first approach, reducing the need to write custom CSS altogether.

Summary

In this module, we’ve covered three popular styling techniques in React:

  • CSS Modules: A way to scope your CSS locally to your components.
  • Styled-Components: A CSS-in-JS solution that allows you to define styled elements within JavaScript using template literals.
  • Tailwind CSS: A utility-first CSS framework that provides a set of utility classes to quickly style components without writing custom CSS.

Each of these techniques provides different ways to approach styling in React, so you can choose the one that best fits your project needs.