Next.js Tailwind CSS
Tailwind CSS is a utility-first CSS framework that provides a set of atomic CSS classes, allowing you to quickly build modern user interfaces directly in HTML. The integration between Next.js and Tailwind CSS is very smooth. This chapter will guide you through setting up and using Tailwind CSS in your Next.js project.
1. Why Choose Tailwind CSS?
- Rapid Development: By composing utility classes, you can quickly implement complex designs without writing custom CSS.
- Constrained Design: Predefined design systems (colors, spacing, font sizes, etc.) help maintain UI consistency.
- High Performance: With PurgeCSS (built into Tailwind CSS v2.0+), unused CSS in production is automatically removed, generating extremely small CSS files.
- Customizable: Easily extend or modify the default design system through the
tailwind.config.jsfile.
2. Installing and Configuring Tailwind CSS in Next.js
Starting from Next.js 12, official built-in support for Tailwind CSS is provided, making the integration process very simple.
Step 1: Create a Next.js Project
If you don't have a project yet, you can use create-next-app with the Tailwind CSS option:
This will automatically configure all necessary files for you.
Step 2: Manual Installation and Configuration
If you want to add Tailwind CSS to an existing project, follow these steps.
-
Install dependencies:
-
Generate configuration files:
This command creates two important files:
tailwind.config.js: For configuring Tailwind CSS.postcss.config.js: For configuring PostCSS, which Next.js automatically uses.
-
Configure
tailwind.config.js:Open the
tailwind.config.jsfile and configure thecontentfield to tell Tailwind CSS which files to scan for used classes. -
Import Tailwind in global CSS:
Open the
styles/globals.cssfile and replace its contents with the following to import Tailwind's base styles, components, and utilities. -
Import global styles in
_app.js:Make sure the
styles/globals.cssfile is imported inpages/_app.js.
3. Using Tailwind CSS in Components
After completing the above configuration, you can use Tailwind CSS utility classes directly in any component or page.
Example: Creating a responsive card component
Using the component in a page:
4. Advanced Usage
Custom Themes
You can customize the design system by modifying the theme object in tailwind.config.js. For example, adding custom colors or fonts.
Now you can use custom classes like bg-brand-blue or font-sans in your project.
Using @apply to Extract Component Classes
If you find yourself repeatedly using the same utility class combinations, you can use the @apply directive to extract them into custom CSS classes.
styles/components.css (needs to be imported into globals.css):
Then you can use class="btn-primary" in HTML.
Note: Overusing
@applymay go against Tailwind's philosophy. It's generally recommended to prioritize utility classes and only extract component classes when necessary.
Summary
Combining Tailwind CSS with Next.js can greatly improve development efficiency and UI consistency. With simple configuration, you get a powerful and highly customizable styling solution. In the next chapter, we'll learn how to handle static assets like images and fonts in Next.js.