Performance: CSS variables with Tailwind
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 |
:root { --btn-color: #4ade80; } and this Tailwind class: class="bg-[var(--btn-color)] p-4 rounded", what color will the button background be?--text-color: #333;? class="text-[var(text-color)] font-bold"Pass the test and this pattern moves unknown