What's your approach to styling in React applications? (CSS Modules, Styled Components, Tailwind, etc.)
5 minintermediatereactbest-practicesapproachstylingapplications
Quick Answer
Key aspects: Tailwind CSS (Preferred Approach); CSS Modules; Styled Components; Emotion (CSS-in-JS Alternative); Design System Approach; Best Practices & Recommendations.
Detailed Answer
What's your approach to styling in React applications? (CSS Modules, Styled Components, Tailwind, etc.)
Answer:
1. Tailwind CSS (Preferred Approach):
// Component with Tailwind classes
const Button: React.FC<ButtonProps> = ({
variant = 'primary',
size = 'md',
children,
...props
}) => {
const baseClasses = 'font-medium rounded-lg transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2';
const variantClasses = {
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500',
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500',
danger: 'bg-red-600 text-white hover:bg-red-700 focus:ring-red-500',
};
const sizeClasses = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-6 py-3 text-lg',
};
return (
<button
className={`${baseClasses} ${variantClasses[variant]} ${sizeClasses[size]}`}
{...props}
>
{children}
</button>
);
};
// Responsive design with Tailwind
const Dashboard: React.FC = () => {
return (
<div className="min-h-screen bg-gray-50">
<header className="bg-white shadow-sm border-b border-gray-200">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div className="flex justify-between items-center h-16">
<h1 className="text-xl font-semibold text-gray-900">Dashboard</h1>
<nav className="hidden md:flex space-x-8">
<a href="#" className="text-gray-500 hover:text-gray-900">Home</a>
<a href="#" className="text-gray-500 hover:text-gray-900">Analytics</a>
</nav>
</div>
</div>
</header>
<main className="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div className="bg-white overflow-hidden shadow rounded-lg">
<div className="p-5">
<div className="flex items-center">
<div className="flex-shrink-0">
<div className="w-8 h-8 bg-blue-500 rounded-md flex items-center justify-center">
<span className="text-white text-sm font-medium">📊</span>
</div>
</div>
<div className="ml-5 w-0 flex-1">
<dl>
<dt className="text-sm font-medium text-gray-500 truncate">
Total Revenue
</dt>
<dd className="text-lg font-medium text-gray-900">
$45,231.89
</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
);
};
2. CSS Modules:
// Button.module.css
.button {
@apply font-medium rounded-lg transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2;
}
.primary {
@apply bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500;
}
.secondary {
@apply bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500;
}
.small {
@apply px-3 py-1.5 text-sm;
}
.medium {
@apply px-4 py-2 text-base;
}
.large {
@apply px-6 py-3 text-lg;
}
// Button.tsx
import styles from './Button.module.css';
import { clsx } from 'clsx';
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'primary' | 'secondary';
size?: 'small' | 'medium' | 'large';
}
const Button: React.FC<ButtonProps> = ({
variant = 'primary',
size = 'medium',
className,
children,
...props
}) => {
return (
<button
className={clsx(
styles.button,
styles[variant],
styles[size],
className
)}
{...props}
>
{children}
</button>
);
};
3. Styled Components:
import styled, { css } from 'styled-components';
interface ButtonProps {
variant?: 'primary' | 'secondary' | 'danger';
size?: 'sm' | 'md' | 'lg';
fullWidth?: boolean;
}
const StyledButton = styled.button<ButtonProps>`
font-weight: 500;
border-radius: 0.5rem;
transition: all 0.2s ease-in-out;
border: none;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
&:focus {
outline: none;
box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.5);
}
${({ variant = 'primary' }) => {
switch (variant) {
case 'primary':
return css`
background-color: #2563eb;
color: white;
&:hover {
background-color: #1d4ed8;
}
`;
case 'secondary':
return css`
background-color: #e5e7eb;
color: #111827;
&:hover {
background-color: #d1d5db;
}
`;
case 'danger':
return css`
background-color: #dc2626;
color: white;
&:hover {
background-color: #b91c1c;
}
`;
}
}}
${({ size = 'md' }) => {
switch (size) {
case 'sm':
return css`
padding: 0.375rem 0.75rem;
font-size: 0.875rem;
`;
case 'md':
return css`
padding: 0.5rem 1rem;
font-size: 1rem;
`;
case 'lg':
return css`
padding: 0.75rem 1.5rem;
font-size: 1.125rem;
`;
}
}}
${({ fullWidth }) =>
fullWidth &&
css`
width: 100%;
`}
`;
// Theme provider setup
const theme = {
colors: {
primary: '#2563eb',
secondary: '#6b7280',
success: '#10b981',
danger: '#dc2626',
},
spacing: {
xs: '0.25rem',
sm: '0.5rem',
md: '1rem',
lg: '1.5rem',
xl: '2rem',
},
breakpoints: {
mobile: '768px',
tablet: '1024px',
desktop: '1280px',
},
};
const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return <StyledThemeProvider theme={theme}>{children}</StyledThemeProvider>;
};
4. Emotion (CSS-in-JS Alternative):
import { css } from '@emotion/react';
import styled from '@emotion/styled';
// Using css prop
const Card: React.FC = () => {
return (
<div
css={css`
background: white;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
padding: 1.5rem;
margin-bottom: 1rem;
&:hover {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transform: translateY(-2px);
transition: all 0.2s ease-in-out;
}
`}
>
<h3
css={css`
margin: 0 0 1rem 0;
color: #1f2937;
font-size: 1.25rem;
font-weight: 600;
`}
>
Card Title
</h3>
<p
css={css`
margin: 0;
color: #6b7280;
line-height: 1.5;
`}
>
Card content goes here...
</p>
</div>
);
};
// Using styled components with Emotion
const StyledInput = styled.input`
width: 100%;
padding: 0.75rem;
border: 1px solid #d1d5db;
border-radius: 0.375rem;
font-size: 1rem;
&:focus {
outline: none;
border-color: #2563eb;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
&::placeholder {
color: #9ca3af;
}
`;
5. Design System Approach:
// Design tokens
export const tokens = {
colors: {
primary: {
50: '#eff6ff',
500: '#3b82f6',
900: '#1e3a8a',
},
gray: {
50: '#f9fafb',
500: '#6b7280',
900: '#111827',
},
},
spacing: {
xs: '0.25rem',
sm: '0.5rem',
md: '1rem',
lg: '1.5rem',
xl: '2rem',
},
typography: {
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif'],
mono: ['JetBrains Mono', 'monospace'],
},
fontSize: {
sm: '0.875rem',
base: '1rem',
lg: '1.125rem',
xl: '1.25rem',
},
},
breakpoints: {
sm: '640px',
md: '768px',
lg: '1024px',
xl: '1280px',
},
};
// Utility functions
export const createResponsiveValue = <T>(values: Partial<Record<keyof typeof tokens.breakpoints, T>>) => {
return Object.entries(values)
.map(([breakpoint, value]) => {
const minWidth = tokens.breakpoints[breakpoint as keyof typeof tokens.breakpoints];
return `@media (min-width: ${minWidth}) { ${value} }`;
})
.join(' ');
};
6. Best Practices & Recommendations:
When to use each approach:
- Tailwind CSS: Best for rapid prototyping, consistent design systems, and utility-first development
- CSS Modules: Good for component-scoped styles with traditional CSS syntax
- Styled Components: Ideal for dynamic styling based on props and complex component logic
- Emotion: Great alternative to Styled Components with better performance
- CSS-in-JS: Best for applications requiring runtime theming and dynamic styles
General Guidelines:
- Consistency: Choose one primary approach and stick to it
- Performance: Consider bundle size and runtime performance
- Developer Experience: Ensure good TypeScript support and tooling
- Maintainability: Use design tokens and consistent naming conventions
- Accessibility: Ensure styles support keyboard navigation and screen readers
- Responsive Design: Always consider mobile-first approach
- Theme Support: Plan for dark mode and theme switching if needed