CSSJanuary 6, 20266 min read

Tailwind CSS Tips & Tricks for Better UI

Rohit Kumar
Rohit Kumar
React, WordPress & Automation Expert

Tailwind CSS has changed the way we build user interfaces. Instead of writing custom CSS for everything, you use small utility classes to style your components. But there's so much more to Tailwind than just adding classes. Let me share some tips and tricks that have made my development much faster and my UI much better.

Using the @apply Directive for Common Patterns

Sometimes you have styles that repeat across multiple components. Instead of writing the same long list of classes everywhere, you can use the @apply directive in your CSS to create reusable styles.

/* In your globals.css or component CSS */
.btn-primary {
  @apply px-6 py-3 bg-blue-600 text-white rounded-lg;
  @apply hover:bg-blue-700 transition-colors;
  @apply font-semibold shadow-md;
}

/* Now use it in your components */
<button className="btn-primary">
  Click Me
</button>

This keeps your HTML clean and makes it easy to update the button style in one place. Just don't overuse it - Tailwind's power comes from utility classes, so use @apply only for truly repetitive patterns.

Customizing Your Design System

The default Tailwind colors and spacing are great, but you probably want to match your brand. You can customize everything in tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      colors: {
        brand: {
          50: '#f0f9ff',
          100: '#e0f2fe',
          500: '#0ea5e9',
          900: '#0c4a6e',
        },
      },
      fontFamily: {
        sans: ['Inter', 'sans-serif'],
      },
      spacing: {
        '128': '32rem',
        '144': '36rem',
      },
    },
  },
}

Now you can use bg-brand-500 or w-128 in your classes. The extend key adds to the default theme without removing anything.

Responsive Design Made Easy

Tailwind's responsive prefixes make it super easy to create mobile-first designs. Start with mobile styles, then add breakpoints as you go up:

<div className="
  w-full          {/* Mobile: full width */}
  md:w-1/2        {/* Tablet: half width */}
  lg:w-1/3        {/* Desktop: one-third width */}
  px-4            {/* Mobile padding */}
  md:px-6         {/* Tablet padding */}
  lg:px-8         {/* Desktop padding */}
">
  Your content
</div>
Always design mobile-first with Tailwind. Start with base styles, then use sm:, md:, lg:, and xl: prefixes to adjust for larger screens.

Using Arbitrary Values for One-Off Styles

Sometimes you need a specific value that's not in the default scale. Instead of adding it to your config, you can use arbitrary values in square brackets:

<div className="
  w-[347px]           {/* Specific width */}
  top-[117px]         {/* Exact positioning */}
  bg-[#1da1f2]        {/* Twitter blue */}
  rotate-[23deg]      {/* Custom rotation */}
">
  Content
</div>

This is perfect for those one-off situations where you need exact control. Just don't overuse it - if you're using the same value in multiple places, add it to your config instead.

Dark Mode Without the Hassle

Adding dark mode is incredibly simple with Tailwind. Just add the dark: prefix to any utility:

<div className="
  bg-white dark:bg-gray-900
  text-gray-900 dark:text-white
  border border-gray-200 dark:border-gray-700
">
  Works in both light and dark mode!
</div>

Enable it in your tailwind.config.js:

module.exports = {
  darkMode: 'class', // or 'media' for system preference
  // rest of config...
}

With 'class' mode, toggle dark mode by adding the dark class to your HTML element. With 'media' mode, it follows the user's system preference automatically.

Group Hover for Complex Interactions

Want child elements to change when you hover over a parent? Use group and group-hover::

<div className="group p-6 bg-white hover:bg-blue-50 rounded-lg">
  <h3 className="text-gray-900 group-hover:text-blue-600">
    Card Title
  </h3>
  <p className="text-gray-600 group-hover:text-gray-900">
    This text changes too when you hover over the card!
  </p>
  <button className="opacity-0 group-hover:opacity-100">
    Hidden until hover
  </button>
</div>

Using Container Queries

Tailwind now supports container queries, which are amazing for component-based responsive design. Instead of reacting to viewport size, elements react to their container size:

<div className="@container">
  <div className="@lg:grid @lg:grid-cols-2 gap-4">
    {/* This becomes a grid when the CONTAINER is large, */}
    {/* not when the viewport is large */}
  </div>
</div>

Reusing Patterns with Components

If you're using React or Vue, create reusable components for common patterns instead of copying classes:

// Card.jsx
export function Card({ children, className = '' }) {
  return (
    <div className={`
      bg-white rounded-lg shadow-md p-6
      hover:shadow-xl transition-shadow
      ${className}
    `}>
      {children}
    </div>
  )
}

// Usage
<Card className="mb-4">
  <h3>My Card</h3>
  <p>Content here</p>
</Card>

Using Plugins for Extra Functionality

Tailwind has official plugins that add useful utilities. Install the ones you need:

npm install -D @tailwindcss/forms @tailwindcss/typography

// tailwind.config.js
module.exports = {
  plugins: [
    require('@tailwindcss/forms'),      // Better form styles
    require('@tailwindcss/typography'), // Prose classes for content
  ],
}

The typography plugin is especially useful for blog posts and documentation. Just wrap your content in className="prose" and it styles all your headings, paragraphs, lists, and more automatically.

Performance Tips

Tailwind automatically removes unused CSS in production, but you can help it work better:

// tailwind.config.js
module.exports = {
  content: [
    './app/**/*.{js,ts,jsx,tsx}',    // Be specific
    './components/**/*.{js,ts,jsx,tsx}',
  ],
  // ...
}

Creating Smooth Animations

Tailwind includes animation utilities, but you can add custom ones:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      animation: {
        'fade-in': 'fadeIn 0.5s ease-in',
        'slide-up': 'slideUp 0.3s ease-out',
      },
      keyframes: {
        fadeIn: {
          '0%': { opacity: '0' },
          '100%': { opacity: '1' },
        },
        slideUp: {
          '0%': { transform: 'translateY(20px)', opacity: '0' },
          '100%': { transform: 'translateY(0)', opacity: '1' },
        },
      },
    },
  },
}

// Use it:
<div className="animate-fade-in">I fade in!</div>
<div className="animate-slide-up">I slide up!</div>

Final Thoughts

Tailwind CSS is incredibly powerful when you know these tips. Start with the basics, customize it to match your brand, and use these advanced techniques to build beautiful UIs faster than ever. The key is finding the right balance - use utility classes for most things, create components for complex patterns, and customize your config for your specific needs.

Remember, the best way to get better at Tailwind is to use it. Build projects, experiment with different approaches, and you'll develop your own workflow that works for you.

Tags:Tailwind CSSCSSUI
Share this article: