Tailwind Class Converter Guide: From CSS to Utility Classes
Tailwind CSS takes a different approach from traditional stylesheets. Instead of writing a custom class for each component, you compose utilities directly in your markup: one class for padding, one for colour, one for rounded corners. The result is styling that lives where it is used, and a stylesheet that only includes the utilities you actually use.
Moving an existing design into Tailwind means translating CSS properties into utility classes. The Tailwind Class Converter performs that translation. This guide explains the mapping, the spacing scale that underpins it, and how to handle the cases that do not convert cleanly.
The spacing scale: the key to conversion
Most Tailwind conversions run through the spacing scale, which maps numbers to rem values in steps of 0.25rem. The class p-4 means 1rem of padding, because 4 times 0.25rem is 1rem. The class py-2 means 0.5rem of vertical padding, because 2 times 0.25rem is 0.5rem. The class mt-8 means 2rem of top margin, because 8 times 0.25rem is 2rem. Once you know the scale, most spacing conversions become simple division: divide the rem value by 0.25 to get the class number.
Pixel values convert through the same scale, since 1rem is normally 16 pixels. A padding of 16 pixels is 1rem, which is p-4. A padding of 8 pixels is 0.5rem, which is p-2. A margin of 32 pixels is 2rem, which is m-8. Values that do not fall on the scale, such as 13 pixels, have no exact utility class, and need either rounding to the nearest scale step or an arbitrary value, written in Tailwind as p-[13px]. Arbitrary values work, but they should be the exception: a design full of arbitrary values has not really adopted the scale.
The scale also covers width, height, gap, and positioning, not just padding and margin. The class w-64 means a width of 16rem, because 64 times 0.25rem is 16rem. The class gap-6 means a gap of 1.5rem. Learning the scale once unlocks conversions across all of these properties.
Colours, typography, and layout
Colours convert through the palette: Tailwind's colour names combine a hue with a shade number, such as blue-500 or gray-200. A hex colour from an existing design rarely matches a palette colour exactly, so conversion usually means picking the nearest palette shade, or defining a custom colour in the Tailwind configuration for brand colours that must match precisely. For brand colours, custom definitions are the right choice: approximating a logo colour with the nearest palette shade is a visible error.
Typography converts similarly. Font size classes, such as text-base, text-lg, and text-xl, map to specific sizes and line heights, and font weight classes, such as font-medium and font-bold, map to numeric weights. Text alignment, letter spacing, and line height each have their own utilities. A CSS rule of font-size 18px with line-height 28px converts approximately to text-lg, though the exact mapping depends on the Tailwind version's defaults, so check the configuration rather than assuming.
Layout properties convert directly in most cases. Display flex becomes flex, grid becomes grid, and positioning such as absolute and relative keeps its name. Flexbox and grid properties — justify-content, align-items, gap — each have utilities, usually with the property name shortened: justify-center, items-center, gap-4. The conversion is mostly mechanical, which is why a converter handles it well.
Worked example: converting a button
Take this CSS for a button:
.button { padding: 0.5rem 1rem; background-color: #2563eb; color: #ffffff; border-radius: 0.375rem; font-weight: 600; }
Convert each property. The padding of 0.5rem vertical and 1rem horizontal becomes py-2, because 0.5 divided by 0.25 is 2, and px-4, because 1 divided by 0.25 is 4. The background colour #2563eb is Tailwind's blue-600, so it becomes bg-blue-600. The white text becomes text-white. The border radius of 0.375rem is Tailwind's rounded-md. The font weight of 600 is semibold, so it becomes font-semibold.
The converted markup is:
Check the mapping: py-2 gives 0.5rem vertical padding, matching the original, and px-4 gives 1rem horizontal padding, matching the original. The colours, radius, and weight all match their CSS values. The button looks the same, but its styling now lives in utilities, so changing it means editing the class list, not hunting through a stylesheet.
Cases that do not convert cleanly
Some CSS does not have a utility equivalent. Complex selectors, such as styling a child on parent hover, need Tailwind's variant syntax, such as group-hover, or a custom component class. Animations with custom keyframes need configuration, not just utilities. And highly specific designs, with many off-scale values, convert into a mess of arbitrary values that is harder to maintain than the original CSS.
In these cases, the pragmatic approach is a hybrid: convert the bulk of the styling to utilities, and keep genuinely custom rules as component classes or configuration. Tailwind supports this explicitly, with directives for adding component classes alongside utilities. A hybrid that is honest about its custom parts beats a forced conversion that hides complexity in arbitrary values.
Another caution concerns responsive design. Tailwind's responsive prefixes, such as md: and lg:, apply utilities at breakpoints, so a CSS media query that changes padding on large screens becomes md:px-8 alongside a base px-4. Converting responsive rules means mapping each breakpoint to its prefix, and checking that the breakpoints match: a custom breakpoint of 900 pixels does not equal Tailwind's default md breakpoint of 768 pixels, and the difference will show.
Related tools
Styling conversions often need companion tools. The Color Converter translates between hex, RGB, and other colour formats, which helps when matching a design's colours to a palette, and the Regex Tester is useful for finding CSS patterns across a large stylesheet. For a related guide on fluid type scales, see the CSS Clamp Calculator Guide.
Converting to Tailwind is mostly a mapping exercise: learn the spacing scale, match colours to the palette or define custom ones, translate layout properties directly, and keep genuinely custom rules as components. Do that, check the result visually against the original, and the converted design will look the same while becoming easier to change.