tailwind-4
Tailwind CSS 4 patterns and best practices. Trigger: When styling with Tailwind - cn(), theme variables, no var() in className.
- License 许可
- Apache-2.0
- 最近更新
- 2026-08-26
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- tailwind-4
- license
- Apache-2.0
- metadata
- { "author": "gentleman-programming", "version": "1.1" }
- description
- Tailwind CSS 4 patterns and best practices. Trigger: When styling with Tailwind - cn(), theme variables, no var() in className.
02 资源文件 13
- 参考文档.skill-meta.json437 B
- 脚本assets/01-cn-example.ts512 B
- 资产文件assets/01-example.tsx26 B
- 资产文件assets/tailwind-complete-example.tsx6.6 KB
- 参考文档README.md2.3 KB
- 参考文档references/01-guide.md38 B
- 参考文档references/01-theme-variables.md250 B
- 参考文档references/02-cn-utility.md263 B
- 参考文档references/02-patterns.md29 B
- 参考文档references/03-responsive-design.md254 B
- 参考文档references/cn-utility-dynamic-styling.md2.8 KB
- 参考文档references/responsive-dark-mode.md2.2 KB
- 参考文档references/tailwind-fundamentals.md2.2 KB
03 SKILL.md 正文
Styling Decision Tree
Tailwind class exists? → className="..."
Dynamic value? → style={{ width: `${x}%` }}
Conditional styles? → cn("base", condition && "variant")
Static only? → className="..." (no cn() needed)
Library can't use class?→ style prop with var() constants
Critical Rules
Never Use var() in className
// ❌ NEVER: var() in className
<div className="bg-[var(--color-primary)]" />
<div className="text-[var(--text-color)]" />
// ✅ ALWAYS: Use Tailwind semantic classes
<div className="bg-primary" />
<div className="text-slate-400" />
Never Use Hex Colors
// ❌ NEVER: Hex colors in className
<p className="text-[#ffffff]" />
<div className="bg-[#1e293b]" />
// ✅ ALWAYS: Use Tailwind color classes
<p className="text-white" />
<div className="bg-slate-800" />
The cn() Utility
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
When to Use cn()
// ✅ Conditional classes
<div className={cn("base-class", isActive && "active-class")} />
// ✅ Merging with potential conflicts
<button className={cn("px-4 py-2", className)} /> // className might override
// ✅ Multiple conditions
<div className={cn(
"rounded-lg border",
variant === "primary" && "bg-blue-500 text-white",
variant === "secondary" && "bg-gray-200 text-gray-800",
disabled && "opacity-50 cursor-not-allowed"
)} />
When NOT to Use cn()
// ❌ Static classes - unnecessary wrapper
<div className={cn("flex items-center gap-2")} />
// ✅ Just use className directly
<div className="flex items-center gap-2" />
Style Constants for Charts/Libraries
When libraries don't accept className (like Recharts):
// ✅ Constants with var() - ONLY for library props
const CHART_COLORS = {
primary: "var(--color-primary)",
secondary: "var(--color-secondary)",
text: "var(--color-text)",
gridLine: "var(--color-border)",
};
// Usage with Recharts (can't use className)
<XAxis tick={{ fill: CHART_COLORS.text }} />
<CartesianGrid stroke={CHART_COLORS.gridLine} />
Dynamic Values
// ✅ style prop for truly dynamic values
<div style={{ width: `${percentage}%` }} />
<div style={{ opacity: isVisible ? 1 : 0 }} />
// ✅ CSS custom properties for theming
<div style={{ "--progress": `${value}%` } as React.CSSProperties} />
Common Patterns
Flexbox
<div className="flex items-center justify-between gap-4" />
<div className="flex flex-col gap-2" />
<div className="inline-flex items-center" />
Grid
<div className="grid grid-cols-3 gap-4" />
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6" />
Spacing
// Padding
<div className="p-4" /> // All sides
<div className="px-4 py-2" /> // Horizontal, vertical
<div className="pt-4 pb-2" /> // Top, bottom
// Margin
<div className="m-4" />
<div className="mx-auto" /> // Center horizontally
<div className="mt-8 mb-4" />
Typography
<h1 className="text-2xl font-bold text-white" />
<p className="text-sm text-slate-400" />
<span className="text-xs font-medium uppercase tracking-wide" />
Borders & Shadows
<div className="rounded-lg border border-slate-700" />
<div className="rounded-full shadow-lg" />
<div className="ring-2 ring-blue-500 ring-offset-2" />
States
<button className="hover:bg-blue-600 focus:ring-2 active:scale-95" />
<input className="focus:border-blue-500 focus:outline-none" />
<div className="group-hover:opacity-100" />
Responsive
<div className="w-full md:w-1/2 lg:w-1/3" />
<div className="hidden md:block" />
<div className="text-sm md:text-base lg:text-lg" />
Dark Mode
<div className="bg-white dark:bg-slate-900" />
<p className="text-gray-900 dark:text-white" />
Arbitrary Values (Escape Hatch)
// ✅ OK for one-off values not in design system
<div className="w-[327px]" />
<div className="top-[117px]" />
<div className="grid-cols-[1fr_2fr_1fr]" />
// ❌ Don't use for colors - use theme instead
<div className="bg-[#1e293b]" /> // NO
Scroll Animations (IntersectionObserver + CSS Keyframes)
Zero-dependency scroll-reveal animations using Tailwind arbitrary values and native IntersectionObserver.
Setup
Add to your <head> or entry script:
<script>
(function () {
const style = document.createElement("style");
style.textContent = `
.animate-on-scroll { animation-play-state: paused !important; }
.animate-on-scroll.animate { animation-play-state: running !important; }
`;
document.head.appendChild(style);
const once = true;
if (!window.__inViewIO) {
window.__inViewIO = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("animate");
if (once) window.__inViewIO.unobserve(entry.target);
}
});
}, { threshold: 0.2, rootMargin: "0px 0px -10% 0px" });
}
window.initInViewAnimations = function (selector = ".animate-on-scroll") {
document.querySelectorAll(selector).forEach((el) => {
window.__inViewIO.observe(el);
});
};
document.addEventListener("DOMContentLoaded", () => initInViewAnimations());
})();
</script>
Keyframes
@keyframes fadeUpIn {
0% { opacity: 0; transform: translateY(30px); filter: blur(8px); }
100% { opacity: 1; transform: translateY(0); filter: blur(0px); }
}
Usage
<div class="animate-on-scroll [animation:fadeUpIn_0.8s_ease-out_0.1s_both]">
Reveals on scroll
</div>
<!-- With custom timing -->
<div class="animate-on-scroll [animation:fadeUpIn_1.2s_ease-out_0.3s_both]">
Slower reveal with delay
</div>
Customization Knobs
| Knob | How to adjust |
|---|---|
| Trigger offset | Change rootMargin: "0px 0px -10% 0px" |
| Repeat | Set once = false for replay on re-enter |
| Motion style | Modify translateY, blur, or add scale in keyframes |
| Timing | Change duration/delay in [animation:name_duration_easing_delay] |
Common Pitfalls
- Forgetting to include keyframes before the JS snippet
- Keyframe name mismatch between
@keyframesand Tailwind[animation:...] - Elements already in view before observer init (lower
threshold)
Resources
See references/ for detailed guides:
references/tailwind-fundamentals.md- Basic utilities, layout, spacing, typographyreferences/cn-utility-dynamic-styling.md- cn() utility, conditional classes, dynamic valuesreferences/responsive-dark-mode.md- Responsive design, dark mode, container queries
Code examples in assets/:
assets/tailwind-complete-example.tsx- Complete working example with header, hero, features, footer, responsive design, dark mode
External Resources
- Main Docs: https://tailwindcss.com/docs
- v4 Upgrade Guide: https://tailwindcss.com/docs/upgrade-guide
- CSS Variables: https://tailwindcss.com/docs/theme#css-variables
04 同仓库更多 Skills
zustand-5
Zustand 5 state management patterns. Trigger: When managing React state with Zustand.
zod-4
Zod 4 schema validation patterns. Trigger: When using Zod for validation - breaking changes from v3.
x-bookmark-quote-posts
Workflow para revisar bookmarks de X/Twitter y convertirlos en drafts de quote posts. Accede a la sección de bookmarks vía browser headless, extrae posts recientes, y genera drafts con voz personal del usuario. No publica ni muta X — solo genera contenido para revisión. Trigger: X bookmarks, Twitter bookmarks, quote posts, bookmark automation, crear quote posts, revisar bookmarks, growth content X.
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
writing-plans
Use when you have a spec or requirements for a multi-step task, before touching code
work-unit-commits
Plan commits as reviewable work units. Trigger: implementation, commit splitting, chained PRs, or keeping tests and docs with code.