Styling React ComponentsEntry 6 of 7
Tailwind CSS
A utility-first CSS framework: you style elements by combining small, single-purpose classes directly in markup.
Notes
- Classes like
px-4,text-sm, andbg-stone-300each do one thing. Variants likehover:,md:, anddark:handle states and breakpoints. - Tailwind scans your source files and generates CSS only for classes it finds, so the output stays small.
- Never build class names dynamically (
`text-${color}-400`). Tailwind can't see them and won't generate the CSS. Write complete class names in both branches of a condition. - Tailwind v4 needs no config file for basic use; the CSS import is enough.
What / Why / When
- What
Utility classes in markup instead of custom CSS.
- Why
No naming, no switching files, consistent spacing and colors, tiny production CSS.
- When
Most new projects in either framework.
- Use case
Styling the email field with a conditional error color.
Code compare
Setup (Vite):
npm install tailwindcss @tailwindcss/viteSetup (PostCSS):
npm install tailwindcss @tailwindcss/postcss postcssDifferences and gotchas
- Tailwind classes are identical in both frameworks; only setup and binding syntax differ.
- Angular's
[class]binding merges with the staticclassattribute, which keeps base and conditional classes cleanly separated. - Long class strings: extract a component rather than using
@applyeverywhere.
Quick reference
| React | Angular |
|---|---|
@tailwindcss/vite plugin | @tailwindcss/postcss + .postcssrc.json |
@import 'tailwindcss' in index.css | @import 'tailwindcss' in styles.css |
className={`base ${cond ? 'a' : 'b'}`} | class="base" [class]="cond ? 'a' : 'b'" |