Back to blog

10 Tailwind CSS Tips for Better Development

3 min read

10 Tailwind CSS Tips for Better Development

Tailwind CSS has revolutionized how we approach styling in modern web development. Here are 10 practical tips to help you use Tailwind more effectively.

1. Use @apply for Component Styles

When you find yourself repeating the same utility classes, consider using @apply:

.btn-primary {
  @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}

2. Leverage Custom Utilities

Create custom utilities for project-specific needs:

@layer utilities {
  .text-shadow {
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
  }
}

3. Use Arbitrary Values Sparingly

While arbitrary values are powerful, use them judiciously:

<!-- Good for one-off cases -->
<div class="top-[117px]">
<!-- Better to add to config for reusable values -->
<div class="top-custom">

4. Organize Classes Logically

Group related utilities together for better readability:

<!-- Layout -->
<div class="flex flex-col items-center justify-center
           <!-- Spacing -->
           p-4 m-2
           <!-- Colors -->
           bg-white text-gray-800
           <!-- Effects -->
           shadow-lg rounded-lg">

5. Use Responsive Design Systematically

Start with mobile-first design:

<div class="text-sm md:text-base lg:text-lg xl:text-xl">
  Responsive text
</div>

6. Leverage CSS Variables with Tailwind

Combine CSS custom properties with Tailwind:

:root {
  --primary-color: #3b82f6;
}
 
.bg-primary {
  background-color: var(--primary-color);
}

7. Use Tailwind's Built-in Dark Mode

Enable dark mode in your config:

module.exports = {
  darkMode: 'class',
  // ... rest of config
}

Then use dark mode utilities:

<div class="bg-white dark:bg-gray-800 text-black dark:text-white">
  Content that adapts to theme
</div>

8. Optimize for Production

Always purge unused styles in production:

module.exports = {
  content: [
    './pages/**/*.{js,ts,jsx,tsx}',
    './components/**/*.{js,ts,jsx,tsx}',
  ],
  // ... rest of config
}

9. Use Tailwind IntelliSense

Install the official VS Code extension for:

  • Autocomplete
  • Linting
  • Hover previews
  • Syntax highlighting

10. Create a Design System

Extend Tailwind's config to match your design system:

module.exports = {
  theme: {
    extend: {
      colors: {
        brand: {
          50: '#eff6ff',
          500: '#3b82f6',
          900: '#1e3a8a',
        }
      },
      fontFamily: {
        sans: ['Inter', 'sans-serif'],
      }
    }
  }
}

Conclusion

These tips will help you write more maintainable and efficient Tailwind CSS. Remember, the key is finding the right balance between utility classes and custom styles for your specific project needs.

Happy styling!