How to Convert Tailwind HTML to Structured, Semantic CSS (Without a Full Rewrite)
How to Convert Tailwind HTML to Structured, Semantic CSS (Without a Full Rewrite) I've been using Tailwind for about two years across a handful of projects. It's fast, it's predictable, and once the muscle memory kicks i
How to Convert Tailwind HTML to Structured, Semantic CSS (Without a Full Rewrite)
I've been using Tailwind for about two years across a handful of projects. It's fast, it's predictable, and once the muscle memory kicks in you can style things without switching files.
But here's what I kept running into:
That component you built six months ago? The one with β flex items-center justify-between p-4 bg-white rounded-lg shadow-mdβ on the outer div, plus another half-dozen classes on each child element? When you come back to refactor it, you're reading styling before you're reading structure. And if you want to move that component into a project that doesn't use Tailwind β or hand it off to someone who doesn't know the utility vocabulary β you're stuck.
The tooling gap
I went looking for something that could take a chunk of Tailwind-styled HTML and give me back clean, semantic CSS. Not one-to-one class translation (there are a dozen tools that do β px-4 β padding: 1remβ ). I wanted something that understood the component structure β that a button inside a card should become β .card-buttonβ , not β .element-4β .
What I found:
One-to-one converters β great if you just need to know what β gap-4β compiles to, but they output β .element {}β β .div {}β flat rules with no hierarchy
StyleLens (VSCode) β finds duplicate utility patterns and suggests refactoring into β @applyβ , but it's an editor workflow, not an export tool
Tailwind's own β @applyβ β moves the problem from the HTML to the CSS file but doesn't change the naming problem
None of them did what I actually wanted: paste some HTML, get back component-level CSS with meaningful class names.
What I ended up building
Since I couldn't find it, I put together a minimal tool that does exactly that. The idea is simple:
- Parse the HTML tree
- Translate every Tailwind class to its CSS equivalent
- Group declarations by component (the tree structure tells you what belongs where)
- Name things based on what they are β semantic tags like β h2β become β .card-titleβ , buttons become β .card-buttonβ , etc.
Here's what it looks like in practice.
Input:
< img class="w-full h-48 object-cover" src="img.jpg" />
Card Title
Description goes here.
Get Started Output: .card { max-width: 24rem; margin-left: auto; margin-right: auto; background-color: #ffffff; border-radius: 0.75rem; box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); overflow: hidden; }
.card-image {
width: 100%;
height: 12rem;
object-fit: cover;
}
.card-body {
padding: 1.5rem;
}
.card-title {
font-size: 1.25rem;
font-weight: 700;
color: #111827;
}
.card-description {
margin-top: 0.5rem;
color: #4b5563;
}
.card-button {
margin-top: 1rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
background-color: #2563eb;
color: #ffffff;
border-radius: 0.25rem;
}
.card-button:hover {
background-color: #1d4ed8;
}
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.