How Tailwind CSS utility classes map to real CSS
Tailwind CSS is a utility-first framework: instead of writing a new rule for each component, you combine small classes that each set one or two properties. Knowing the naming pattern lets you read and write it without a reference open.
The naming pattern
Most utilities follow property-value, with an optional prefix in front for a screen size or a state:
So md:hover:bg-blue-600 reads as “from the medium breakpoint up, on hover, set the background colour to blue shade 600”. The build step scans your files for class names like this and writes only the CSS rules you used.
Reading the spacing scale
In the default theme one spacing step is 0.25rem. With the usual browser root font size of 16px, that is 4px per step.
size in px = number × 4 (at a 16px root size)
| Class | CSS it produces | Pixels at 16px root |
|---|---|---|
p-4 | padding: 1rem | 16px |
px-6 | left and right padding of 1.5rem | 24px |
py-3 | top and bottom padding of 0.75rem | 12px |
gap-2.5 | gap: 0.625rem | 10px |
mt-8 | margin-top: 2rem | 32px |
w-1/3 | width: 33.333333% | depends on parent |
A user who raises their browser font size scales every rem value too, which is one reason rem units are friendlier than fixed pixels.
Responsive prefixes are mobile-first
An unprefixed class applies at every width. A prefixed class applies from that width upward. The default breakpoints are:
| Prefix | Minimum width |
|---|---|
sm: | 640px |
md: | 768px |
lg: | 1024px |
xl: | 1280px |
2xl: | 1536px |
So flex-col md:flex-row stacks items on phones and lines them up in a row from 768px. A common mistake is writing sm: to mean “on small screens”. It means “640px and wider”.
Translating a component by hand
Here is a button written in plain CSS:
.btn {
display: inline-flex; align-items: center; gap: 0.5rem;
padding: 0.75rem 1.5rem; border-radius: 0.5rem;
font-weight: 600; color: white; background: #2563eb;
}
.btn:hover { background: #1d4ed8; }
And the same button as utilities:
<button class="inline-flex items-center gap-2 px-6 py-3 rounded-lg
font-semibold text-white bg-blue-600 hover:bg-blue-700">
Save
</button>
- Take each declaration in turn.
- Convert lengths to scale steps by dividing the rem value by 0.25 (0.75rem ÷ 0.25 = 3, so
py-3). - Match colours to the nearest palette shade, or use an arbitrary value such as
bg-[#2563eb]. - Move pseudo-classes into prefixes:
:hoverbecomeshover:.
Traps to avoid
- Building class names from pieces.
"bg-" + colour + "-500"never appears in your source as a whole word, so the scanner does not generate it. Write the full class names out, for example in a lookup object. - Too many arbitrary values.
mt-[13px]works, but many of them mean you have left the design scale and lost consistency. - Conflicting utilities.
p-4 p-6on one element does not mean “the last one wins” by class order. The order of rules in the generated stylesheet decides, so remove one. - Endless repetition. When the same twenty classes appear on every button, move them into a component in your framework or template, not into a copy-paste habit.
Questions
Does Tailwind make pages heavier?
The production stylesheet only contains the utilities you used, so it often stays small. The HTML gets longer, but repeated class names compress well.
Can I mix Tailwind with ordinary CSS?
Yes. You can still write normal rules for anything awkward to express as utilities, such as complex animations or third-party widget overrides.
Is the theme fixed?
No. You can change colours, the spacing step, fonts and breakpoints in the project configuration. Check the documentation for the version you install, because the configuration format changed between major versions.