Skip to content

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.

Terminal
composer require tailwindphp/tailwindphp

Latest release v1.7.0 MIT PHP 8.2+

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.

PHP
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)

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.

TailwindPHP compared with other tools
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.

Questions and answers

Short answers from the TailwindPHP documentation.

Install the package with Composer and follow the getting started guide to generate your first stylesheet from PHP.