TailwindCSS provides a powerful configuration system that allows developers to fully customize the design system, including colors, fonts, spacing, breakpoints, and more.
Configuration File Basics
TailwindCSS uses the tailwind.config.js file for configuration, which is typically located in the project root directory.
Basic Configuration Structure
javascriptmodule.exports = { content: [ './src/**/*.{html,js,ts,jsx,tsx,vue,svelte}', ], theme: { extend: { // Custom configuration }, }, plugins: [], }
Color Configuration
1. Custom Colors
javascriptmodule.exports = { theme: { extend: { colors: { 'brand': { '50': '#f0f9ff', '100': '#e0f2fe', '500': '#0ea5e9', '900': '#0c4a6e', }, 'accent': '#ff6b6b', }, }, }, }
2. Using Custom Colors
html<div class="bg-brand-500 text-white"> Using custom brand colors </div>
Font Configuration
1. Adding Custom Fonts
javascriptmodule.exports = { theme: { extend: { fontFamily: { 'sans': ['Inter', 'system-ui', 'sans-serif'], 'serif': ['Merriweather', 'Georgia', 'serif'], 'mono': ['Fira Code', 'monospace'], }, }, }, }
2. Font Size Configuration
javascriptmodule.exports = { theme: { extend: { fontSize: { 'xxs': '0.625rem', 'huge': '5rem', }, }, }, }
Spacing Configuration
1. Custom Spacing
javascriptmodule.exports = { theme: { extend: { spacing: { '128': '32rem', '144': '36rem', }, }, }, }
2. Using Custom Spacing
html<div class="p-128 m-144"> Custom spacing </div>
Breakpoint Configuration
1. Custom Breakpoints
javascriptmodule.exports = { theme: { extend: { screens: { 'xs': '475px', '3xl': '1600px', 'print': {'raw': 'print'}, }, }, }, }
Border and Radius Configuration
1. Custom Borders
javascriptmodule.exports = { theme: { extend: { borderWidth: { '3': '3px', }, borderRadius: { '4xl': '2rem', }, }, }, }
Shadow Configuration
javascriptmodule.exports = { theme: { extend: { boxShadow: { 'glow': '0 0 20px rgba(0, 0, 0, 0.5)', 'inner-lg': 'inset 0 2px 4px 0 rgba(0, 0, 0, 0.06)', }, }, }, }
Animation Configuration
javascriptmodule.exports = { theme: { extend: { animation: { 'bounce-slow': 'bounce 3s infinite', 'spin-slow': 'spin 3s linear infinite', }, keyframes: { 'bounce-slow': { '0%, 100%': { transform: 'translateY(-25%)', animationTimingFunction: 'cubic-bezier(0.8, 0, 1, 1)' }, '50%': { transform: 'translateY(0)', animationTimingFunction: 'cubic-bezier(0, 0, 0.2, 1)' }, }, }, }, }, }
Plugin System
1. Using Official Plugins
javascriptconst forms = require('@tailwindcss/forms'); const typography = require('@tailwindcss/typography'); module.exports = { plugins: [ forms, typography, ], }
2. Creating Custom Plugins
javascriptconst plugin = require('tailwindcss/plugin'); module.exports = { plugins: [ plugin(function({ addUtilities, theme }) { const newUtilities = { '.text-shadow': { textShadow: theme('textShadow.DEFAULT'), }, }; addUtilities(newUtilities); }), ], }
Preset Configuration
TailwindCSS supports preset configurations that can be shared across multiple projects:
javascript// tailwind.config.js module.exports = { presets: [ require('./tailwind.preset.js'), ], theme: { extend: { // Override configurations in preset }, }, }
Best Practices
- Use extend instead of override: Use the
extendobject to extend the default theme, avoid completely overriding - Organize configuration structure: Group related configurations together to keep code clear
- Use semantic naming: Use meaningful names for custom colors, fonts, etc.
- Version control configuration: Include configuration files in version control
- Document custom configuration: Create configuration documentation for the team
Configuration Validation
You can use npx tailwindcss init --full to generate a complete configuration file template to understand all configurable options.