Mục tiêu: Sau 60–90 phút, bạn có thể đọc, hiểu, và tự tay dựng các UI cơ bản với Tailwind CSS, biết cách đọc class, tuỳ biến theme, và tổ chức code sạch.
Mục lục
- Tailwind là gì? Vì sao nên dùng?
- Cài đặt nhanh (CDN) & thiết lập dự án chuẩn (Vite/PostCSS)
- Tư duy Utility-first & JIT
- Cú pháp class cơ bản
- Thang đo mặc định (spacing, font, màu…)
- Responsive, trạng thái & biến thể
- Tuỳ biến
tailwind.config.js - Các nhóm tiện ích hay dùng
- Ví dụ thực chiến
- Mẹo quy ước & kiến trúc
- FAQ ngắn
1) Tailwind là gì? Vì sao nên dùng?
Tailwind CSS là framework utility-first: thay vì viết CSS tuỳ biến cho từng component, bạn gắn class ngắn gọn (utilities) trực tiếp vào HTML. Ví dụ: p-4 bg-blue-600 text-white rounded-lg.
Lợi ích chính:
- Tốc độ dựng UI rất nhanh, không rời khỏi HTML.
- JIT compiler chỉ build những class bạn dùng → CSS cuối gọn nhẹ.
- Hệ token/thang đo nhất quán: khoảng cách, màu, font, breakpoint…
- Dễ scale với variants:
hover:,md:,dark:,aria-checked:…
<a id="cai-dat"></a>
2) Cài đặt
2.1 Cài đặt siêu nhanh (CDN) — phù hợp demo/học nhanh
<!doctype html>
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-6">
<button class="px-4 py-2 bg-indigo-600 text-white rounded-md hover:bg-indigo-700">Nút</button>
</body>
</html>
Lưu ý: CDN tiện học, nhưng production nên build để tối ưu dung lượng & kiểm soát phiên bản.
2.2 Thiết lập dự án chuẩn (Vite + PostCSS)
# 1) Tạo dự án
npm create vite@latest my-app -- --template vanilla
cd my-app
# 2) Cài Tailwind + PostCSS + Autoprefixer
npm i -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Cấu trúc chính:
my-app/
├─ index.html
├─ src/main.js
├─ src/style.css # nhập directives @tailwind
├─ tailwind.config.js
└─ postcss.config.js
src/style.css
@tailwind base;
@tailwind components;
@tailwind utilities;
tailwind.config.js (quét file để JIT biết class nào cần build)
/** @type {import('tailwindcss').Config} */
export default {
content: [
'./index.html',
'./src/**/*.{js,ts,jsx,tsx,vue}',
],
theme: {
extend: {},
},
plugins: [],
}
Chạy dev:
npm run dev
Build production:
npm run build
<a id="utility"></a>
3) Tư duy Utility-first & JIT
- Utility-first: mỗi class đại diện cho một thuộc tính CSS (hoặc một nhóm nhỏ hợp lý). Ví dụ
flex,items-center,gap-4. - JIT: Khi bạn gõ
bg-[#0ea5e9]hayh-[42px], Tailwind JIT sinh CSS ngay lập tức cho giá trị tuỳ ý đó.
Cú pháp giá trị tuỳ ý (arbitrary values): đặt trong dấu ngoặc vuông [].
- Ví dụ:
w-[72px],bg-[rgb(10,10,10)],grid-cols-[200px_auto].
<a id="cu-phap"></a>
4) Cú pháp class cơ bản (ý nghĩa từng phần)
<property>-<scale> # p-4 => padding: theme.spacing[4]
<property>-<color>-<shade> # text-slate-700 => color: slate@700
<variant>:<property>-<scale> # hover:bg-blue-600
<breakpoint>:<property>-<...> # md:flex
!important modifier # !mt-0 => margin-top: 0 !important
[arbitrary] # w-[37rem]
Ví dụ:
<div class="p-4 md:p-6 bg-slate-50 rounded-xl shadow hover:shadow-lg transition">
<h2 class="text-lg font-semibold text-slate-900">Thẻ nội dung</h2>
<p class="mt-2 text-slate-600">Mô tả ngắn…</p>
</div>
<a id="thang-do"></a>
5) Thang đo mặc định quan trọng
5.1 Spacing (px ~ rem)
| Class | rem | px (mặc định 1rem=16px) |
|---|---|---|
p-0/m-0 | 0 | 0 |
-0.5 | 0.125 | 2 |
-1 | 0.25 | 4 |
-1.5 | 0.375 | 6 |
-2 | 0.5 | 8 |
-2.5 | 0.625 | 10 |
-3 | 0.75 | 12 |
-3.5 | 0.875 | 14 |
-4 | 1 | 16 |
-5 | 1.25 | 20 |
-6 | 1.5 | 24 |
-8 | 2 | 32 |
-10 | 2.5 | 40 |
-12 | 3 | 48 |
-16 | 4 | 64 |
-20 | 5 | 80 |
-24 | 6 | 96 |
-32 | 8 | 128 |
-40 | 10 | 160 |
-64 | 16 | 256 |
Đọc
p-6=> padding: 1.5rem (24px).px-4=> padding trái/phải 16px.
5.2 Màu & sắc độ
- Tên màu:
slate, gray, zinc, neutral, stone, red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose. - Sắc độ:
50(nhạt) →900(đậm). Ví dụ:bg-emerald-500,text-slate-700. - Màu tuỳ ý:
bg-[#0ea5e9]hoặctext-[rgb(30,41,59)].
5.3 Breakpoints mặc định
| Prefix | Min-width |
|---|---|
sm: | 640px |
md: | 768px |
lg: | 1024px |
xl: | 1280px |
2xl: | 1536px |
Cách đọc: md:flex nghĩa là từ 768px trở lên áp dụng display:flex.
<a id="responsive"></a>
6) Responsive, trạng thái & biến thể
6.1 Responsive
<div class="p-4 sm:p-6 md:p-8 lg:p-10">Nội dung</div>
6.2 Trạng thái (pseudo-class)
- Tương tác:
hover:,focus:,active:,visited:,focus-visible: - Form:
disabled:,required:,checked:,invalid: - ARIA:
aria-checked:,aria-expanded: - Dark mode:
dark:(khiclass="dark"trênhtml/body)
<button class="px-4 py-2 rounded-md bg-blue-600 text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400">Nút</button>
6.3 Group & Peer
<!-- group: style con khi hover cha -->
<div class="group p-4 rounded-lg border">
<span class="text-slate-600 group-hover:text-slate-900">Label</span>
</div>
<!-- peer: style dựa trên trạng thái element ngang hàng -->
<label class="flex items-center gap-2">
<input type="checkbox" class="peer accent-blue-600"/>
<span class="text-slate-600 peer-checked:text-blue-600">Tôi đồng ý</span>
</label>
6.4 Dark mode
Thiết lập trong tailwind.config.js (mặc định media). Khuyến nghị dùng class để chủ động:
darkMode: 'class'
<html class="dark">
<body class="bg-white text-slate-900 dark:bg-slate-900 dark:text-slate-100">...</body>
</html>
<a id="config"></a>
7) Tuỳ biến tailwind.config.js
export default {
content: ['./index.html','./src/**/*.{js,ts,jsx,tsx,vue}'],
theme: {
extend: {
colors: {
brand: {
50: '#eef6ff',
500: '#1e90ff',
700: '#1a73e8',
},
},
spacing: {
18: '4.5rem', // dùng: p-18 => 72px
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif'],
},
screens: {
'3xl': '1920px',
},
},
},
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
require('@tailwindcss/aspect-ratio'),
require('@tailwindcss/line-clamp'),
],
}
Giải thích nhanh:
extend.colors.brand.500→bg-brand-500,text-brand-700…spacing.18→m-18,p-18= 72px.screens.3xl→ dùng3xl:cho layout cực rộng.- Plugins giúp có thêm utilities cho form, bài viết (prose), tỉ lệ khung, rút gọn dòng…
<a id="nhom-tien-ich"></a>
8) Các nhóm tiện ích hay dùng (kèm ý nghĩa)
8.1 Layout
- Display:
block,inline-block,flex,grid,hidden. - Container:
container mx-auto px-4(kết hợp với plugin/container nếu cần). - Overflow:
overflow-hidden|auto|scroll. - Position:
relative,absolute,fixed,sticky,inset-0,top-0…
8.2 Flexbox
<div class="flex items-center justify-between gap-4">
<div>A</div>
<div class="flex-1">B (chiếm phần còn lại)</div>
<div>C</div>
</div>
items-center(align-items),justify-between(justify-content),gap-4(khoảng cách phần tử con),flex-1(flex:1 1 0%).
8.3 Grid
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
<div class="bg-white p-4 rounded-xl shadow">Card</div>
<!-- ... -->
</div>
grid-cols-3(3 cột),gap-6(khoảng cách), có thể dùnggrid-cols-[200px_auto](tùy ý).
8.4 Khoảng cách & kích thước
- Margin:
m-4,mx-auto,mt-2, số âm:-mt-2. - Padding:
p-6,px-3,py-1.5. - Width/Height:
w-64,max-w-xl,h-10,min-h-[60vh].
8.5 Typography
- Font:
text-sm|base|lg|xl|2xl|...,font-medium|semibold|bold. - Màu chữ:
text-slate-700,text-white. - Dãn dòng:
leading-6, khoảng chữ:tracking-wide.
8.6 Màu nền, viền, đổ bóng
- Nền:
bg-slate-50,bg-blue-600, gradient:bg-gradient-to-r from-blue-500 to-cyan-500. - Viền:
border,border-slate-200,rounded-lg,ring-2 ring-blue-400. - Đổ bóng:
shadow,shadow-lg,shadow-[0_2px_80px_-20px_rgba(0,0,0,0.35)].
8.7 Hiệu ứng
- Transition:
transition,duration-300,ease-in-out. - Transform:
scale-95,rotate-6,translate-y-1. - Filter:
blur,backdrop-blur.
<a id="vi-du"></a>
9) Ví dụ thực chiến
9.1 Nút (3 biến thể)
<!-- Primary -->
<button class="inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-blue-600 text-white hover:bg-blue-700 active:scale-95 transition">
Lưu
</button>
<!-- Secondary -->
<button class="inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-slate-100 text-slate-700 hover:bg-slate-200">
Huỷ
</button>
<!-- Ghost -->
<button class="inline-flex items-center px-3 py-2 rounded-md text-slate-600 hover:bg-slate-100">
Tuỳ chọn
</button>
9.2 Card sản phẩm
<article class="group rounded-2xl border border-slate-200 overflow-hidden bg-white shadow-sm">
<img src="https://picsum.photos/640/360" alt="thumb" class="aspect-video w-full object-cover group-hover:scale-[1.02] transition"/>
<div class="p-4">
<h3 class="text-lg font-semibold text-slate-900">Tai nghe XYZ</h3>
<p class="mt-1 text-slate-600">Chống ồn chủ động, pin 35h.</p>
<div class="mt-4 flex items-center justify-between">
<span class="text-xl font-bold text-emerald-600">1.490.000₫</span>
<button class="px-4 py-2 rounded-lg bg-emerald-600 text-white hover:bg-emerald-700">Mua</button>
</div>
</div>
</article>
9.3 Navbar responsive
<header class="border-b">
<div class="container mx-auto px-4 h-14 flex items-center justify-between">
<a class="font-semibold">Brand</a>
<nav class="hidden md:flex items-center gap-6 text-slate-600">
<a class="hover:text-slate-900" href="#">Sản phẩm</a>
<a class="hover:text-slate-900" href="#">Giải pháp</a>
<a class="hover:text-slate-900" href="#">Bảng giá</a>
</nav>
<button class="md:hidden p-2 rounded hover:bg-slate-100">☰</button>
</div>
</header>
9.4 Form đăng nhập (với plugin forms)
<form class="max-w-sm mx-auto p-6 bg-white rounded-2xl shadow">
<h2 class="text-xl font-semibold">Đăng nhập</h2>
<div class="mt-4 grid gap-3">
<label class="grid gap-1">
<span class="text-sm text-slate-600">Email</span>
<input type="email" class="mt-1" placeholder="ban@congty.com"/>
</label>
<label class="grid gap-1">
<span class="text-sm text-slate-600">Mật khẩu</span>
<input type="password" class="mt-1" placeholder="••••••••"/>
</label>
<button class="mt-4 px-4 py-2 rounded-lg bg-blue-600 text-white hover:bg-blue-700">Tiếp tục</button>
</div>
</form>
9.5 Bố cục dashboard đơn giản
<div class="min-h-screen bg-slate-50">
<aside class="hidden md:block md:w-64 p-4 bg-white border-r fixed inset-y-0">Sidebar</aside>
<main class="md:ml-64 p-6">
<h1 class="text-2xl font-semibold">Bảng điều khiển</h1>
<section class="mt-6 grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-6">
<div class="p-4 bg-white rounded-xl shadow">Doanh thu</div>
<div class="p-4 bg-white rounded-xl shadow">Người dùng</div>
<div class="p-4 bg-white rounded-xl shadow">Đơn hàng</div>
<div class="p-4 bg-white rounded-xl shadow">Chuyển đổi</div>
</section>
</main>
</div>
<a id="meo"></a>
10) Mẹo quy ước & kiến trúc (scale dài hạn)
- Quy ước prefix theo ngữ nghĩa:
btn,carddùng @apply trong file CSS khi cần tái sử dụng nhiều nơi.
/* src/components.css */
@tailwind components;
@layer components {
.btn {
@apply inline-flex items-center justify-center gap-2 px-4 py-2 rounded-lg font-medium transition;
}
.btn-primary { @apply btn bg-blue-600 text-white hover:bg-blue-700; }
.btn-ghost { @apply btn text-slate-700 hover:bg-slate-100; }
}
- Tối ưu đọc: nhóm class theo trật tự: layout → box → spacing → typo → màu → hiệu ứng. Ví dụ:
flex items-center gap-2 px-4 py-2 rounded bg-blue-600 text-white hover:bg-blue-700. - Hạn chế
!important: chỉ dùng khi override cần thiết (!mt-0). - Extract token vào
theme.extendđể thống nhất brand. - Kiểm soát size build: cấu hình chính xác
content, tránh quét thư mục thừa.
<a id="faq"></a>
11) FAQ ngắn
Hỏi: Có cần biết CSS thuần không? Đáp: Có. Tailwind chỉ là lớp tiện ích, hiểu CSS giúp bạn dùng đúng và linh hoạt.
Hỏi: Tôi có thể viết class tùy ý cho mọi thuộc tính không?
Đáp: JIT hỗ trợ rất nhiều qua giá trị tùy ý []. Nếu chưa có utility, cân nhắc @apply hoặc plugin.
Hỏi: Làm sao bật dark mode?
Đáp: darkMode: 'class' và thêm class="dark" vào html/body, sau đó dùng dark:.
Checklist tự kiểm (khi review PR)
- Class có được nhóm theo trật tự dễ đọc?
- Đã dùng token brand từ
theme.extend? - Responsive & hover/focus đã đủ?
- Kích thước build CSS có hợp lý?
Tài liệu tham khảo tiếp
- Tra cứu nhanh: gõ tên utility + "tailwind" trên web (ví dụ: "tailwind line-clamp").
- Khuyến nghị đọc thêm về: container queries, ARIA variants, typography plugin.
Kết luận: Nắm vững utility, thang đo, và variants là đủ để bạn bứt tốc với Tailwind. Bắt đầu bằng CDN để thử nghiệm, sau đó chuyển sang setup build để tối ưu production.