---
title: "Sử dụng Tailwind CSS trong NextJS"
description: "NextJS là framework cho phép chúng ta thực hiện Server Side Rendering (SSR). Tailwind CSS là một utility-first CSS framework cho phép chúng ta tạo style cho trang web mà không cần viết nhiều CSS."
date: "2021-05-21"
author:
  name: "Nguyen Pham"
  role: "Xin chào. Mình thích lập trình và công nghệ. Đây là blog mình chia sẻ lại các kiến thức về lập trình, công nghệ."
  avatar: "http://0.gravatar.com/avatar/6fb61398e24046b34e13870dd6c357b9?s=128&d=mm&r=g"
category: "JavaScript"
tags: ["JavaScript", "Programming", "css", "javascript", "nextjs", "react", "tailwindcss"]
coverImage: "/blog/covers/su-dung-tailwind-css-trong-nextjs.png"
featured: false
readingTime: "1 min read"
---

NextJS là framework cho phép chúng ta thực hiện `Server Side Rendering (SSR)`

Tailwind CSS là một utility-first CSS framework cho phép chúng ta tạo style cho trang web mà không cần viết nhiều CSS.

## 1. Cài đặt

Tạo nextjs app

```
  npx create-next-app next-tailwindcss
  cd next-tailwindcss
```

Cài đặt Tailwind CSS

```
  yarn install -D tailwindcss@latest postcss@latest autoprefixer@lates
```

## 2. Cấu hình

Tạo config Tailwind CSS: `tailwind.config.js` , `postcss.config.js`

```
  npx tailwindcss init -p
```

`tailwind.config.js`

```
module.exports = {
  mode: "jit",
  purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
};
```

`postcss.config.js`

```
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}
```

`package.json`

```
{
  "name": "next-tailwindcss",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "10.2.2",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "autoprefixer": "^10.2.5",
    "postcss": "^8.3.0",
    "tailwindcss": "^2.1.2"
  }
}
```

## 3. Chạy

```
yarn dev
```

Tham khảo:

- <https://tailwindcss.com/docs/just-in-time-mode#enabling-jit-mode>
- <https://tailwindcss.com/docs/guides/nextjs>
- <https://nextjs.org/docs>
