TechLead
Lesson 7 of 9
5 min read
Tailwind CSS

Customization & Configuration

Extend and customize Tailwind's default theme with custom colors, fonts, spacing, and more.

Customizing Tailwind CSS

Tailwind is highly customizable through its configuration file. You can extend the default theme, add custom utilities, or completely override the defaults.

Configuration File Structure

// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    './src/**/*.{js,jsx,ts,tsx,html}',
  ],
  theme: {
    // Override defaults completely
    colors: {
      // Replaces all default colors
    },
    extend: {
      // Extend defaults (recommended)
      colors: {
        // Adds to default colors
      },
    },
  },
  plugins: [],
}

Custom Colors

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        // Single color
        'brand': '#ff6b6b',

        // Color palette
        'brand': {
          50: '#fef2f2',
          100: '#fee2e2',
          200: '#fecaca',
          300: '#fca5a5',
          400: '#f87171',
          500: '#ef4444',
          600: '#dc2626',
          700: '#b91c1c',
          800: '#991b1b',
          900: '#7f1d1d',
          950: '#450a0a',
        },

        // Using CSS variables
        'primary': 'var(--color-primary)',
        'secondary': 'var(--color-secondary)',
      },
    },
  },
}

Use custom colors:

<div class="bg-brand-500 text-brand-50">Brand colored element</div>
<button class="bg-brand hover:bg-brand-600">Brand button</button>

Custom Fonts

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      fontFamily: {
        'sans': ['Inter', 'system-ui', 'sans-serif'],
        'serif': ['Merriweather', 'Georgia', 'serif'],
        'mono': ['Fira Code', 'monospace'],
        'display': ['Poppins', 'sans-serif'],
        'body': ['Open Sans', 'sans-serif'],
      },
    },
  },
}
<h1 class="font-display text-4xl">Display Font</h1>
<p class="font-body">Body text with Open Sans</p>
<code class="font-mono">Monospace code</code>

Custom Spacing

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      spacing: {
        '128': '32rem',
        '144': '36rem',
        '4.5': '1.125rem',
        'page': '1200px',
      },
    },
  },
}
<div class="w-128 p-4.5">Custom spacing</div>
<div class="max-w-page mx-auto">Page width container</div>

Custom Breakpoints

// tailwind.config.js
module.exports = {
  theme: {
    screens: {
      'xs': '475px',
      'sm': '640px',
      'md': '768px',
      'lg': '1024px',
      'xl': '1280px',
      '2xl': '1536px',
      '3xl': '1920px',
      // Max-width breakpoints
      'max-md': { 'max': '767px' },
      // Range
      'tablet': { 'min': '640px', 'max': '1023px' },
    },
  },
}

Custom Border Radius

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      borderRadius: {
        '4xl': '2rem',
        '5xl': '2.5rem',
      },
    },
  },
}

Custom Box Shadows

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'soft': '0 2px 15px -3px rgba(0, 0, 0, 0.07), 0 10px 20px -2px rgba(0, 0, 0, 0.04)',
        'inner-lg': 'inset 0 2px 4px 0 rgb(0 0 0 / 0.1)',
        'brand': '0 4px 14px 0 rgba(239, 68, 68, 0.39)',
      },
    },
  },
}

Custom Animations

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      animation: {
        'fade-in': 'fadeIn 0.5s ease-in-out',
        'slide-up': 'slideUp 0.3s ease-out',
        'bounce-slow': 'bounce 2s infinite',
        'spin-slow': 'spin 3s linear infinite',
      },
      keyframes: {
        fadeIn: {
          '0%': { opacity: '0' },
          '100%': { opacity: '1' },
        },
        slideUp: {
          '0%': { transform: 'translateY(10px)', opacity: '0' },
          '100%': { transform: 'translateY(0)', opacity: '1' },
        },
      },
    },
  },
}
<div class="animate-fade-in">Fading in</div>
<div class="animate-slide-up">Sliding up</div>

Adding Custom Utilities with Plugins

// tailwind.config.js
const plugin = require('tailwindcss/plugin');

module.exports = {
  plugins: [
    plugin(function({ addUtilities, addComponents, theme }) {
      // Add custom utilities
      addUtilities({
        '.text-shadow': {
          'text-shadow': '2px 2px 4px rgba(0, 0, 0, 0.1)',
        },
        '.text-shadow-lg': {
          'text-shadow': '4px 4px 8px rgba(0, 0, 0, 0.2)',
        },
        '.scrollbar-hide': {
          '-ms-overflow-style': 'none',
          'scrollbar-width': 'none',
          '&::-webkit-scrollbar': {
            display: 'none',
          },
        },
      });

      // Add custom components
      addComponents({
        '.btn': {
          padding: theme('spacing.4') + ' ' + theme('spacing.6'),
          borderRadius: theme('borderRadius.lg'),
          fontWeight: theme('fontWeight.semibold'),
        },
        '.card': {
          backgroundColor: theme('colors.white'),
          borderRadius: theme('borderRadius.xl'),
          padding: theme('spacing.6'),
          boxShadow: theme('boxShadow.lg'),
        },
      });
    }),
  ],
}

Using CSS Variables

/* globals.css */
:root {
  --color-primary: 59 130 246; /* blue-500 RGB */
  --color-secondary: 168 85 247; /* purple-500 RGB */
}

.dark {
  --color-primary: 96 165 250; /* blue-400 RGB */
  --color-secondary: 192 132 252; /* purple-400 RGB */
}
// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: 'rgb(var(--color-primary) / <alpha-value>)',
        secondary: 'rgb(var(--color-secondary) / <alpha-value>)',
      },
    },
  },
}
<div class="bg-primary text-white">Uses CSS variable</div>
<div class="bg-primary/50">50% opacity works too!</div>

Continue Learning