To set up Next.js with Tailwind CSS, follow these steps:
If you haven't already created a Next.js project, you can create one using create-next-app.
npx create-next-app@latest my-next-app cd my-next-app
Inside your Next.js project, install Tailwind CSS along with its required dependencies.
npm install -D tailwindcss postcss autoprefixer
Initialize Tailwind CSS by generating the tailwind.config.js and postcss.config.js files.
npx tailwindcss init -p
This will create tailwind.config.js and postcss.config.js files in the root of your project.
Replace the content of tailwind.config.js with the following configuration to enable Tailwind in the relevant files:
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ './pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}', ], theme: { extend: {}, }, plugins: [], }
In your project, open or create the ./styles/globals.css file and add the following lines to import Tailwind's base, components, and utilities:
@tailwind base; @tailwind components; @tailwind utilities;
Now, start the development server to see Tailwind CSS in action:
npm run dev
Your Next.js project should now be set up with Tailwind CSS. You can use Tailwind utility classes in your components to style them.
Here's an example of using Tailwind CSS in a Next.js page (pages/index.js):
export default function Home() { return (); }Welcome to Next.js with Tailwind CSS!
With this setup, you can now start building your Next.js application using Tailwind's utility-first CSS framework!
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3