Performance: Tailwind with Next.js
This affects page load speed and rendering performance by how CSS is generated, loaded, and applied in Next.js projects using Tailwind.
module.exports = {
mode: 'jit',
purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
// other config
};@import 'tailwindcss/base'; @import 'tailwindcss/components'; @import 'tailwindcss/utilities'; // No purge or JIT enabled in tailwind.config.js
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Tailwind without JIT and purge | Normal DOM | 1 reflow per style change | High paint cost due to large CSS | [X] Bad |
| Tailwind with JIT and purge in Next.js | Normal DOM | 1 reflow per style change | Low paint cost due to small CSS | [OK] Good |
div element?className with double quotes for CSS classes.<div className="bg-blue-500 text-white p-4">Content</div> uses correct JSX syntax with double quotes. <div class="bg-blue-500 text-white p-4">Content</div> uses plain HTML class, which is invalid in JSX. <div className={bg-blue-500 text-white p-4}>Content</div> treats the classes as a JavaScript expression without proper quotes. <div className=bg-blue-500 text-white p-4>Content</div> lacks quotes around the class names.export default function Button() {
return (
<button className="bg-green-500 sm:bg-red-500 text-white p-3">Click Me</button>
)
}bg-green-500 applies by default. The sm:bg-red-500 applies only on small screens and above (min-width 640px).bg-green-500 applies. On small screens and larger, bg-red-500 overrides the background.export default function Header() {
return (
<header className="flex justify-center items-center p-4">
<h1 className="text-2xl font-bold">Welcome</h1>
</header>
)
}items-center works if the container has height. Without height, vertical centering has no effect.min-h-screen or a fixed height allows vertical centering.min-h-screen or height on header to enable vertical centering -> Option Bflex-col stacks items vertically; flex-row arranges items horizontally.flex flex-col md:flex-row means vertical stacking by default (mobile) and horizontal layout on medium screens and above (desktop).<nav className="grid grid-cols-4 md:grid-cols-1 gap-4 p-4">...</nav> uses grid-cols-4 by default, spreading items horizontally on mobile instead of stacking vertically. <nav className="block md:flex-row gap-4 p-4">...</nav> misses flex class, so md:flex-row won't apply properly. <nav className="flex-row flex md:flex-col gap-4 p-4">...</nav> reverses directions incorrectly.Pass the test and this pattern moves unknown