Tailwind CSS has completely changed how we write CSS. It makes building beautiful interfaces so much faster. Let me share my favorite tips and tricks that I use all the time to make my work easier and more efficient.
Using @apply for Common Patterns
One thing I love about Tailwind is that you don't have to repeat the same classes everywhere. When you find yourself using the same combination of utility classes again and again, you can extract them into a component class using @apply. Like for buttons, instead of writing all those classes every time, create a reusable button class once and use it everywhere.
@layer components {
.btn-primary {
@apply px-6 py-3 bg-indigo-600 text-white rounded-lg;
@apply hover:bg-indigo-700 transition-colors;
@apply font-semibold shadow-lg hover:shadow-xl;
}
}
Arbitrary Values Give You Exact Control
Sometimes you need a specific value that's not in Tailwind's default scale. That's where square brackets come in super handy. You can use any custom value you want by wrapping it in square brackets. Need a width of exactly 347 pixels? No problem. Want to use your brand color? Just put it in brackets.
<div class="w-[347px] top-[117px]">
<p class="text-[#1da1f2]">
<div class="grid-cols-[200px_1fr_100px]">
Group Hover for Parent-Child Effects
This is one of my favorite features. You can style child elements based on the parent's state. Just add the group class to the parent, and then use group-hover: on any child element. When you hover over the parent, all the children with group-hover classes will update. Perfect for cards and complex components.
<div class="group">
<img class="group-hover:scale-110 transition">
<h3 class="group-hover:text-indigo-600">
</div>
Peer for Sibling Interactions
Similar to group, but for sibling elements. Mark one element as peer, and then you can style its siblings based on that peer's state. Super useful for custom checkboxes and radio buttons where you want to style the label based on whether the input is checked.
<input class="peer" type="checkbox">
<label class="peer-checked:text-indigo-600">
Adding Your Brand Colors
Tailwind's default colors are great, but you'll want to add your own brand colors. You can extend the theme in your config file and add as many custom colors as you need. Once you add them, you can use them just like any other Tailwind color throughout your project.
module.exports = {
theme: {
extend: {
colors: {
brand: {
50: '#f0f9ff',
500: '#0ea5e9',
900: '#0c4a6e',
}
}
}
}
}
Dark Mode is Super Easy
Implementing dark mode with Tailwind is ridiculously simple. Just add the dark: prefix to any utility class and it'll apply when dark mode is active. First, set darkMode: 'class' in your config, then add a dark class to your HTML element to enable it.
<div class="bg-white dark:bg-gray-900">
<h1 class="text-gray-900 dark:text-white">
</div>
Responsive Design Made Simple
Tailwind's responsive prefixes make building mobile-first designs really easy. By default, classes apply to all screen sizes, and you add prefixes to change things at larger breakpoints. sm: starts at 640px, md: at 768px, lg: at 1024px, xl: at 1280px, and 2xl: at 1536px. Just prefix any class with these and you're good to go.
<div class="text-sm md:text-base lg:text-lg">
Spacing Between Elements
Instead of adding margins to each element individually, use space-x for horizontal spacing and space-y for vertical spacing on the parent container. Tailwind automatically adds the right margin between all children except the last one. It's so much cleaner than adding margins everywhere.
<div class="flex space-x-4">
<button>One</button>
<button>Two</button>
<button>Three</button>
</div>
Borders Between List Items
When you have a list and want borders between items (but not at the top or bottom), use the divide-y utility. It's perfect for menus, lists, and vertical layouts where you need visual separation between items.
<div class="divide-y divide-gray-200">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Beautiful Gradient Backgrounds
Creating gradients in Tailwind is super intuitive. Specify the direction with things like bg-gradient-to-r (left to right) or bg-gradient-to-br (diagonal), then add your colors with from-, via-, and to- prefixes. You can create stunning backgrounds in seconds.
<div class="bg-gradient-to-r from-purple-400 via-pink-500 to-red-500">
<div class="bg-gradient-to-br from-indigo-500 to-purple-600">
Better Focus States with Ring
The ring utilities are perfect for focus states on buttons and inputs. They create an outline that doesn't affect the layout (unlike borders). You can control the width, color, and even the opacity. Much better than the default browser focus outline.
<button class="focus:ring-4 focus:ring-indigo-500 focus:ring-opacity-50">
Handling Long Text
For text that might overflow, truncate will cut it off with an ellipsis. If you need to show multiple lines before truncating, use line-clamp-3 (or whatever number you want). Really handy for cards and preview text.
<p class="truncate">Very long text...</p>
<p class="line-clamp-3">Multiple lines...</p>
Consistent Aspect Ratios
Need to maintain a specific aspect ratio? The aspect- utilities make it super easy. aspect-video gives you 16:9, perfect for video embeds. aspect-square keeps things at 1:1. No more padding hacks!
<div class="aspect-video"> <!-- 16:9 -->
<div class="aspect-square"> <!-- 1:1 -->
Container Queries Are Here
This is a newer feature that's super powerful. Container queries let elements respond to their container's size instead of the viewport. Mark a container with @container, then use @lg:, @md: etc. on children. Game changer for component-based design.
<div class="@container">
<div class="@lg:text-xl">
</div>
Creating Your Own Utilities
Sometimes you need utilities that Tailwind doesn't provide. You can add your own custom utilities in the config. Text shadows, custom scrollbar hiding, whatever you need - just add them to the utilities layer and use them like any other Tailwind class.
@layer utilities {
.text-shadow {
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}
.no-scrollbar::-webkit-scrollbar {
display: none;
}
}
A Few More Pro Tips
Install the Tailwind IntelliSense extension for VS Code - it'll autocomplete classes and show you the actual CSS. The Prettier Tailwind plugin will automatically sort your classes in a consistent order, which makes things much more readable.
Always make sure to purge unused styles in production to keep your CSS file small. You can create a whole design system by customizing theme values in your config. And remember, use @apply sparingly - the whole point of Tailwind is the utility-first approach, so embrace it!
Start Using These Today
These tips have made me so much faster at building UIs. Tailwind becomes incredibly powerful once you know all these little tricks. Try them out in your next project and see how much more productive you become. You'll wonder how you ever built websites without them!