Create a project with Next.js and Tailwindcss
Setup Next.js and Tailwindcss project.
Let's start with how to create the project with Next.js using Tailwindcss within a few easy steps.
And also how to add Tailwindcss to the Next.js project
1 . Creating the Next.js project.
The first thing we need to open the terminal, in the respective folder where we want to create the Next.js project. ↓
➜ npx create-next-app@latest my-project --typescript --eslint
➜ cd my-project
2 . Install Tailwindcss
Check and make sure you are in the my-project
folder. Now, Install the tailwindcss to the project.
Install the tailwindcss dependencies via npm command as follows
Then run the init command to generate both tailwind.config.js
and postcss.config.js
.
➜ npm install -D tailwindcss postcss autoprefixer
➜ npx tailwindcss init -p
3 . Arrange your template paths
Open the tailwind.config.js
and make changes
Add the paths to all of your template files in your tailwind.config.js
file.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx}",
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
// Or if using `src` directory:
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
4. Add the Tailwind directives to your CSS
Open the globals.css
file and add the directives for applying styles.
Add the @tailwind
directives for each Tailwind’s layers to your globals.css
file.
@tailwind base;
@tailwind components;
@tailwind utilities;
5. Start your build process
Now, start the project, Run your build process with npm run dev
.
npm run dev
6 . Add Tailwind styles to the project
Open index.tsx
file and start using the styles
export default function Home() {
return (
<h1 className="text-3xl font-bold underline">
Hello world!
</h1>
)
}
Done! Created the Next.js & tailwindcss project🥳. Thank you.