颜色系统基础

1. 默认调色板

Tailwind CSS 提供了一套精心设计的默认颜色系统:

  1. // tailwind.config.js 默认颜色示例
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. // 灰度
  6. gray: {
  7. 50: '#f9fafb',
  8. 100: '#f3f4f6',
  9. 200: '#e5e7eb',
  10. // ... 更多色阶
  11. 900: '#111827',
  12. },
  13. // 主题色
  14. blue: {
  15. 50: '#eff6ff',
  16. 100: '#dbeafe',
  17. 500: '#3b82f6',
  18. // ... 更多色阶
  19. 900: '#1e3a8a',
  20. },
  21. // 其他颜色...
  22. }
  23. }
  24. }

2. 颜色命名规范

  • 基础色名:grayredblue
  • 色阶值:50-900,数值越大越深
  • 特殊颜色:whiteblacktransparentcurrent

3. 颜色使用方式

  1. <!-- 基础颜色使用 -->
  2. <div class="bg-blue-500 text-white">
  3. 背景蓝色,文字白色
  4. </div>
  5. <!-- 透明度使用 -->
  6. <div class="bg-blue-500/75">
  7. 带透明度的蓝色背景
  8. </div>
  9. <!-- 悬停状态 -->
  10. <button class="bg-blue-500 hover:bg-blue-600">
  11. 悬停变色按钮
  12. </button>

主题定制

1. 扩展默认主题

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. colors: {
  6. 'brand': {
  7. light: '#60A5FA',
  8. DEFAULT: '#3B82F6',
  9. dark: '#1D4ED8',
  10. },
  11. 'accent': '#F59E0B',
  12. }
  13. }
  14. }
  15. }

2. 完全自定义主题

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. colors: {
  5. // 完全自定义的颜色系统
  6. primary: {
  7. light: '#85d7ff',
  8. DEFAULT: '#1fb6ff',
  9. dark: '#009eeb',
  10. },
  11. secondary: {
  12. light: '#ff7ce5',
  13. DEFAULT: '#ff49db',
  14. dark: '#ff16d1',
  15. }
  16. }
  17. }
  18. }

深色模式实现

1. 基础配置

  1. // tailwind.config.js
  2. module.exports = {
  3. darkMode: 'class', // 或 'media'
  4. }

2. 深色模式使用

  1. <!-- 深色模式适配 -->
  2. <div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100">
  3. <h1 class="text-2xl font-bold">
  4. 自适应深色模式的标题
  5. </h1>
  6. <p class="text-gray-600 dark:text-gray-300">
  7. 自适应深色模式的段落
  8. </p>
  9. </div>

3. 切换实现

  1. // 切换深色模式
  2. function toggleDarkMode() {
  3. document.documentElement.classList.toggle('dark');
  4. }

主题切换系统

1. 动态主题配置

  1. // themes.js
  2. export const themes = {
  3. light: {
  4. primary: '#3B82F6',
  5. secondary: '#10B981',
  6. background: '#FFFFFF',
  7. text: '#111827',
  8. },
  9. dark: {
  10. primary: '#60A5FA',
  11. secondary: '#34D399',
  12. background: '#111827',
  13. text: '#F9FAFB',
  14. },
  15. }

2. CSS 变量实现

  1. /* 根变量定义 */
  2. :root {
  3. --color-primary: theme('colors.blue.500');
  4. --color-secondary: theme('colors.green.500');
  5. }
  6. .dark {
  7. --color-primary: theme('colors.blue.400');
  8. --color-secondary: theme('colors.green.400');
  9. }

3. 组件应用

  1. <!-- 使用 CSS 变量的组件 -->
  2. <button class="bg-[var(--color-primary)] text-white hover:bg-[var(--color-primary-dark)]">
  3. 动态主题按钮
  4. </button>

颜色系统最佳实践

1. 语义化颜色使用

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. colors: {
  6. // 语义化命名
  7. 'success': '#10B981',
  8. 'warning': '#F59E0B',
  9. 'error': '#EF4444',
  10. 'info': '#3B82F6',
  11. }
  12. }
  13. }
  14. }

2. 渐变色使用

  1. <!-- 渐变色示例 -->
  2. <div class="bg-gradient-to-r from-blue-500 to-purple-500">
  3. 渐变背景
  4. </div>
  5. <div class="bg-gradient-to-tr from-pink-500 via-red-500 to-yellow-500">
  6. 三色渐变
  7. </div>

3. 色彩无障碍

  1. <!-- 无障碍色彩对比 -->
  2. <button class="bg-blue-600 text-white">
  3. <!-- WCAG 2.0 AA 标准合规的按钮 -->
  4. 高对比度按钮
  5. </button>

实战应用示例

1. 品牌色系统

  1. <!-- 品牌色应用 -->
  2. <div class="space-y-4">
  3. <!-- 主要按钮 -->
  4. <button class="bg-brand-DEFAULT hover:bg-brand-dark text-white px-4 py-2 rounded">
  5. 主要按钮
  6. </button>
  7. <!-- 次要按钮 -->
  8. <button class="bg-brand-light hover:bg-brand-DEFAULT text-brand-dark px-4 py-2 rounded">
  9. 次要按钮
  10. </button>
  11. </div>

2. 状态颜色

  1. <!-- 状态颜色应用 -->
  2. <div class="space-y-2">
  3. <div class="bg-success/10 text-success p-2 rounded">
  4. 成功提示消息
  5. </div>
  6. <div class="bg-warning/10 text-warning p-2 rounded">
  7. 警告提示消息
  8. </div>
  9. <div class="bg-error/10 text-error p-2 rounded">
  10. 错误提示消息
  11. </div>
  12. </div>

进阶技巧

1. 动态颜色生成

  1. // 使用 CSS 变量生成动态颜色
  2. function generateColors(hue) {
  3. return {
  4. light: `hsl(${hue}, 70%, 80%)`,
  5. DEFAULT: `hsl(${hue}, 70%, 50%)`,
  6. dark: `hsl(${hue}, 70%, 30%)`,
  7. }
  8. }

2. 主题预设

  1. // presets.js
  2. const brandPreset = {
  3. theme: {
  4. colors: {
  5. brand: {
  6. // 品牌色预设
  7. }
  8. }
  9. }
  10. }

总结

Tailwind CSS 的颜色系统和主题定制提供了:

  1. 完整的默认调色板
  2. 灵活的自定义能力
  3. 深色模式支持
  4. 主题切换机制

通过合理运用这些特性,我们可以:

  1. 建立统一的品牌形象
  2. 提供良好的用户体验
  3. 实现灵活的主题切换
  4. 确保无障碍访问

在实际开发中,应该根据项目需求合理规划颜色系统,并建立清晰的使用规范。