Setup Nextjs Tailwind CSS Styled Components with TypeScript
- Mohammad Abu Mattar
- Frontend
- 08 Nov, 2022
- 05 Mins read
Introduction
In this post, we will setup Nextjs Tailwind CSS Styled Components with TypeScript, and we will use the following tools:
- Nextjs
- Tailwind CSS
- Styled Components
- TypeScript
Prerequisites
You need to have the following tools installed on your system:
- Node.js
- Yarn
Setup Nextjs App
Step 1: Create a Nextjs app
First, we will create a Nextjs app using the following command:
Then, you will be asked to choose a TypeScript and ESLint, choose the following options:
Step 2: Go to the Nextjs app directory
After creating the Nextjs app, we need to go to the Nextjs app directory using the following command:
Step 3: Prepare the Nextjs app
First, we need to install the following dependencies:
Next, we need to create a .npmrc
file in the root directory of the Nextjs app, and we will add the following content to the .npmrc
file:
After, we need to create a .nvmrc
file in the root directory of the Nextjs app, and we will add the following content to the .nvmrc
file:
Then, we need to create a .yarnrc
file in the root directory of the Nextjs app, and we will add the following content to the .yarnrc
file:
Then, we need to create a .prettierrc
file in the root directory of the Nextjs app, and we will add the following content to the .prettierrc
file:
Then, we need to create a .prettierignore
file in the root directory of the Nextjs app, and we will add the following content to the .prettierignore
file:
Then, we need to create a .eslintrc
file in the root directory of the Nextjs app, and we will add the following content to the .eslintrc
file:
Then, we need to create a .eslintignore
file in the root directory of the Nextjs app, and we will add the following content to the .eslintignore
file:
Add, the engines field to the package.json
file, and we will add the following content to the package.json
file:
Step 4: Install Tailwind CSS
First, we will install Tailwind CSS using the following command:
Then, we will create a Tailwind CSS configuration file using the following command:
After, we will change the tailwind.config.js
file, and we will add the following content to the tailwind.config.js
file:
Step 5: Install Styled Components
First, we will install Styled Components using the following command:
Then, we will create a .babelrc
file in the root directory of the Nextjs app using the following command:
Then, we will add the following code to the .babelrc
file:
Step 6: Install and Setup twin.macro
First, we will install twin.macro
using the following command:
Then, we will add the following code to the .babelrc
file:
Add, the babelMacros
field to the package.json
file, and we will add the following content to the package.json
file:
Step 7: Setup _document.tsx
First, we will create a pages/_document.tsx
file using the following command:
Then, we will add the following code to the pages/_document.tsx
file:
Step 8: Setup _app.tsx
We will add the following code to the pages/_app.tsx
file:
Step 9: Start styling
We will create a styles
directory in the root directory of the Nextjs app using the following command:
Then, we will create a main.ts
file in the styles
directory using the following command:
Then, we will add the following code to the styles/main.ts
file:
Then, we will redisign the pages/index.tsx
file using the following code:
Source Code
The source code for this tutorial is available on GitHub
Live Demo
The live demo for this tutorial is available on Vercel
Conclusion
In this tutorial, we have learned how to use Tailwind CSS with Nextjs and Styled Components. We have also learned how to use TypeScript with Nextjs and Styled Components.