Performance: CSS variables with Tailwind
MEDIUM IMPACT
Using CSS variables with Tailwind affects style recalculation and paint performance during theme changes or dynamic styling.
/* Define CSS variables in :root or a wrapper */ <style> :root { --primary-color: #3b82f6; /* blue-500 */ --text-color: #ffffff; } </style> <div class="bg-[var(--primary-color)] text-[var(--text-color)]">Hello</div> /* Change theme by updating CSS variables only */
/* Tailwind classes with hardcoded colors */ <div class="bg-blue-500 text-white">Hello</div> /* Changing theme requires changing many classes or re-rendering */
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hardcoded Tailwind classes for colors | None | Multiple on theme change | High due to repaint and layout shifts | [X] Bad |
| CSS variables with Tailwind utility classes | None | Single style recalculation | Low paint cost, no layout shift | [OK] Good |