Styling Strategy
Style approaches
React does not prescribe a single styling system, so teams choose an approach based on bundle size, runtime cost, and scoping guarantees:
- Global stylesheets + CSS variables: Best for typography, base resets, and shared design tokens available everywhere.
- CSS Modules (
*.module.css): Scopes class names locally at build time so two components can both define.cardwithout colliding. - Utility-first CSS: Applies composable spacing, color, and layout classes directly in JSX, reducing dead CSS while requiring discipline around shared component abstractions.
With CSS Modules, importing the stylesheet yields a map of unique generated class names that you apply via className:
import styles from "./AlertBanner.module.css";
function AlertBanner({ tone = "info", children }) {
// CSS Modules hashes `banner` and `info` so styles never leak globally
return (
<div className={`${styles.banner} ${styles[tone]}`}>
{children}
</div>
);
}
Naming conventions
Whether using plain CSS or CSS Modules, name classes by semantic role (.summaryHeader, .statusBadge) rather than visual appearance (.blueBox, .mt20). Keep specificity flat—prefer single class selectors over deeply nested descendants so overrides never require !important.
Responsive rules
Write mobile-first base rules and layer wider breakpoints with min-width media queries or CSS container queries. Avoid fixed pixel widths on cards and tables; use flexible grids (minmax(), flex-wrap) so components adapt cleanly across viewports.
Accessibility and styling
Visual polish must never compromise usability:
- Preserve visible keyboard focus rings using
:focus-visibleinstead of removingoutline. - Maintain WCAG AA contrast ratios between text and background tokens.
- Respect
@media (prefers-reduced-motion: reduce)by disabling non-essential transitions and animations.
Practice
Refactor a card component to use CSS Modules backed by CSS custom property tokens, responsive grid wrapping, and :focus-visible keyboard outlines.