AgentSkillsCN

tailwind-patterns

遵循Tailwind CSS v4的设计原则:以CSS为先的配置方式、容器查询以及现代化的设计模式。

SKILL.md
--- frontmatter
name: tailwind-patterns
description: Tailwind CSS v4 principles. CSS-first configuration, container queries, modern patterns.
allowed-tools: Read, Write, Edit

Tailwind CSS Patterns (v4 - 2025)

Modern utility-first CSS with CSS-native configuration.


1. Tailwind v4 vs v3

v3 (Legacy)v4 (Current)
tailwind.config.jsCSS-based @theme directive
PostCSS pluginOxide engine (10x faster)
JIT modeNative, always-on
Plugin systemCSS-native features

2. Configuration in CSS

Theme Definition

css
@theme {
  /* Colors - semantic names */
  --color-primary: oklch(0.7 0.15 250);
  --color-surface: oklch(0.98 0 0);

  /* Spacing scale */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 2rem;

  /* Typography */
  --font-sans: 'Inter', system-ui, sans-serif;
}

3. Breakpoint System

PrefixMin WidthTarget
(none)0pxMobile-first base
sm:640pxLarge phone
md:768pxTablet
lg:1024pxLaptop
xl:1280pxDesktop
2xl:1536pxLarge desktop

Mobile-First Principle

  1. Write mobile styles first (no prefix)
  2. Add larger screen overrides with prefixes
  3. Example: w-full md:w-1/2 lg:w-1/3

4. Container Queries (v4 Native)

TypeResponds To
Breakpoint (md:)Viewport width
Container (@container)Parent element width

When to Use

ScenarioUse
Page-level layoutsViewport breakpoints
Component-level responsiveContainer queries
Reusable componentsContainer queries

5. Dark Mode

MethodBehaviorUse When
class.dark class togglesManual theme switcher
mediaFollows system preferenceNo user control

Pattern

ElementLightDark
Backgroundbg-whitedark:bg-zinc-900
Texttext-zinc-900dark:text-zinc-100
Bordersborder-zinc-200dark:border-zinc-700

6. Layout Patterns

Flexbox

PatternClasses
Center (both)flex items-center justify-center
Vertical stackflex flex-col gap-4
Horizontal rowflex gap-4
Space betweenflex justify-between items-center

Grid

PatternClasses
Auto-fit responsivegrid grid-cols-[repeat(auto-fit,minmax(250px,1fr))]
Bento/Asymmetricgrid grid-cols-3 grid-rows-2 with spans
Sidebar layoutgrid grid-cols-[auto_1fr]

Prefer Bento/asymmetric layouts over symmetric grids.


7. Modern Color System

OKLCH (Recommended in 2025)

FormatAdvantage
OKLCHPerceptually uniform
HSLIntuitive hue/saturation
RGBLegacy compatibility

Color Token Layers

LayerExamplePurpose
Primitive--blue-500Raw values
Semantic--color-primaryPurpose-based
Component--button-bgComponent-specific

8. Anti-Patterns

Don'tDo
Arbitrary values everywhereUse design system scale
!importantFix specificity properly
Inline style=Use utilities
Duplicate long class listsExtract component
Use @apply heavilyPrefer components

Remember: Tailwind v4 is CSS-first. Embrace CSS variables and native features.