Lesson 3
18 min
The class naming logic, the value scale and arbitrary values.
Definition
A utility class is a short, predictable CSS class that applies a single CSS declaration (or close to it) to an element. For example,text-center is equivalent totext-align: center;. By combining several of these classes in theclass attribute, you compose the complete style of an element without writing any custom CSS.
The heart of Tailwind is its utility classes: short, predictable class names that each correspond (or nearly) to a single CSS declaration. Once you understand the naming logic, you can guess most classes without having to consult the documentation.
This predictability is intentional: Tailwind reuses the vocabulary of CSS rather than inventing its own. If you know that the property is calledtext-align, you can guess that the class will start withtext-. This is what makes learning fast for someone who already knows the basics of CSS.
Most classes follow the property-value pattern. Here are a few examples to convince you:
<div class="flex"> <!-- display: flex; -->
<div class="block"> <!-- display: block; -->
<div class="hidden"> <!-- display: none; -->
<div class="text-center"> <!-- text-align: center; -->
<div class="font-bold"> <!-- font-weight: 700; -->
<div class="rounded-lg"> <!-- border-radius: 0.5rem; -->
<div class="w-full"> <!-- width: 100%; -->
<div class="uppercase"> <!-- text-transform: uppercase; -->These classes combine freely in the class attribute, separated by spaces. The order does not matter:
<button class="bg-indigo-600 text-white font-semibold px-4 py-2 rounded-lg">
Send
</button>In this button, each class has a precise role: bg-indigo-600colors the background, text-white sets the text color,font-semibold the font weight, px-4 andpy-2 the horizontal and vertical padding, androunded-lg rounds the corners.
Tailwind's hundreds of classes fall into a few major families. Recognizing them helps you find them faster:
flex, grid,block, hidden, relative.p-* (padding),m-* (margin), gap-*, space-y-*.w-* (width),h-* (height), max-w-*, min-h-*.text-* (size or color),font-*, leading-*, uppercase.bg-*,border-*, rounded-*, shadow-*.Rather than accepting any arbitrary value, Tailwind offers a consistent numeric scale for spacing, sizes and other dimensions. This scale is generally based on a multiple of 0.25rem (4px):
p-0 → padding: 0p-1 → padding: 0.25rem (4px)p-2 → padding: 0.5rem (8px)p-4 → padding: 1rem (16px)p-8 → padding: 2rem (32px)This same numeric logic applies to a great many properties:m-* (margin), w-* (width), h-*(height), gap-*, text-* for font sizes, and so on. Learning this scale once lets you reuse it everywhere.
Calculation tip: to find the value in pixels, simply multiply the number by 4. So p-6 is 24px and p-12 is 48px.
Beyond simple numbers, a few notations come up often and are worth knowing from the start:
<div class="w-1/2"> <!-- width: 50%; -->
<div class="bg-blue-500"> <!-- a shade of blue (from 50 to 950) -->
<div class="bg-black/50"> <!-- black at 50% opacity -->
<div class="-mt-2"> <!-- margin-top: -0.5rem; (negative value) -->Fractions (1/2, 1/3...) give widths as percentages, the slash after a color sets its opacity, and a hyphen in front of a spacing class produces a negative value.
Sometimes no value on the scale matches your need exactly (for example a design that requires precisely 13px). Tailwind then lets you use an arbitrary value in square brackets:
<div class="w-[137px] top-[13px] bg-[#1a1a2e]">
Custom value
</div>Use them sparingly: they are very useful occasionally, but overusing them loses one of Tailwind's main advantages, the visual consistency provided by the predefined scale.
A utility class can also apply only in certain contexts. You just add a prefix followed by a colon in front of the class. You will see these variants in detail in the upcoming lessons, but here is a first preview:
<button class="bg-indigo-600 hover:bg-indigo-700">
Darker background on hover
</button>
<div class="p-2 md:p-6">
Smaller padding on mobile, larger from md and up
</div>
<p class="text-slate-900 dark:text-white">
Text adapted to dark mode
</p>Here, hover: targets the mouse hover, md:kicks in from a medium-sized screen and dark:applies in dark mode. The class stays the same, only the context changes.
When facing a long list of classes, the right method is to read it in small groups: first the layout, then the sizing and spacing, then the colors and typography. Take this example:
<div class="flex items-center gap-3 p-4 rounded-xl bg-white shadow-sm">
Card
</div>You read it as: a flex container whose items are vertically centered (items-center) and spaced 12px apart (gap-3), with 16px of padding, rounded corners, a white background and a subtle shadow. With a bit of practice, this decoding becomes almost instantaneous.
padding-4 instead of p-4. An unknown class is ignored without an error."p-" + value. Tailwind only detects classes that are written out in full in your code.Nobody memorizes all of Tailwind's classes by heart, not even experienced developers. In practice, you use your editor's autocompletion (the officialTailwind CSS IntelliSenseextension for VS Code) and you refer to theofficial documentationwhen in doubt. What matters is understanding the logic, not memorizing the exhaustive list.
To make progress, practice the exercise in both directions: starting from a CSS rule, find the Tailwind class, then starting from a class, find the CSS rule it produces. A few minutes a day is enough for this vocabulary to become a reflex.
Practical exercise
display: flex;,font-size: 1.5rem;, border-radius: 9999px;,text-align: right;. Then check your answers (hints:flex, text-2xl, rounded-full,text-right).