JavaScript and React course Β· Module 14: Advanced Styling
Comparing Styling Approaches - Galaxy Map
In this lesson7
We now have three powerful tools for styling in our arsenal. It's time to compare them and understand when to use each one. It's like choosing a spaceship -- each has its strengths and best use cases.
Comparison table
| Feature | CSS Modules | Styled-components | Tailwind CSS |
|---|---|---|---|
| Isolation | Automatic (unique classes) | Automatic (unique classes) | None (global classes) |
| Dynamic styles | Limited (CSS variables) | Full (props + JS) | clsx/cn + conditions |
| Bundle size | Small | Larger (runtime JS) | Small (purge) |
| Learning curve | Low | Medium | Medium |
| Theming | CSS variables | ThemeProvider | tailwind.config |
| Pseudo-classes | Yes (plain CSS) | Yes (& syntax) | Yes (prefixes) |
| Responsiveness | Media queries | Media queries | Prefixes (md:, lg:) |
| SSR | Seamless | Requires configuration | Seamless |
When to use CSS Modules?
CSS Modules are ideal when:
- You want a simple solution without additional libraries
- The team knows plain CSS well
- The project uses Create React App or Next.js (built-in support)
- Performance matters (zero runtime JS)
- You're migrating from an existing CSS project
// CSS Modules -- simple, familiar CSS
import styles from './Card.module.css';
function Card({ title }) {
return <div className={styles.card}><h2>{title}</h2></div>;
}When to use Styled-components?
Styled-components are ideal when:
- You need very dynamic styles (dependent on props, state)
- You're building a component library with built-in styles
- You want advanced theming with theme switching
- The project is component-based (styles = part of the component)
// Styled-components -- styles as part of the component
const Card = styled.div`
background: ${({ variant }) => variant === 'dark' ? '#0a0a2e' : '#ffffff'};
padding: ${({ theme }) => theme.spacing.lg};
`;When to use Tailwind CSS?
Tailwind CSS is ideal when:
- You want to quickly prototype interfaces
- You value design consistency (design system in config)
- The team likes the utility-first approach
- You care about a small bundle (purging unused classes)
- You use Next.js or Vite (great integration)
// Tailwind -- rapid prototyping
function Card({ title }) {
return (
<div className="bg-slate-900 p-6 rounded-xl border border-indigo-700">
<h2 className="text-xl font-bold text-purple-400">{title}</h2>
</div>
);
}Mixing approaches
In practice, many projects combine different approaches:
- Tailwind for layout and rapid prototyping
- CSS Modules for complex animations and specific components
- Styled-components for components with dynamic styles
This is perfectly acceptable! The key is to establish conventions within the team and stick to them.
Popularity and trends
According to React developer surveys:
- Tailwind CSS -- growing popularity, dominant in new projects
- CSS Modules -- stable popularity, often used in Next.js
- Styled-components -- still popular, but slightly declining
- Zero-runtime CSS-in-JS (vanilla-extract, Panda CSS) -- new trend, growing interest
Practical tips for choosing
Before choosing a styling approach, ask yourself a few questions:
- What is the team size? -- a large team may prefer Tailwind (consistent values) or CSS Modules (familiar CSS)
- Do you need dynamic styles? -- if so, styled-components or Emotion will be best
- How important is performance? -- for time-critical applications, consider CSS Modules or zero-runtime CSS-in-JS
- Are you using SSR? -- CSS Modules and Tailwind work seamlessly, styled-components require additional configuration
- Do you have existing CSS? -- if so, CSS Modules offer the easiest migration
There is no single "best" solution -- each approach is a different spaceship, designed for a different type of mission. The key is understanding the trade-offs and choosing what best fits your project and team.
Code for this lesson: App.jsx
import React, { useState } from 'react';
import styled from 'styled-components';
const Container = styled.div`
font-family: 'Segoe UI', sans-serif;
padding: 20px;
background: #0a0a2e;
color: #e0e0e0;
min-height: 100vh;
`;
const Heading = styled.h1`
color: #64b5f6;
text-align: center;
margin-bottom: 24px;
`;
const ComparisonGrid = styled.div`
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 16px;
margin-bottom: 24px;
`;
const ApproachCard = styled.div`
background: ${props => props.active ? '#1a1a5e' : '#1a1a4e'};
border: 2px solid ${props => props.active ? '#7c4dff' : '#3949ab'};
border-radius: 12px;
padding: 20px;
cursor: pointer;
transition: all 0.3s ease;
&:hover { border-color: #7c4dff; transform: translateY(-2px); }
`;
const ApproachName = styled.h3`
color: #7c4dff;
margin-bottom: 8px;
font-size: 18px;
`;
const ProsList = styled.ul`
list-style: none;
padding: 0;
margin: 8px 0;
li {
padding: 4px 0;
font-size: 14px;
&::before { content: 'β '; color: #00e676; }
}
`;
const ConsList = styled.ul`
list-style: none;
padding: 0;
margin: 8px 0;
li {
padding: 4px 0;
font-size: 14px;
&::before { content: 'Γ '; color: #f44336; }
}
`;
const Label = styled.span`
font-size: 12px;
font-weight: bold;
color: ${props => props.type === 'pro' ? '#00e676' : '#f44336'};
text-transform: uppercase;
`;
const approaches = [
{
name: 'CSS Modules',
desc: 'Isolated styles with automatically unique classes',
pros: ['Zero runtime JS', 'Plain CSS', 'Built into CRA/Next.js', 'Easy migration'],
cons: ['Limited dynamic styles', 'Harder to combine classes', 'No native theming'],
bestFor: 'Simple projects, migrating from CSS, Next.js apps',
},
{
name: 'Styled-components',
desc: 'CSS-in-JS with tagged template literals and props',
pros: ['Dynamic styles (props)', 'ThemeProvider', 'Full pseudo-classes', 'Component extension'],
cons: ['Runtime JS', 'Larger bundle', 'SSR requires configuration', 'Learning curve'],
bestFor: 'Component libraries, dynamic UI, theming',
},
{
name: 'Tailwind CSS',
desc: 'Utility-first framework with classes directly in JSX',
pros: ['Fast prototyping', 'Small bundle (purge)', 'Consistent design system', 'Responsiveness (prefixes)'],
cons: ['Longer classNames', 'Class learning curve', 'Less readable JSX', 'Configuration required'],
bestFor: 'Fast development, prototypes, projects with a design system',
},
];
export default function App() {
const [selected, setSelected] = useState(0);
const approach = approaches[selected];
return (
<Container>
<Heading>Comparing styling approaches</Heading>
<ComparisonGrid>
{approaches.map((a, i) => (
<ApproachCard
key={a.name}
active={i === selected}
onClick={() => setSelected(i)}
>
<ApproachName>{a.name}</ApproachName>
<p style={{ fontSize: '14px', color: '#b0bec5' }}>{a.desc}</p>
</ApproachCard>
))}
</ComparisonGrid>
<div style={{ background: '#1a1a4e', borderRadius: '12px', padding: '24px', border: '1px solid #3949ab' }}>
<h2 style={{ color: '#7c4dff', marginBottom: '16px' }}>{approach.name}</h2>
<Label type="pro">Pros</Label>
<ProsList>{approach.pros.map(p => <li key={p}>{p}</li>)}</ProsList>
<Label type="con">Cons</Label>
<ConsList>{approach.cons.map(c => <li key={c}>{c}</li>)}</ConsList>
<div style={{ marginTop: '16px', padding: '12px', background: '#0d1137', borderRadius: '8px' }}>
<span style={{ color: '#64b5f6', fontWeight: 'bold' }}>Best for: </span>
<span style={{ color: '#e0e0e0' }}>{approach.bestFor}</span>
</div>
</div>
</Container>
);
}Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which approach does NOT require additional runtime JavaScript in the browser?
2. Which approach is best for rapid interface prototyping?
Hands-on tasks in the game
- Click in order
Arrange the composes syntax in CSS Modules:
- Horizontal ordering
Arrange the syntax for combining CSS Modules classes with a condition using a template literal:
- Code editor
Implement a switcher for three space-themed themes