Utility-First CSS
Problem
Creating custom CSS classes for every component variation leads to bloated stylesheets and decision fatigue. Developers spend time naming classes like “card-header-large-bold-centered” only to need similar styles elsewhere, resulting in duplicated CSS and inconsistent spacing or colors across the interface.
Solution
Compose styles from small, single-purpose utility classes rather than writing custom CSS. This speeds development and enforces consistency but can create verbose markup.
Example
This demonstrates building interfaces with small, single-purpose utility classes instead of custom CSS, speeding development and enforcing design consistency through a predefined system.
<!-- Build layout with utility classes instead of custom CSS -->
<div class="flex items-center justify-between p-4 bg-white rounded-lg shadow">
<!-- Typography utilities for text styling -->
<h2 class="text-xl font-bold text-gray-900">Title</h2>
<!-- Spacing, color, and state utilities for button -->
<button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
Click me
</button>
</div>
Benefits
- Speeds development by eliminating need to write and name custom CSS.
- Enforces consistency by using predefined spacing, colors, and sizes.
- Reduces CSS bloat since utilities are reused across components.
- Makes it easy to build responsive designs with utility variants.
Tradeoffs
- Creates verbose HTML with long lists of class names.
- Requires learning utility class naming conventions.
- Can be harder to read and maintain than semantic class names.
- May need build step to purge unused utilities for production.