TailwindPHP
A port of TailwindCSS 4.x to PHP. Generate Tailwind utility CSS in PHP, with no Node.js and no build step. Ships cn(), tailwind-merge, CVA, the typography and forms plugins, and a CLI.
composer require tailwindphp/tailwindphpLatest release v1.7.0 MIT PHP 8.2+
use function TailwindPHP\merge;
merge('px-2 py-1 bg-red-500', 'px-4 bg-blue-500');
// => 'py-1 px-4 bg-blue-500'
// Conflict resolution is variant-aware
merge('hover:bg-red-500', 'hover:bg-blue-500');
// => 'hover:bg-blue-500'In practice
Pass markup and optional input CSS to one PHP call and get back a stylesheet.
Generate CSS from PHP
tw::generate() scans the markup for class names and returns only the CSS those classes need. The second argument takes input CSS, so a color defined in @theme becomes a utility.
cn() builds class strings and resolves Tailwind conflicts, so the last conflicting utility wins.
use TailwindPHP\tw;
$css = tw::generate(
'<div class="bg-brand p-4">Hello</div>',
'@import "tailwindcss"; @theme { --color-brand: #3b82f6; }'
);
// .bg-brand now resolves to var(--color-brand)use function TailwindPHP\cn;
cn('px-2 py-1', 'px-4'); // 'py-1 px-4'
cn('btn', ['btn-primary' => true]); // 'btn btn-primary'Pure PHP
No Node.js and no build step. Runs on PHP 8.2+ with the standard mbstring and ctype extensions. composer require and go.
Tracks TailwindCSS v4.3.3
A port of Tailwind v4.3.3 with the same utilities, variants and directives. The known differences from Tailwind are documented.
4,400+ tests
Output is verified against TailwindCSS's own test suites, extracted from the TypeScript source and re-run on every push.
Every utility & variant
The full utility set and every variant: hover, focus, responsive, dark mode, container queries, and arbitrary values.
CSS directives
@import, @theme, @apply, @utility, @custom-variant, @layer, @plugin and @source, parsed and resolved in PHP.
Runtime generation
Generate utility CSS on the fly from HTML, database content, or template variables. No build step required.
Batteries included
cn(), merge(), join(), and variants(): clsx, tailwind-merge, and CVA ported to PHP, with no extra packages.
Plugins
@tailwindcss/typography and @tailwindcss/forms ported to PHP, plus a PHP plugin API for your own utilities and components.
CLI
bin/tailwindphp builds a stylesheet from an input CSS file with -i, -o, --watch and --minify.
Inspect the theme
Read raw and computed property values, colors, breakpoints, and spacing for any class straight from PHP.
Cache and minifier
A built-in CSS minifier and a file cache with TTL keep repeated builds fast and output lean.
WordPress & PHP frameworks
Ship Tailwind-based themes and plugins with no Node.js in the stack. TailwindPHP is a single Composer dependency.
Why TailwindPHP
One runtime in production
A PHP project that adopts Tailwind usually also takes on Node.js, an npm install and a build step in CI and Docker. TailwindPHP compiles the CSS inside the PHP process instead.
CSS for content known at runtime
Markup from a database, user input or a template variable can go straight to tw::generate(), which returns only the CSS its classes need.
Checked against a pinned Tailwind release
Test cases are extracted from the Tailwind source pinned in the repository and run against the PHP output, so drift fails CI. Release 1.7.0 was checked against Tailwind v4.3.3 with 4,427 tests.
Per-page CSS from one compiler
Compile the theme once with tw::compile(), then call generateExact() for each page. Each call returns what a cold tw::generate() of that page would produce, without parsing the theme again.
Compared with
Tailwind Labs ships its own ways to build Tailwind CSS. This is how TailwindPHP differs.
| Tool | Good at | Difference with TailwindPHP |
|---|---|---|
| Tailwind CLI (@tailwindcss/cli) | Good atTailwind's own command-line tool, installed from npm. It scans your HTML files, components and templates for class names and writes the matching styles to a static CSS file. | Difference with TailwindPHPTailwindPHP compiles inside PHP with no Node.js or npm install. It tracks a fixed Tailwind release (v4.3.3 in 1.7.0), and its own benchmark shows the TypeScript original running the low-level operations faster. |
| Tailwind standalone CLI | Good atThe Tailwind CLI as a standalone executable, for projects that want to use it without installing Node.js. | Difference with TailwindPHPThe executable is a separate binary that runs as a build step. TailwindPHP is a Composer package your PHP code calls, so it can also generate CSS at request time from database content or template variables. |
| Tailwind Play CDN | Good atA script tag that runs Tailwind in the browser, for trying it without any build step. Tailwind's docs say it is designed for development and not intended for production. | Difference with TailwindPHPTailwindPHP needs no build step either, but it generates the CSS in PHP on the server and returns it as a string, for example to write to a stylesheet. |
Sources, checked
Questions and answers
Short answers from the TailwindPHP documentation.
Release 1.7.0 tracks Tailwind CSS v4.3.3. Its test suites were re-extracted against that version.
The output is tested against cases extracted from Tailwind's own test files. The 1.7.0 changelog records a few serialization differences, for example .p-0 keeps padding: 0px where the reference CLI emits padding: 0.
No. Plugins are PHP: the typography and forms plugins are ported, and you can write your own against the PHP plugin API and register it with registerPlugin(). A @plugin name that is not registered throws an error.
In the project's benchmark on PHP 8.4.5 and an Apple M1, full-page generation took roughly 14 ms per page. For repeated input the file cache skips compilation, and a long-lived compiler with generateExact() reuses the parsed theme.
No. IntelliSense, autocomplete, class sorting and source maps are left out on purpose. TailwindPHP only generates CSS.
PHP 8.2 or later with the ctype and mbstring extensions. composer.json declares the PHP version, and CI runs the test suite on PHP 8.2, 8.3 and 8.4.
Install the package with Composer and follow the getting started guide to generate your first stylesheet from PHP.