Tailwind CSS v4 — Utility-First CSS Framework
Tailwind CSS là gì?
Tailwind CSS = utility-first CSS framework — thay vì viết CSS riêng cho từng component, bạn dùng hàng trăm class nhỏ (flex, pt-4, text-lg) ngay trong HTML để build giao diện nhanh và nhất quán.
Khác biệt cốt lõi so với Bootstrap / Material UI:
| Tailwind CSS | Bootstrap | |
|---|---|---|
| Triết lý | Utility-first — build từ class nhỏ | Component-first — dùng component có sẵn |
| Customization | Design system trong config → build ra class | Ghi đè CSS variable |
| File output | Chỉ class bạn dùng (tree-shake) | Cả thư viện hoặc import từng phần |
| HTML | Nhiều class hơn, nhưng CSS file nhỏ hơn | Ít class hơn, CSS file to hơn |
| Design system | Bạn tự định nghĩa màu, spacing, font... | Có sẵn, ghi đè nếu cần |
<div class="card__header--highlighted">, không CSS file riêng cho từng component. Đổi giao diện mà không sợ break CSS ở chỗ khác. Đây là trade-off mà Tailwind chọn.v4.0 — Oxide Engine rewrite
Tailwind CSS v4 là bản viết lại từ đầu bằng Rust (Oxide Engine) — thay đổi toàn diện cách Tailwind hoạt động bên dưới:
Oxide Engine (Rust)
CSS-first config
tailwind.config.js. Dùng @theme, @utility, @custom-variant trực tiếp trong CSS. IDE autocomplete vẫn chạy ngon qua LSP.Automatic content detection
content array nữa. Tailwind tự scan tất cả file trong project, tự biết class nào cần generate.OKLCH + P3 color
Code thay đổi dễ thấy
/* v3 — @tailwind directives */
@tailwind base;
@tailwind components;
@tailwind utilities;
/* v4 — single import */
@import "tailwindcss";
/* v3 — tailwind.config.js */
module.exports = {
theme: {
extend: { colors: { brand: '#1e40af' } }
}
}
/* v4 — CSS-first config trong file CSS */
@import "tailwindcss";
@theme {
--color-brand: #1e40af;
--color-brand-light: oklch(0.7 0.15 250);
}
Tất cả token định nghĩa trong @theme đều tự động expose thành CSS custom properties — dùng được ở mọi nơi: var(--color-brand).
Installation
# Vite
npm install tailwindcss @tailwindcss/vite
# Next.js (PostCSS plugin vẫn hỗ trợ)
npm install tailwindcss @tailwindcss/postcss
# Nuxt
npx nuxi module add @nuxtjs/tailwindcss
# CDN — dùng thử nhanh (không khuyến nghị production)
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
/* main.css — file CSS chính */
@import "tailwindcss";
@theme {
--color-primary: oklch(0.55 0.2 250);
}
<!-- Hello World — dùng thử ngay -->
<div class="min-h-screen flex items-center justify-center bg-gray-100">
<div class="bg-white p-8 rounded-2xl shadow-lg text-center">
<h1 class="text-3xl font-bold text-primary mb-4">
Tailwind CSS v4
</h1>
<p class="text-gray-600">Utility-first CSS, Rust-powered.</p>
</div>
</div>
Version timeline
| Phiên bản | Thời gian | Highlights |
|---|---|---|
| v4.0 | Tháng 01/2025 | Oxide Engine (Rust), CSS-first config, OKLCH, auto content detection, container queries, 3D transforms, radial/conic gradients |
| v4.1 | Tháng 04/2025 | text-shadow utilities, mask utilities, CSS-first @plugin, improved LSP |
| v4.2 | Giữa 2025 | Màu mới (mauve, olive, mist, taupe), logical properties (pbs-*, mbs-*...), webpack plugin, import aliases |
| v4.3 | Tháng 05/2026 | Scrollbar styling (scrollbar-thin, scrollbar-thumb-*, scrollbar-track-*), container-size, zoom, tab-size |
"Ba thứ thay đổi toàn bộ trải nghiệm: (1) Oxide Engine viết bằng Rust — build nhanh gấp 5x, incremental 100x. (2) CSS-first config — bỏ
tailwind.config.js, dùng@theme+@utilityngay trong CSS. (3) OKLCH color space mặc định — màu đồng đều hơn về mặt perceptive, hỗ trợ P3 wide gamut."
Vì sao chọn Tailwind thay vì raw CSS?
Đây là câu hỏi phỏng vấn cực hay gặp khi bạn ghi Tailwind vào CV:
Tốc độ viết code
Design system nhất quán
p-4, p-6, p-8), color palette, font size — tất cả limited choices. Không có padding: 13px tự chế.Bundle siêu nhỏ
Team collaboration
Tailwind: Utility-first, viết class trong HTML, bundle nhỏ vì class shared, design system built-in. Học hơi dốc lúc đầu vì phải nhớ tên class.
CSS Modules: Viết CSS bình thường, scope local tự động. Quen thuộc với dev CSS truyền thống. Nhưng không có design system constraint → dễ drift style.
Styled Components: CSS-in-JS, style gắn với component React. Dynamic style dễ hơn. Nhưng runtime overhead, bundle to hơn.
Tailwind CSS v3 vs v4 — khác biệt chính
| v3 | v4 | |
|---|---|---|
| Engine | JavaScript (PostCSS) | Rust (Oxide Engine) |
| Config | tailwind.config.js | CSS @theme block |
| Content detection | Manual content array | Automatic |
| Color space | sRGB | OKLCH (P3-ready) |
| Import | @tailwind base/components/utilities | @import "tailwindcss" |
| Token exposure | Manual CSS var | Auto expose all tokens |
| Container queries | Plugin (@tailwindcss/container-queries) | Built-in |
| 3D transforms | Không có | Built-in (rotate-x-*, rotate-y-*, rotate-z-*) |
| Gradients | Linear only | Radial + conic (bg-radial-*, bg-conic-*) |
| Text shadow | Plugin | Built-in (v4.1) |
| Scrollbar | Plugin | Built-in (v4.3) |
Lộ trình học
| Trang | Nội dung |
|---|---|
| 1. Layout & Spacing | Display, position, container queries, flexbox, grid, spacing, sizing, overflow, scrollbar — 19 bài tập thực hành |
| 2. Typography & Colors | Font, text size, color OKLCH, text shadow (v4.1), opacity, dark mode |
| 3. Backgrounds & Borders | Background color/image/gradient (radial/conic v4.0), border, outline, ring, divide |
| 4. Effects & Filters | Shadow, blur, backdrop, transform, transition, animation, 3D transforms (v4.0) |
| 5. Responsive Design | Breakpoints, container queries (@container), mobile-first, logical properties (v4.2) |
| 6. Dark Mode & Theming | dark: variant, @custom-variant, color-scheme, system preference |
| 7. Customization v4 | @theme, @utility, @custom-variant, @plugin, extend/override |
| 8. Practical Project | Xây dựng landing page hoàn chỉnh bằng Tailwind CSS v4 |
| 9. Câu hỏi phỏng vấn | 50+ Q&A Tailwind CSS cho dev frontend |
5. Practical Todo App
Hướng dẫn chi tiết từng bước xây dựng Todo App hoàn chỉnh với Nuxt 4 — composables, useState, components, TypeScript, localStorage.
1. Layout & Spacing
Tailwind CSS v4 layout system — container queries, display types, flexbox, grid, spacing (padding, margin, gap). Bài tập từng khái niệm.