How to use the css to tailwind converter
- Paste CSS declarations (
display: flex; padding: 16px;) or full rules with selectors into the CSS box. - Read the classes. For plain declarations you get one class list, and for rules you get one line per selector, with
hover:,md:and similar variants added. - Check the note for arbitrary values, which are written as
[..], and for declarations that were not converted. - Copy the classes into your
classorclassNameattribute and remove the old CSS.
How this CSS to Tailwind converter picks classes
Each declaration is matched against the Tailwind CSS v3 default theme. The converter reads the number and unit, turns rem into pixels at 16px, and looks for an exact match in the spacing scale, the palette, the font size scale, the radius scale or the shadow list. padding: 16px and padding: 1rem both become p-4, and background-color: #3b82f6 becomes bg-blue-500.
When no exact utility exists, the value is kept as written in square brackets, for example p-[13px] or bg-[#123456]. A property Tailwind has no utility for becomes an arbitrary property such as [animation:spin_1s_linear_infinite]. Nothing is rounded to a nearby value, so the generated classes produce the same CSS as your input.
Spacing scale: pixels to Tailwind classes
The default spacing scale is the same for padding, margin, gap, width, height and inset. A step of 1 is 0.25rem, which is 4px.
| CSS value | rem | Class (padding shown) |
|---|---|---|
| 4px | 0.25rem | p-1 |
| 8px | 0.5rem | p-2 |
| 12px | 0.75rem | p-3 |
| 16px | 1rem | p-4 |
| 20px | 1.25rem | p-5 |
| 24px | 1.5rem | p-6 |
| 32px | 2rem | p-8 |
| 40px | 2.5rem | p-10 |
| 48px | 3rem | p-12 |
| 64px | 4rem | p-16 |
Margins and inset values can be negative, which gives -mt-4 or -left-2. A value of auto gives mx-auto. Shorthands collapse: margin: 0 auto becomes my-0 mx-auto, and four equal sides become a single class.
Colors, font sizes and borders
Colors are matched by their exact hex value in the default palette. Short hex, uppercase hex, rgb() and hsl() are all normalized first. A color with alpha becomes an opacity modifier, so rgba(0, 0, 0, 0.5) is bg-black/50.
| Hex | Tailwind color |
|---|---|
#3b82f6 | blue-500 |
#ef4444 | red-500 |
#22c55e | green-500 |
#f59e0b | amber-500 |
#6b7280 | gray-500 |
#8b5cf6 | violet-500 |
| Font size | Class | Line height set by the class |
|---|---|---|
| 12px | text-xs | 16px |
| 14px | text-sm | 20px |
| 16px | text-base | 24px |
| 18px | text-lg | 28px |
| 20px | text-xl | 28px |
| 24px | text-2xl | 32px |
| 30px | text-3xl | 36px |
| 36px | text-4xl | 40px |
Note that text-sm sets a line height as well as a font size. If your CSS has only font-size, the converter adds a note, and text-[14px] changes the size alone. Border radius maps 4px to rounded, 6px to rounded-md, 8px to rounded-lg and 9999px to rounded-full.
Selectors, pseudo-classes and media queries
When the input has rules, the converter groups classes by selector. A simple class selector with pseudo-classes becomes variants: :hover is hover:, :first-child is first:, and ::before is before:. A min-width media query that matches a breakpoint becomes sm:, md:, lg:, xl: or 2xl: (640, 768, 1024, 1280 and 1536 pixels). Other widths use min-[500px]:. Selectors with descendant combinators cannot be expressed as a variant, so the converter lists the classes and tells you to put them on the matching element.
!important becomes the ! modifier, as in !text-white. If a property is repeated in one rule, only the last value is kept, as in the cascade.
Tailwind to CSS: reading classes the other way
To go from Tailwind to CSS, look up the class in the default theme. This converter works only in the CSS to Tailwind direction, but the table shows what common utilities expand to.
| Tailwind class | CSS |
|---|---|
p-4 | padding: 1rem |
mx-auto | margin-left: auto; margin-right: auto |
w-1/2 | width: 50% |
text-sm | font-size: 0.875rem; line-height: 1.25rem |
font-bold | font-weight: 700 |
rounded-lg | border-radius: 0.5rem |
opacity-50 | opacity: 0.5 |
z-10 | z-index: 10 |
If your project customizes tailwind.config.js, the class names for your own spacing or colors can differ from these defaults. For the stylesheet side, the CSS formatter and CSS minifier help, and the Tailwind gradient generator builds gradient classes. Coming from a preprocessor? Compile first with the LESS to CSS converter or the SCSS to CSS converter.
Worked examples
| Example | Input | Result |
|---|---|---|
| Button rule with hover and breakpoint | CSS: .btn { / display: inline-flex; / align-items: center; / padding: 8px 16px; / font-size: 14px; / font-weight: 600; / color: #ffffff; / background-color: #3b82f6; / border-radius: 6px; / box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05); / transition: all 0.2s ease-in-out; / } / .btn:hover { background-color: #2563eb; } / @media (min-width: 768px) { / .btn { padding: 12px 24px; } / } | Utility classes: 16; Arbitrary values: 1; Not converted: 0 |
| Declarations only | CSS: display: flex; / align-items: center; / justify-content: space-between; / gap: 16px; / padding: 24px; / background-color: #f9fafb; | Utility classes: 6; Arbitrary values: 0; Not converted: 0 |
| Values that need arbitrary classes | CSS: width: 237px; / margin-top: 13px; / color: #123456; / font-size: 15px; / animation: spin 1s linear infinite; | Utility classes: 5; Arbitrary values: 5; Not converted: 0 |
| Card with border and shadow | CSS: .card { / border: 1px solid #e5e7eb; / border-radius: 12px; / box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1); / padding: 24px; / max-width: 448px; / } | Utility classes: 7; Arbitrary values: 0; Not converted: 0 |
FAQ
How do I convert CSS to Tailwind?
Paste the CSS into the converter. Each declaration is matched to a Tailwind v3 utility, such as display: flex to flex and padding: 16px to p-4, and values without an exact match become arbitrary values in square brackets.
Does it use Tailwind v3 or v4 class names?
Tailwind v3, using the default theme. A project that customizes its theme, or that uses v4 with a different default scale, can need different class names.
What is an arbitrary value?
It is a class with the exact value in square brackets, like w-[237px] or bg-[#123456]. Tailwind v3 generates the CSS for it on demand, so the result matches your original declaration.
Can I convert Tailwind to CSS with this tool?
No, it converts CSS to Tailwind. To go back, look the class up in the theme. The table above shows the CSS for common utilities.
Why does text-sm appear for a plain font-size?
text-sm is the exact class for 14px, but it also sets line-height: 1.25rem. When you want only the font size, use the arbitrary form such as text-[14px].
Is my CSS uploaded?
No. The conversion runs in your browser and nothing is sent to a server.
Sources and references
- Tailwind CSS v3 default theme: spacing, colors, font sizes and shadows used for the mapping