What Are Utility Classes?
Utility classes in Tailwind CSS are single-purpose classes that apply one specific style. Rather than writing custom CSS, you use these predefined classes directly in your HTML to style elements quickly and consistently.
1. Why Use Utility Classes?
- Write less custom CSS
- Keep styles consistent across components
- Faster development with reusable patterns
- Smaller final CSS with JIT compilation
2. Common Examples
Here are a few utility classes youβll use often:
p-4 β padding: 1remtext-center β text-align: centerbg-blue-500 β background-color: bluerounded-lg β border-radius: largehover:text-white β white text on hovermd:flex β display: flex on medium screens3. Without vs With Utility Classes
Traditional CSS:
.card {
background-color: white;
padding: 1rem;
border-radius: 0.5rem;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}
Tailwind Utility Classes:
<div class="bg-white p-4 rounded shadow">...</div>
4. When Not to Use Utilities
Utility classes work best for smaller components and layouts. For large, repeated styles, consider extracting them with @apply or custom classes.
Conclusion
Utility classes make your code faster, cleaner, and easier to maintain. Theyβre the heart of Tailwind CSS and a major shift from traditional CSS writing.