mcpField
T
tailwindcss

tailwindcss

github.com/blencorp设计与 UI111174 次安装48 周前更新
代码仓库

关于 tailwindcss

面向构建响应式界面的开发者,提供 Tailwind CSS v4 工具类开发指南,涵盖 CSS 优先配置、常见组件模式以及样式和生产构建实践。

主要功能

  1. 01工具类优先的样式指南
  2. 02移动优先的响应式设计
  3. 03CSS 优先的主题配置
  4. 04可复用组件模式
  5. 05深色模式与插件指南
  6. 06生产构建注意事项

适用场景

  1. 01设计响应式页面布局
  2. 02定义项目主题颜色
  3. 03创建可复用的按钮和卡片样式
  4. 04规划深色模式样式

安装

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

$npx skills add https://github.com/blencorp/claude-code-kit --skill tailwindcss

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

包含的文件

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

skill-rules-fragment.json
resources/
component-library.md
configuration.md
utility-patterns.md

详细介绍

Tailwind CSS v4 开发指南

有效使用 Tailwind CSS v4 工具类的最佳实践。

注意:Tailwind CSS v4(于 2025 年 1 月发布)采用 CSS 优先的配置方式。如果需要兼容 v3,仍支持 tailwind.config.js。

核心原则

  1. 工具类优先:使用工具类,而不是自定义 CSS
  2. 移动优先:先针对移动设备设计,再使用响应式修饰符逐步扩展
  3. 提取组件:将重复的模式提取为组件
  4. 间距一致:使用 Tailwind 的间距刻度
  5. 自定义配置:扩展默认主题,以保持品牌一致性

基础工具类

布局
// Flexbox
<div className="flex items-center justify-between gap-4">
  <div className="flex-1">Content</div>
  <div className="flex-shrink-0">Sidebar</div>
</div>

// Grid
<div className="grid grid-cols-3 gap-4">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

// Positioning
<div className="relative">
  <div className="absolute top-0 right-0">Badge</div>
</div>
间距
// Padding and Margin
<div className="p-4 m-2">           {/* padding: 1rem, margin: 0.5rem */}
<div className="px-6 py-4">        {/* padding-x: 1.5rem, padding-y: 1rem */}
<div className="mt-8 mb-4">        {/* margin-top: 2rem, margin-bottom: 1rem */}

// Space between children
<div className="space-y-4">        {/* margin-bottom on all but last child */}
  <div>Item 1</div>
  <div>Item 2</div>
</div>
排版
<h1 className="text-4xl font-bold text-gray-900">Heading</h1>
<p className="text-base font-normal text-gray-600 leading-relaxed">
  Paragraph text with comfortable line height.
</p>
<span className="text-sm font-medium text-blue-600">Label</span>
颜色
// Text colors
<p className="text-gray-900 dark:text-gray-100">Text</p>

// Background colors
<div className="bg-blue-500 hover:bg-blue-600">Button</div>

// Border colors
<div className="border border-gray-300">Box</div>

响应式设计

断点
// Mobile-first responsive classes
<div className="w-full md:w-1/2 lg:w-1/3">
  {/* Full width on mobile, half on medium screens, third on large */}
</div>

<h1 className="text-2xl md:text-4xl lg:text-6xl">
  {/* Responsive text sizes */}
</h1>

<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
  {/* Responsive grid */}
</div>
容器
<div className="container mx-auto px-4">
  {/* Centered container with horizontal padding */}
</div>

<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
  {/* Responsive container padding */}
</div>

组件模式

按钮
<button className="px-4 py-2 bg-blue-600 text-white font-medium rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
  Click me
</button>

// Variants
<button className="px-4 py-2 border border-gray-300 rounded-md hover:bg-gray-50">
  Secondary
</button>
卡片
<div className="bg-white rounded-lg shadow-md overflow-hidden">
  <img src="/image.jpg" alt="" className="w-full h-48 object-cover" />
  <div className="p-6">
    <h2 className="text-xl font-semibold mb-2">Card Title</h2>
    <p className="text-gray-600">Card content goes here.</p>
  </div>
</div>
表单输入框
<div className="space-y-2">
  <label htmlFor="email" className="block text-sm font-medium text-gray-700">
    Email
  </label>
  <input
    type="email"
    id="email"
    className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
    placeholder="[email protected]"
  />
  <p className="text-sm text-gray-500">We'll never share your email.</p>
</div>

状态变体

悬停、聚焦、激活
<button className="bg-blue-500 hover:bg-blue-600 active:bg-blue-700 focus:ring-2 focus:ring-blue-500">
  Interactive Button
</button>

<a href="#" className="text-blue-600 hover:text-blue-800 hover:underline">
  Link
</a>
组悬停
<div className="group">
  <img src="/image.jpg" className="group-hover:opacity-75 transition-opacity" />
  <p className="group-hover:text-blue-600">Hover the container</p>
</div>
禁用
<button className="disabled:opacity-50 disabled:cursor-not-allowed" disabled>
  Disabled Button
</button>

深色模式

/* Tailwind v4: Configure in app/globals.css */
@import "tailwindcss";

@media (prefers-color-scheme: dark) {
  /* Or use class-based: .dark */
}
// Usage (same as v3)
<div className="bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100">
  <h1 className="text-gray-900 dark:text-white">Title</h1>
  <p className="text-gray-600 dark:text-gray-400">Description</p>
</div>

自定义样式

任意值
<div className="top-[117px]">           {/* Custom top value */}
<div className="bg-[#1da1f2]">          {/* Custom color */}
<div className="grid-cols-[200px_1fr]"> {/* Custom grid template */}
@apply 指令
/* components/button.css */
.btn-primary {
  @apply px-4 py-2 bg-blue-600 text-white font-medium rounded-md;
  @apply hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500;
  @apply disabled:opacity-50 disabled:cursor-not-allowed;
}

配置

Tailwind v4:CSS 优先配置
/* app/globals.css */
@import "tailwindcss";

@theme {
  /* Custom colors */
  --color-brand-50: #eff6ff;
  --color-brand-100: #dbeafe;
  --color-brand-900: #1e3a8a;

  /* Custom spacing */
  --spacing-128: 32rem;

  /* Custom fonts */
  --font-family-sans: 'Inter', sans-serif;

  /* Custom breakpoints */
  --breakpoint-3xl: 1920px;
}
Tailwind v3 配置(仍受支持)
// tailwind.config.js (optional in v4)
module.exports = {
  content: [
    './app/**/*.{js,ts,jsx,tsx,mdx}',
    './components/**/*.{js,ts,jsx,tsx,mdx}',
  ],
  theme: {
    extend: {
      colors: {
        brand: {
          50: '#eff6ff',
          100: '#dbeafe',
          900: '#1e3a8a',
        }
      },
      spacing: {
        '128': '32rem',
      },
      fontFamily: {
        sans: ['Inter', 'sans-serif'],
      },
    },
  },
  plugins: [
    require('@tailwindcss/forms'),
    require('@tailwindcss/typography'),
  ],
}

插件

官方插件
npm install @tailwindcss/forms
npm install @tailwindcss/typography
npm install @tailwindcss/aspect-ratio
npm install @tailwindcss/container-queries
// @tailwindcss/forms
<input type="text" className="form-input rounded-md" />

// @tailwindcss/typography
<article className="prose lg:prose-xl">
  <h1>Article Title</h1>
  <p>Content...</p>
</article>

性能

自动检测内容

Tailwind v4 会自动检测并扫描所有模板文件,无需配置 content。

构建性能

Tailwind v4 借助 @property 和 color-mix() 等现代 CSS 特性,完整构建速度比 v3 快 3.5 倍(约 100ms)。

浏览器要求:Safari 16.4+、Chrome 111+、Firefox 128+

常见模式

居中内容
<div className="flex items-center justify-center min-h-screen">
  <div>Centered content</div>
</div>
粘性页眉
<header className="sticky top-0 z-50 bg-white border-b">
  <nav>Navigation</nav>
</header>
网格布局
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
  {posts.map(post => (
    <PostCard key={post.id} post={post} />
  ))}
</div>
截断文本
<p className="truncate">This text will be truncated with ellipsis if too long</p>
<p className="line-clamp-3">This text will show max 3 lines with ellipsis</p>

最佳实践

  1. 保持间距一致:遵循 Tailwind 的间距刻度
  2. 默认采用响应式设计:始终考虑移动优先设计
  3. 提取组件:避免重复冗长的类列表
  4. 使用主题颜色:在配置中定义自定义颜色,而不是使用任意值
  5. 谨慎使用 @apply:优先在 JSX 中使用工具类
  6. 启用深色模式:从一开始就规划深色模式
  7. 使用插件:利用官方插件满足常见需求
  8. 优化生产环境:确保正确配置 purge

更多资源

详细信息请参阅:

评论

支持 Markdown