Skip to content

Introduction to Tailwind CSS in Next.js

Tailwind CSS is a utility-first CSS framework. Instead of providing pre-designed components, it gives you a comprehensive set of low-level utility classes that you can compose directly in your JSX (or HTML) to build any design.

Why Tailwind CSS?

  • Speed of Development: Rapidly build designs without writing custom CSS from scratch. You stay entirely within your component files.
  • No Class Name Conflicts: Since you're using utility classes like flex, text-blue-500, p-4, there are very few instances where you'd need to worry about naming conventions or class conflicts.
  • Highly Customizable: While it provides defaults, you can easily customize Tailwind's configuration (tailwind.config.js) to match your brand's design system.
  • Responsive Design Built-in: Tailwind uses utility classes like sm:, md:, lg: for responsive design, making it intuitive to create mobile-first and adaptive layouts.
  • Performance: Tailwind scans your code and generates only the CSS you actually use, resulting in a very small production CSS file.

Setting up Tailwind CSS in a Next.js Project

If you opted for Tailwind CSS during your create-next-app setup, it's already configured! Next.js and Tailwind CSS work seamlessly together out of the box.

The key files involved are:

  1. tailwind.config.js: This is where you configure Tailwind. It includes content which tells Tailwind which files to scan for utility classes to include in your final CSS bundle.

    tailwind.config.js (Existing file - no changes needed, just for understanding)

    /** @type {import('tailwindcss').Config} */
    module.exports = {
      content: [
        './pages/**/*.{js,ts,jsx,tsx,mdx}',
        './components/**/*.{js,ts,jsx,tsx,mdx}',
        './app/**/*.{js,ts,jsx,tsx,mdx}',
      ],
      theme: {
        extend: {},
      },
      plugins: [],
    };
    

    The content array is crucial: it ensures that any Tailwind classes you use in your pages, components, or app directories are processed and included in your bundled CSS.

  2. postcss.config.js: This file configures PostCSS, which Tailwind uses to transform your CSS.

    postcss.config.js (Existing file - no changes needed)

    module.exports = {
      plugins: {
        tailwindcss: {},
        autoprefixer: {},
      },
    };
    
  3. app/globals.css: As seen earlier, this file imports Tailwind's base styles, components, and utilities using @tailwind directives.

With these files in place, you can start using Tailwind CSS classes directly in your JSX/TSX.

Common Tailwind Classes for Layout, Typography, Colors, Spacing

You've already seen Tailwind classes in action in our Header, Footer, HomePage, DashboardPage, and SingleTaskPage components. Let's briefly recap some common categories:

  • Layout (Flexbox & Grid):
    • flex, flex-row, flex-col, items-center, justify-center, space-x-, space-y-
    • grid, grid-cols-, gap-
  • Spacing (Margin & Padding):
    • m- (margin), p- (padding)
    • mt-, mb-, ml-, mr- (top, bottom, left, right)
    • mx-, my- (horizontal, vertical)
    • Example: p-4 (1rem padding all sides), mb-8 (2rem margin-bottom)
  • Sizing (Width & Height):
    • w-full, h-screen, max-w-md, min-h-[calc(...)]
  • Typography:
    • text-lg, text-xl, text-2xl (font size)
    • font-bold, font-semibold (font weight)
    • text-center, text-left, text-right (text alignment)
    • tracking-tight (letter spacing)
    • leading-relaxed (line height)
    • text-blue-700, text-gray-600 (text color)
  • Colors (Backgrounds & Text):
    • bg-blue-600, bg-gray-100, bg-white
    • text-white, text-gray-800
    • from-blue-600, to-indigo-700 (for gradients)
  • Borders & Shadows:
    • border, rounded-lg, shadow-lg, shadow-md
  • Responsiveness:
    • Prefixes like sm:, md:, lg:, xl: apply styles only at or above a certain breakpoint.
    • Example: md:flex-row will make an element flex row only on medium screens and up.

You've already seen these in action in our previous components. The beauty of Tailwind is that you don't need to remember every class; the documentation is excellent, and VS Code extensions provide intelligent autocompletion.