
Tailwind CSS is a utility-first CSS framework that allows you to build modern websites without leaving your HTML. Unlike traditional frameworks like Bootstrap or Foundation, Tailwind doesn't provide pre-built components. Instead, it gives you low-level utility classes that you can combine to create custom designs.
To get started with Tailwind CSS, you need to install it via npm:
npm install -D tailwindcss
npx tailwindcss init
Instead of writing CSS like this:
.card {
background-color: white;
border-radius: 0.25rem;
padding: 1rem;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
You use utility classes in your HTML:
<div class="bg-white rounded-lg p-4 shadow-md">
<!-- Content here -->
</div>
Tailwind uses a mobile-first breakpoint system. You can add prefixes to any utility:
sm: (640px and up)md: (768px and up)lg: (1024px and up)xl: (1280px and up)2xl: (1536px and up)Example:
<div class="w-full md:w-1/2 lg:w-1/3">
<!-- This div will be full width on mobile, half width on medium screens,
and one-third width on large screens -->
</div>
m-{size} (m-2, m-4, etc.)p-{size} (p-2, p-4, etc.){property}{direction}-{size}
text-{color}-{shade}bg-{color}-{shade}border-{color}-{shade}Example:
<button class="bg-blue-500 text-white hover:bg-blue-600">
Click me
</button>
text-sm, text-base, text-lg, text-xlfont-thin, font-normal, font-boldtext-left, text-center, text-rightTailwind provides state variants for most utilities:
<button class="bg-blue-500 hover:bg-blue-700 focus:ring-2 focus:ring-blue-400">
Hover and focus me
</button>
Keep it organized: Group related utilities together
<div class="
// Position and layout
flex items-center justify-between
// Spacing
p-4 my-2
// Visual styling
bg-white rounded-lg shadow-md
">
Extract components: When you find yourself repeating the same utility patterns, consider extracting them into components
Use @apply for complex patterns: When you need to reuse a complex set of utilities, use @apply in your CSS:
.btn-primary {
@apply px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600;
}
Tailwind CSS provides a powerful and flexible way to style your applications. While the learning curve might seem steep at first, the productivity gains and maintainability benefits make it worth the investment. Start small, learn the common utilities, and gradually expand your Tailwind toolkit as you build more complex interfaces.