Tailwind helps you style websites quickly using ready-made classes. Next.js lets you build fast React websites easily. Using Tailwind with Next.js means you can make beautiful, fast websites with less work.
Tailwind with Next.js
1. Install Tailwind CSS in your Next.js project: npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p 2. Configure tailwind.config.js to include your files: module.exports = { content: ["./app/**/*.{js,ts,jsx,tsx}", "./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"], theme: { extend: {}, }, plugins: [], } 3. Add Tailwind directives to your global CSS (e.g., styles/globals.css): @tailwind base; @tailwind components; @tailwind utilities;
The content array tells Tailwind where to look for class names to include in the final CSS.
Use Tailwind classes directly in your JSX inside Next.js components for styling.
<div className="bg-blue-500 text-white p-4 rounded">Hello Tailwind!</div><button className="hover:bg-green-600 bg-green-500 text-white font-bold py-2 px-4 rounded">Click me</button><main className="max-w-4xl mx-auto p-6"> <h1 className="text-3xl font-bold mb-4">Welcome to Next.js with Tailwind</h1> <p className="text-gray-700">Build fast and beautiful websites.</p> </main>
This simple page uses Tailwind classes to center content, style a heading, and a button with hover effect. It shows how Tailwind works inside a Next.js project.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Next.js with Tailwind Example</title> <link href="/styles/globals.css" rel="stylesheet" /> </head> <body> <main class="min-h-screen bg-gray-100 flex flex-col items-center justify-center"> <h1 class="text-4xl font-extrabold text-blue-600 mb-6">Hello from Next.js + Tailwind!</h1> <button class="bg-blue-500 hover:bg-blue-700 text-white font-semibold py-2 px-6 rounded"> Press me </button> </main> </body> </html>
Remember to restart your Next.js server after installing Tailwind for changes to take effect.
Use semantic HTML tags like <main> and <button> for better accessibility.
Test your site on different screen sizes to ensure your Tailwind styles respond well.
Tailwind CSS lets you style your Next.js app quickly with utility classes.
Install Tailwind, configure it, and use its classes inside your React components.
Use semantic HTML and responsive classes for accessible, mobile-friendly sites.
Practice
Solution
Step 1: Understand Tailwind CSS role
Tailwind CSS provides utility classes for styling HTML elements quickly without writing custom CSS.Step 2: Identify Next.js features
Next.js handles routing and server-side rendering, not styling or database management.Final Answer:
To quickly style components using utility classes -> Option CQuick Check:
Tailwind CSS = Utility-first styling [OK]
- Confusing Tailwind with JavaScript logic
- Thinking Tailwind manages routing or backend
- Assuming Tailwind replaces Next.js features
div element?Solution
Step 1: Recall JSX syntax for class names
In React and Next.js, useclassNamewith double quotes for CSS classes.Step 2: Check each option
<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 HTMLclass, 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.Final Answer:
<div className="bg-blue-500 text-white p-4">Content</div> -> Option AQuick Check:
JSX uses className="..." [OK]
- Using plain class instead of className in JSX
- Missing quotes around class names
- Using invalid JSX syntax for className
export default function Button() {
return (
<button className="bg-green-500 sm:bg-red-500 text-white p-3">Click Me</button>
)
}Solution
Step 1: Understand Tailwind responsive prefixes
The classbg-green-500applies by default. Thesm:bg-red-500applies only on small screens and above (min-width 640px).Step 2: Identify screen size effect
On screens smaller than 640px, onlybg-green-500applies. On small screens and larger,bg-red-500overrides the background.Final Answer:
Green background -> Option AQuick Check:
Default class applies below sm breakpoint [OK]
- Assuming sm: classes apply on all screen sizes
- Ignoring default class without prefix
- Confusing breakpoint directions
export default function Header() {
return (
<header className="flex justify-center items-center p-4">
<h1 className="text-2xl font-bold">Welcome</h1>
</header>
)
}But the header content is not centered vertically. What is the likely cause?
Solution
Step 1: Understand flexbox vertical centering
Flexbox vertical centering withitems-centerworks if the container has height. Without height, vertical centering has no effect.Step 2: Check header height
The header has padding but no height or min-height. Addingmin-h-screenor a fixed height allows vertical centering.Final Answer:
Missingmin-h-screenor height on header to enable vertical centering -> Option BQuick Check:
Flex vertical center needs container height [OK]
- Thinking justify-center controls vertical alignment
- Assuming padding sets height
- Ignoring need for Tailwind installation
Solution
Step 1: Understand layout direction classes
flex-colstacks items vertically;flex-rowarranges items horizontally.Step 2: Apply responsive prefixes
Usingflex flex-col md:flex-rowmeans vertical stacking by default (mobile) and horizontal layout on medium screens and above (desktop).Step 3: Check other options
<nav className="grid grid-cols-4 md:grid-cols-1 gap-4 p-4">...</nav>usesgrid-cols-4by default, spreading items horizontally on mobile instead of stacking vertically.<nav className="block md:flex-row gap-4 p-4">...</nav>missesflexclass, somd:flex-rowwon't apply properly.<nav className="flex-row flex md:flex-col gap-4 p-4">...</nav>reverses directions incorrectly.Final Answer:
<nav className="flex flex-col md:flex-row gap-4 p-4">...</nav> -> Option DQuick Check:
flex-col mobile + md:flex-row desktop = responsive nav [OK]
- Forgetting to add base flex class
- Using wrong order of flex-row and flex-col
- Confusing grid with flex for simple nav layout
Ready to prove it?
Pass the test and this pattern moves unknown
