Tailwind CSS 的一大特色是其高度可定制性。通过配置文件,我们可以完全控制框架的行为,创建符合项目需求的样式系统。本节将深入探讨 Tailwind CSS 的配置系统,帮助你掌握自定义配置的各个方面。

配置文件基础

配置文件结构

  1. // tailwind.config.js
  2. module.exports = {
  3. content: [
  4. "./src/**/*.{js,jsx,ts,tsx}",
  5. "./public/index.html"
  6. ],
  7. theme: {
  8. // 主题配置
  9. },
  10. plugins: [
  11. // 插件配置
  12. ],
  13. variants: {
  14. // 变体配置
  15. },
  16. presets: [
  17. // 预设配置
  18. ]
  19. }

配置文件生成

  1. # 生成完整配置文件
  2. npx tailwindcss init --full
  3. # 生成基础配置文件
  4. npx tailwindcss init

主题定制

颜色系统

  1. module.exports = {
  2. theme: {
  3. colors: {
  4. // 完全覆盖默认颜色
  5. primary: {
  6. 50: '#f0f9ff',
  7. 100: '#e0f2fe',
  8. // ...其他色阶
  9. 900: '#0c4a6e'
  10. },
  11. // 使用现有颜色
  12. gray: ({ theme }) => theme('colors.neutral')
  13. },
  14. extend: {
  15. colors: {
  16. // 扩展默认颜色
  17. brand: {
  18. light: '#60A5FA',
  19. DEFAULT: '#3B82F6',
  20. dark: '#2563EB'
  21. }
  22. }
  23. }
  24. }
  25. }

间距和尺寸

  1. module.exports = {
  2. theme: {
  3. spacing: {
  4. px: '1px',
  5. 0: '0',
  6. 0.5: '0.125rem',
  7. // ...自定义间距
  8. },
  9. extend: {
  10. width: {
  11. '1/7': '14.2857143%',
  12. 'screen-1/2': '50vw'
  13. },
  14. height: {
  15. '128': '32rem'
  16. },
  17. maxWidth: {
  18. '8xl': '88rem'
  19. }
  20. }
  21. }
  22. }

断点设置

  1. module.exports = {
  2. theme: {
  3. screens: {
  4. 'sm': '640px',
  5. 'md': '768px',
  6. 'lg': '1024px',
  7. 'xl': '1280px',
  8. '2xl': '1536px',
  9. // 自定义断点
  10. 'tablet': '640px',
  11. 'laptop': '1024px',
  12. 'desktop': '1280px'
  13. }
  14. }
  15. }

变体配置

自定义状态变体

  1. module.exports = {
  2. variants: {
  3. extend: {
  4. backgroundColor: ['active', 'disabled'],
  5. opacity: ['disabled'],
  6. cursor: ['disabled'],
  7. // 为特定功能启用变体
  8. textColor: ['visited'],
  9. borderColor: ['focus-visible']
  10. }
  11. }
  12. }

响应式变体

  1. module.exports = {
  2. variants: {
  3. extend: {
  4. // 启用特定断点的变体
  5. display: ['responsive'],
  6. padding: {
  7. responsive: true,
  8. hover: true
  9. }
  10. }
  11. }
  12. }

插件系统

创建自定义插件

  1. // plugins/button.js
  2. const plugin = require('tailwindcss/plugin')
  3. module.exports = plugin(function({ addComponents, theme }) {
  4. const buttons = {
  5. '.btn': {
  6. padding: `${theme('spacing.2')} ${theme('spacing.4')}`,
  7. borderRadius: theme('borderRadius.lg'),
  8. fontWeight: theme('fontWeight.bold'),
  9. '&:focus': {
  10. outline: 'none',
  11. boxShadow: theme('boxShadow.outline')
  12. }
  13. },
  14. '.btn-primary': {
  15. backgroundColor: theme('colors.blue.600'),
  16. color: theme('colors.white'),
  17. '&:hover': {
  18. backgroundColor: theme('colors.blue.700')
  19. }
  20. }
  21. }
  22. addComponents(buttons)
  23. })

注册插件

  1. // tailwind.config.js
  2. module.exports = {
  3. plugins: [
  4. require('./plugins/button'),
  5. // 带选项的插件
  6. require('@tailwindcss/forms')({
  7. strategy: 'class'
  8. })
  9. ]
  10. }

预处理器集成

PostCSS 配置

  1. // postcss.config.js
  2. module.exports = {
  3. plugins: {
  4. 'postcss-import': {},
  5. 'tailwindcss/nesting': {},
  6. tailwindcss: {},
  7. autoprefixer: {}
  8. }
  9. }

自定义 CSS 变量

  1. module.exports = {
  2. theme: {
  3. extend: {
  4. colors: {
  5. primary: 'var(--color-primary)',
  6. secondary: 'var(--color-secondary)'
  7. }
  8. }
  9. }
  10. }
  1. /* styles/variables.css */
  2. :root {
  3. --color-primary: #3B82F6;
  4. --color-secondary: #10B981;
  5. }
  6. .dark {
  7. --color-primary: #60A5FA;
  8. --color-secondary: #34D399;
  9. }

性能优化

内容配置优化

  1. module.exports = {
  2. content: [
  3. './src/**/*.{js,jsx,ts,tsx}',
  4. // 排除测试文件
  5. '!./src/**/*.test.{js,jsx,ts,tsx}',
  6. // 排除故事书文件
  7. '!./src/**/*.stories.{js,jsx,ts,tsx}'
  8. ]
  9. }

按需加载配置

  1. module.exports = {
  2. // 禁用未使用的核心插件
  3. corePlugins: {
  4. float: false,
  5. objectFit: false,
  6. objectPosition: false
  7. },
  8. // 禁用未使用的变体
  9. variants: {
  10. extend: {
  11. backgroundColor: ['hover', 'focus'],
  12. // 移除不需要的变体
  13. opacity: ['hover']
  14. }
  15. }
  16. }

工程化实践

模块化配置

  1. // config/theme/colors.js
  2. module.exports = {
  3. primary: {
  4. light: '#60A5FA',
  5. DEFAULT: '#3B82F6',
  6. dark: '#2563EB'
  7. }
  8. // ...其他颜色定义
  9. }
  10. // config/theme/typography.js
  11. module.exports = {
  12. fontFamily: {
  13. sans: ['Inter var', 'sans-serif'],
  14. serif: ['Merriweather', 'serif']
  15. }
  16. // ...其他排版相关配置
  17. }
  18. // tailwind.config.js
  19. module.exports = {
  20. theme: {
  21. colors: require('./config/theme/colors'),
  22. typography: require('./config/theme/typography')
  23. }
  24. }

环境配置

  1. // tailwind.config.js
  2. const colors = require('./config/theme/colors')
  3. const typography = require('./config/theme/typography')
  4. const isDevelopment = process.env.NODE_ENV === 'development'
  5. module.exports = {
  6. theme: {
  7. colors: isDevelopment
  8. ? { ...colors, debug: '#ff0000' }
  9. : colors,
  10. typography
  11. },
  12. plugins: [
  13. ...(isDevelopment ? [require('./plugins/debug')] : [])
  14. ]
  15. }

最佳实践

  1. 配置组织

    • 模块化配置文件
    • 使用预设共享配置
    • 环境特定配置
  2. 主题设计

    • 建立设计令牌系统
    • 使用语义化命名
    • 保持一致性
  3. 性能考虑

    • 移除未使用的功能
    • 优化构建配置
    • 监控样式体积
  4. 维护策略

    • 版本控制配置
    • 文档化自定义设置
    • 团队规范同步