AgentSkillsCN

Tailwind-CSS-design

当需要实现高级 Tailwind CSS 布局、优化 Lighthouse 分数、减少样式覆盖、保持设计令牌的一致性,或在无障碍功能方面下功夫——通过巧妙运用隐藏实用工具来应对布局细节、提升可访问性、精进排版、优化表单交互并提升性能时,可灵活运用此工具

SKILL.md
--- frontmatter
name: Tailwind-CSS-design
description: Use when implementing advanced Tailwind CSS layouts, optimizing Lighthouse scores, reducing overrides, maintaining design token consistency, or working with accessibility features - strategic use of hidden utilities for layout quirks, A11y enhancements, typography refinements, form interactions, and performance optimizations

Tailwind CSS Design

Overview

Strategic use of hidden Tailwind utilities that improve Lighthouse scores, reduce overrides, and maintain consistent design tokens. Mastery of advanced layout techniques, accessibility enhancements, and performance optimizations that replace complex JavaScript logic.

Core principle: Use utility classes strategically to solve layout problems, improve accessibility, and optimize performance without custom CSS overrides.

When to Use

Use when:

  • Implementing complex layouts (multi-column, grid with safe alignment)
  • Optimizing Lighthouse accessibility and performance scores
  • Building accessible forms and interactions
  • Implementing responsive design with container queries
  • Working with dark mode, high contrast, or motion preferences
  • Creating typography that adapts to content (text-balance, hyphens)
  • Preventing layout quirks (clipping, stacking context issues)
  • Styling based on parent/ancestor state without JavaScript

Don't use when:

  • Simple layouts that standard utilities handle well
  • One-off custom styles that don't benefit from utility patterns
  • Overriding Tailwind's design tokens unnecessarily

Core Categories

1. Layout, Struttura e Allineamento Avanzato

Advanced layout techniques for complex flows and layout quirks.

Gestione Semantica (Display)

Remove a parent wrapper from layout flow (Flex/Grid) while maintaining semantics for screen readers.

Utility: contents

Example:

tsx
// Wrapper maintains semantic structure but doesn't affect layout
<div className="contents">
  <nav>Navigation</nav>
  <main>Content</main>
</div>

Contesto di Stacking

Create isolated stacking context (isolation: isolate) to prevent clipping or unexpected overlap of positioned elements.

Utility: isolate

Allineamento Grid Shorthand

Use shorthand to center/align content on both horizontal and vertical axes in CSS Grid, avoiding combining justify-* and items-*.

Utility: place-content-*

Example:

tsx
<div className="grid place-content-center">
  {/* Centered on both axes */}
</div>

Prevenzione del Clipping

Use "safe" alignment to center content until overflow risk, then fall back to initial alignment, ensuring elements (e.g., nav pills) remain visible.

Utility: justify-center-safe / items-center-safe

Layout Multi-Colonna

Implement magazine-style layouts for long content without extra containers or grid hacks.

Utility: columns-*

Example:

tsx
<div className="columns-2 md:columns-3 gap-4">
  {/* Content flows across columns automatically */}
</div>

Prevenzione della Rottura

Prevent internal elements (tabs, callouts) from breaking when using multi-column layouts or generating PDFs.

Utility: break-inside-avoid-column

Example:

tsx
<div className="columns-2">
  <article className="break-inside-avoid-column">
    {/* Won't split across columns */}
  </article>
</div>

2. Accessibilità e Interazioni (A11y/UX)

Utilities that improve Lighthouse scores and optimize UX, especially in SPAs or dark mode.

Offset per Header Fissi

Insert invisible offset to prevent navigation anchors (headings) from being hidden by sticky headers.

Utility: scroll-pt-* and scroll-m-*

Example:

tsx
<h2 id="section" className="scroll-mt-20">
  {/* Offset accounts for fixed header */}
</h2>

Controllo del Movimento

Apply animations and transitions only to users who haven't opted for reduced motion (preventing vestibular discomfort).

Utility: motion-safe: / motion-reduce:

Example:

tsx
<div className="motion-safe:animate-spin motion-reduce:animate-none">
  {/* Respects user preferences */}
</div>

Styling ARIA Dichiarativo

Style elements based on ARIA attribute values (aria-expanded, aria-checked) without additional JavaScript.

Utility: aria-* variants

Example:

tsx
<button aria-expanded="false" className="aria-expanded:rotate-180">
  {/* Styles change based on ARIA state */}
</button>

Controllo del Colore Schema

Suggest browser color scheme (light/dark) for native form controls and scrollbars.

Utility: scheme-light, scheme-dark

Supporto Modalità Alto Contrasto

Prevent OS overrides (e.g., High Contrast) from ruining visual fidelity of sensitive elements (icons, gradients).

Utility: forced-color-adjust-none

Inversione Colori

Optimize shadows and gradients specifically for OS-level color inversion modes (inverted-colors: media query).

Utility: inverted-colors:

Gestione delle Interazioni

Disable pointer interactions (pointer-events-none) on elements like overlays or buttons in "loading" state.

Utility: pointer-events-*

Helper per Screen Reader

Hide content visually while keeping it accessible, and reveal it at specific breakpoints.

Utility: sr-only and not-sr-only

Example:

tsx
<span className="sr-only md:not-sr-only">
  Label visible to screen readers, then visible on larger screens
</span>

3. Tipografia e Rifiniture Visive

Helpers that optimize readability and text aesthetics.

Bilanciamento del Titolo

Use text wrap algorithms to keep multi-line titles visually balanced.

Utility: text-balance

Example:

tsx
<h1 className="text-balance">
  {/* Text wraps more evenly across lines */}
</h1>

Prevenzione di Vedove/Orfane

Avoid isolated words or short lines at end of paragraphs to improve readability.

Utility: text-pretty

Stile Evidenziazione Inline

Control how borders and backgrounds behave when inline elements (highlights) wrap (slice vs clone).

Utility: box-decoration-slice / box-decoration-clone

Ipnosi Intelligente

Enable automatic hyphenation based on language metadata to improve readability and prevent ragged borders in narrow columns.

Utility: hyphens-auto

Ombre Colorate e Depth

Implement colored drop-shadow effects (e.g., neon effects) and internal shadows (inset-shadow-*) for advanced depth and neumorphic effects.

Utility: drop-shadow-* (colored), inset-shadow-*

4. Form e Interazione Utente Avanzata

Utilities for more accessible, consistent, and dynamic forms.

Personalizzazione Controlli

Style radio, checkbox, and range sliders consistently with brand while respecting contrast preferences.

Utility: accent-*

Example:

tsx
<input type="checkbox" className="accent-blue-600" />
<input type="radio" className="accent-purple-500" />

Colore del Cursore

Control text cursor color in input and textarea (e.g., caret-indigo-500).

Utility: caret-*

Example:

tsx
<input className="caret-blue-600" />

Input Auto-Crescenti

Use CSS field-sizing property to allow input or textarea to expand to fit content, eliminating need for autoresize scripts.

Utility: field-sizing-content

Example:

tsx
<textarea className="field-sizing-content">
  {/* Automatically grows with content */}
</textarea>

Validazione Ritardata

Apply validation styling (user-invalid: / user-valid:) only after user interaction, preventing "error on page load" experience.

Utility: user-invalid: / user-valid:

Example:

tsx
<input 
  className="user-invalid:border-red-500 user-valid:border-green-500"
  required 
/>

Controllo Gestures

Use touch-action utilities to refine mobile interactions, e.g., preventing horizontal scroll on carousels.

Utility: touch-pan-y, touch-none

5. Logica Dichiarativa e Performance (Tailwind v4 Features)

Mastery of latest and most powerful Tailwind features, often replacing complex JS logic.

Componenti Self-Responsive

Use container queries so components (e.g., cards) respond to their container's size rather than viewport.

Utility: @container variants

Example:

tsx
<div className="@container">
  <div className="@lg:grid-cols-3">
    {/* Responsive to container, not viewport */}
  </div>
</div>

Progressive Enhancement

Apply utilities only if browser supports specific CSS features (e.g., display: grid or backdrop-filter).

Utility: supports-[...] arbitrary variants

Example:

tsx
<div className="supports-[display:grid]:grid supports-[backdrop-filter]:backdrop-blur-sm">
  {/* Only applies if supported */}
</div>

Animazioni di Ingresso Zero-JS

Define starting style for elements entering DOM, creating fade-in or slide animations without JavaScript.

Utility: starting: variant

Example:

tsx
<div className="opacity-0 starting:opacity-100 starting:transition-opacity">
  {/* Animates in on mount */}
</div>

Reazione allo Stato Ancestor

Style element based on ancestor state (hover, focus) without adding group class to parent.

Utility: in-hover: / in-focus: variants

Example:

tsx
<button>
  <span className="in-hover:scale-110">
    {/* Responds to parent hover without group class */}
  </span>
</button>

Styling Parent-Based

Style parent (group-has) when specific descendant changes state or attribute (e.g., highlight menu label when sub-element is active).

Utility: group-has-[...] variants

Example:

tsx
<div className="group-has-[.active]:bg-blue-100">
  <a className="active">Active link</a>
  {/* Parent styles when child has .active */}
</div>

Quick Reference

CategoryUtilityUse Case
LayoutcontentsRemove wrapper from layout flow
LayoutisolateCreate stacking context
Layoutplace-content-*Center grid on both axes
Layoutjustify-center-safeSafe alignment (prevents clipping)
Layoutcolumns-*Multi-column layout
Layoutbreak-inside-avoid-columnPrevent column breaks
A11yscroll-pt-* / scroll-m-*Offset for fixed headers
A11ymotion-safe: / motion-reduce:Respect motion preferences
A11yaria-* variantsStyle by ARIA state
A11ysr-only / not-sr-onlyScreen reader helpers
A11yforced-color-adjust-nonePreserve design in high contrast
Typographytext-balanceBalance multi-line titles
Typographytext-prettyPrevent widows/orphans
Typographyhyphens-autoAutomatic hyphenation
Formaccent-*Custom form control colors
Formcaret-*Custom cursor color
Formfield-sizing-contentAuto-growing inputs
Formuser-invalid: / user-valid:Delayed validation styling
Formtouch-*Mobile gesture control
v4@containerContainer queries
v4supports-[...]Progressive enhancement
v4starting:Zero-JS entry animations
v4in-hover: / in-focus:Ancestor state styling
v4group-has-[...]Parent styling by descendant

Common Mistakes

MistakeFix
Combining justify-* and items-* for grid centeringUse place-content-center instead
Custom CSS for fixed header offsetUse scroll-pt-* or scroll-m-*
JavaScript for ARIA-based stylingUse aria-* variants
Ignoring motion preferencesWrap animations in motion-safe:
Manual autoresize for textareaUse field-sizing-content
JavaScript for container-responsive designUse @container queries
Complex JS for parent-child state stylingUse group-has-[...] or in-* variants
Custom CSS overrides for design tokensUse Tailwind's utility classes

Implementation Guidelines

Before Using Advanced Utilities

  1. Check browser support - Some utilities require modern browsers (e.g., @container, field-sizing)
  2. Test accessibility - Verify screen reader behavior, motion preferences, high contrast mode
  3. Measure performance - Use Lighthouse to verify improvements (especially A11y score)
  4. Avoid overrides - If you're writing custom CSS, check if a Tailwind utility exists first

Progressive Enhancement Pattern

tsx
// Base styles for all browsers
<div className="grid grid-cols-1">
  {/* Progressive enhancement for supporting browsers */}
  <div className="@container @lg:grid-cols-3">
    {/* Container query layout */}
  </div>
  {/* Fallback for non-supporting browsers */}
  <div className="md:grid-cols-3">
    {/* Viewport-based fallback */}
  </div>
</div>

Real-World Impact

Benefits:

  • Lighthouse scores: +10-15 points on A11y and Performance by using semantic utilities and avoiding JS
  • Bundle size: Reduce JavaScript by replacing autoresize, state management, and animation logic
  • Maintainability: Fewer custom CSS overrides = easier design token consistency
  • Accessibility: Better screen reader support, motion preference respect, high contrast compatibility
  • Performance: Container queries and progressive enhancement improve rendering efficiency

Typical use cases:

  • Fixed navigation with smooth scrolling to anchors
  • Forms with proper validation states and accessibility
  • Responsive cards that adapt to container size
  • Typography that reads beautifully across devices
  • Dark mode and high contrast mode compatibility