mcpField
T
tailwind-patterns

tailwind-patterns

github.com/jezweb设计与 UI1.1k1.1k 次安装3 周前更新
代码仓库

关于 tailwind-patterns

tailwind-patterns 是面向 Tailwind CSS 项目的组件与界面模式指南,适用于构建响应式页面的开发者。内容涵盖布局、表单、导航和主题,并提供可复制的模板组件与参考文档。

主要功能

  1. 01容器、网格与卡片模式
  2. 02按钮、表单与导航模式
  3. 03移动优先断点与间距规范
  4. 04语义颜色令牌与深色模式
  5. 05可复制的模板组件

适用场景

  1. 01搭建响应式页面布局
  2. 02统一项目中的间距与颜色
  3. 03添加常见表单和导航界面

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/jezweb/claude-skills --skill tailwind-patterns

安装后在 Claude Code 中输入/tailwind-patterns确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

README.md
.claude-plugin/
plugin.json
references/
button-patterns.md
card-patterns.md
dark-mode-patterns.md
form-patterns.md
layout-patterns.md
navigation-patterns.md
typography-patterns.md
rules/
tailwind-patterns.md
templates/components/
contact-form.tsx
feature-grid.tsx
footer.tsx
hero-section.tsx

详细介绍

Tailwind CSS 组件模式

状态:已可用于生产 ✅ 最后更新:2026-01-14 Tailwind 兼容性:v3.x 和 v4.x 来源:生产项目、shadcn/ui 模式


快速入门

基本模式
// Section Container
<section className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-16 sm:py-24">
  {/* content */}
</section>

// Card Base
<div className="bg-card text-card-foreground rounded-lg border border-border p-6">
  {/* content */}
</div>

// Button Primary
<button className="bg-primary text-primary-foreground px-4 py-2 rounded-md hover:bg-primary/90 transition-colors">
  Click me
</button>

// Responsive Grid
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
  {/* items */}
</div>

间距比例

一致的间距可以避免设计风格偏移:

用途类输出
紧凑间距gap-2 p-2 space-y-20.5rem(8px)
标准间距gap-4 p-4 space-y-41rem(16px)
舒适间距gap-6 p-6 space-y-61.5rem(24px)
宽松间距gap-8 p-8 space-y-82rem(32px)
区块间距py-16 sm:py-244rem/6rem(64px/96px)

标准模式:使用 4 的倍数(4、6、8、12、16、24)


响应式断点

移动优先方法(基础样式 = 移动端,添加更大的断点):

断点最小宽度模式示例
基础0px无前缀text-base
sm640pxsm:sm:text-lg
md768pxmd:md:grid-cols-2
lg1024pxlg:lg:px-8
xl1280pxxl:xl:max-w-7xl
2xl1536px2xl:2xl:text-6xl
// Mobile: 1 column, Tablet: 2 columns, Desktop: 3 columns
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">

容器模式

标准页面容器
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
  {/* content */}
</div>

变体:

  • max-w-4xl - 窄内容(博客文章)
  • max-w-5xl - 中等内容
  • max-w-6xl - 宽内容
  • max-w-7xl - 全宽(默认)
区块间距
<section className="py-16 sm:py-24">
  <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
    {/* content */}
  </div>
</section>

卡片模式

基本卡片
<div className="bg-card text-card-foreground rounded-lg border border-border p-6">
  <h3 className="text-lg font-semibold mb-2">Card Title</h3>
  <p className="text-muted-foreground">Card description goes here.</p>
</div>
带图标的功能卡片
<div className="bg-card text-card-foreground rounded-lg border border-border p-6 hover:shadow-lg transition-shadow">
  <div className="h-12 w-12 rounded-lg bg-primary/10 flex items-center justify-center mb-4">
    {/* Icon */}
  </div>
  <h3 className="text-lg font-semibold mb-2">Feature Title</h3>
  <p className="text-muted-foreground">Feature description.</p>
</div>
定价卡片
<div className="bg-card text-card-foreground rounded-lg border-2 border-border p-8 relative">
  <div className="text-sm font-semibold text-primary mb-2">Pro Plan</div>
  <div className="text-4xl font-bold mb-1">$29<span className="text-lg text-muted-foreground">/mo</span></div>
  <p className="text-muted-foreground mb-6">For growing teams</p>
  <button className="w-full bg-primary text-primary-foreground py-2 rounded-md hover:bg-primary/90">
    Get Started
  </button>
</div>

如需更多变体,请参阅 references/card-patterns.md。


网格布局

自动响应式网格
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
  {items.map(item => <Card key={item.id} {...item} />)}
</div>
自动适配网格(动态列)
<div className="grid grid-cols-[repeat(auto-fit,minmax(280px,1fr))] gap-6">
  {/* Automatically adjusts columns based on available space */}
</div>
瀑布流样式网格
<div className="columns-1 md:columns-2 lg:columns-3 gap-6 space-y-6">
  {items.map(item => (
    <div key={item.id} className="break-inside-avoid">
      <Card {...item} />
    </div>
  ))}
</div>

按钮模式

// Primary
<button className="bg-primary text-primary-foreground px-4 py-2 rounded-md hover:bg-primary/90 transition-colors">
  Primary
</button>

// Secondary
<button className="bg-secondary text-secondary-foreground px-4 py-2 rounded-md hover:bg-secondary/80">
  Secondary
</button>

// Outline
<button className="border border-border bg-transparent px-4 py-2 rounded-md hover:bg-accent">
  Outline
</button>

// Ghost
<button className="bg-transparent px-4 py-2 rounded-md hover:bg-accent hover:text-accent-foreground">
  Ghost
</button>

// Destructive
<button className="bg-destructive text-destructive-foreground px-4 py-2 rounded-md hover:bg-destructive/90">
  Delete
</button>

尺寸变体:

  • 小:px-3 py-1.5 text-sm
  • 默认:px-4 py-2
  • 大:px-6 py-3 text-lg

完整参考请参阅 references/button-patterns.md。


表单模式

输入框
<div className="space-y-2">
  <label htmlFor="email" className="text-sm font-medium">
    Email
  </label>
  <input
    id="email"
    type="email"
    className="w-full px-3 py-2 bg-background border border-border rounded-md focus:outline-none focus:ring-2 focus:ring-primary"
    placeholder="[email protected]"
  />
</div>
下拉选择框
<select className="w-full px-3 py-2 bg-background border border-border rounded-md focus:outline-none focus:ring-2 focus:ring-primary">
  <option>Option 1</option>
  <option>Option 2</option>
</select>
复选框
<div className="flex items-center space-x-2">
  <input
    id="terms"
    type="checkbox"
    className="h-4 w-4 rounded border-border text-primary focus:ring-2 focus:ring-primary"
  />
  <label htmlFor="terms" className="text-sm">
    I agree to the terms
  </label>
</div>
错误状态
<div className="space-y-2">
  <label htmlFor="password" className="text-sm font-medium">
    Password
  </label>
  <input
    id="password"
    type="password"
    className="w-full px-3 py-2 bg-background border border-destructive rounded-md focus:outline-none focus:ring-2 focus:ring-destructive"
  />
  <p className="text-sm text-destructive">Password must be at least 8 characters</p>
</div>

完整模式请参阅 references/form-patterns.md。


排版模式

标题
<h1 className="text-4xl sm:text-5xl lg:text-6xl font-bold">
  Page Title
</h1>

<h2 className="text-3xl sm:text-4xl font-bold">
  Section Title
</h2>

<h3 className="text-2xl sm:text-3xl font-semibold">
  Subsection
</h3>

<h4 className="text-xl font-semibold">
  Card Title
</h4>
正文
<p className="text-base text-muted-foreground">
  Regular paragraph text.
</p>

<p className="text-lg text-muted-foreground leading-relaxed">
  Larger body text with comfortable line height.
</p>

<p className="text-sm text-muted-foreground">
  Small supporting text or captions.
</p>
列表
<ul className="space-y-2 text-muted-foreground">
  <li className="flex items-start">
    <CheckIcon className="h-5 w-5 text-primary mr-2 mt-0.5" />
    <span>Feature one</span>
  </li>
  <li className="flex items-start">
    <CheckIcon className="h-5 w-5 text-primary mr-2 mt-0.5" />
    <span>Feature two</span>
  </li>
</ul>

完整指南请参阅 references/typography-patterns.md。


导航模式

带 Logo 的页眉
<header className="sticky top-0 z-50 w-full border-b border-border bg-background/95 backdrop-blur">
  <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
    <div className="flex h-16 items-center justify-between">
      <div className="flex items-center gap-8">
        {/* Logo */}
        <a href="/" className="font-bold text-xl">Brand</a>

        {/* Desktop Nav */}
        <nav className="hidden md:flex gap-6">
          <a href="#" className="text-sm hover:text-primary transition-colors">Features</a>
          <a href="#" className="text-sm hover:text-primary transition-colors">Pricing</a>
          <a href="#" className="text-sm hover:text-primary transition-colors">About</a>
        </nav>
      </div>

      {/* CTA */}
      <button className="bg-primary text-primary-foreground px-4 py-2 rounded-md text-sm">
        Sign Up
      </button>
    </div>
  </div>
</header>
页脚
<footer className="border-t border-border bg-muted/50">
  <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
    <div className="grid grid-cols-2 md:grid-cols-4 gap-8">
      <div>
        <h4 className="font-semibold mb-4">Product</h4>
        <ul className="space-y-2 text-sm text-muted-foreground">
          <li><a href="#" className="hover:text-primary">Features</a></li>
          <li><a href="#" className="hover:text-primary">Pricing</a></li>
        </ul>
      </div>
      {/* More columns */}
    </div>
  </div>
</footer>

如需移动端菜单和下拉菜单,请参阅 references/navigation-patterns.md。


深色模式支持

所有模式都使用语义颜色令牌,可自动适配:

令牌浅色模式深色模式
bg-background白色深灰色
text-foreground深灰色白色
bg-card白色稍浅的灰色
text-muted-foreground灰色浅灰色
border-border浅灰色深灰色
bg-primary品牌色更浅的品牌色

切勿使用原始颜色,例如 bg-blue-500;请始终使用语义令牌。

主题切换实现请参阅 references/dark-mode-patterns.md。


常见类组合

带标题的区块
<section className="py-16 sm:py-24">
  <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
    <h2 className="text-3xl sm:text-4xl font-bold text-center mb-12">
      Section Title
    </h2>
    <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
      {/* content */}
    </div>
  </div>
</section>
居中内容
<div className="flex flex-col items-center justify-center text-center">
  <h1 className="text-4xl font-bold mb-4">Centered Title</h1>
  <p className="text-muted-foreground max-w-2xl">Centered description</p>
</div>
悬停效果
// Lift on hover
<div className="transition-transform hover:scale-105">

// Shadow on hover
<div className="transition-shadow hover:shadow-lg">

// Color change on hover
<button className="transition-colors hover:bg-primary/90">

关键规则

✅ 务必做到
  1. 使用语义颜色令牌(bg-card、text-foreground)
  2. 采用移动优先的响应式设计(base → sm: → md:)
  3. 使用一致的间距比例(4、6、8、12、16、24)
  4. 添加 transition-* 类,实现平滑交互
  5. 在浅色和深色模式下都进行测试
❌ 切勿这样做
  1. 使用 Tailwind 原始颜色(bg-blue-500 会破坏主题)
  2. 省略响应式前缀(影响移动端用户体验)
  3. 随意混用间距比例(造成视觉混乱)
  4. 忘记为交互元素添加悬停状态
  5. 为文本使用固定的 px 值(用 text-base,而不是 text-[16px])

模板组件

templates/components/ 中可直接使用的组件:

  • hero-section.tsx - 带 CTA 的响应式主视觉区
  • feature-grid.tsx - 带图标的三列功能网格
  • contact-form.tsx - 带验证样式的完整表单
  • footer.tsx - 带链接的多列页脚

复制并根据你的项目进行自定义。


参考文档

references/ 目录中的详细模式:

  • layout-patterns.md - 容器、网格、Flexbox 布局
  • card-patterns.md - 所有卡片变体和状态
  • navigation-patterns.md - 页眉、菜单、面包屑、页脚
  • form-patterns.md - 完整的表单样式指南
  • button-patterns.md - 所有按钮变体和尺寸
  • typography-patterns.md - 文本样式和层级
  • dark-mode-patterns.md - 主题切换实现

官方文档


最后更新:2026-01-14 技能版本:1.0.0 生产环境:已在 10+ 个项目中测试

评论

支持 Markdown