"Why not just use inline styles?"
Utility classes resemble inline styles because both apply styles directly to an element rather than through a named class. Utilities still have three advantages:
- Design constraints: Inline styles require "magic numbers," while utilities draw values from a predefined design system, which keeps the UI consistent.
-
States (hover, focus, etc.): Inline styles can't target hover or focus, but variants such as
hover:bg-purple-600can. -
Media queries: Inline styles can't use media queries, but responsive variants such as
sm:flex-rowcan.
Key points of the page
The page explains how to build components by combining many single-purpose utility classes directly in your markup.
- Benefits: Faster development, safer changes (edits affect only one element), easier maintenance, portable markup, and CSS that doesn't grow in proportion to the number of features.
-
Thinking in utilities:
- Variants apply conditional styles (
hover:,sm:,dark:, etc.) and can be stacked. - Arbitrary values (e.g.
bg-[#316ff6]) handle one-off values outside the theme. -
group-hover:and arbitrary variants cover complex selectors. - Tailwind generates CSS by scanning your source files for class names.
- Inline styles are still appropriate for dynamic values coming from a database or API.
- Variants apply conditional styles (
-
Managing duplication: Use loops, multi-cursor editing, or components/template partials (recommended for shared styles). For simple cases, use custom CSS in
@layer components. -
Managing conflicts: Avoid putting conflicting classes on one element. If needed, use the
!modifier, theimportantflag, or theprefix(tw)option.
Top comments (0)