The website you're referring to — Tailwind Play — is a special, customized environment created by Tailwind Labs to showcase and experiment with TailwindCSS in an in-browser code editor. It allows you to write and test TailwindCSS classes directly in an editor and see the results in real-time.
-
Live Code Editor:
- Tailwind Play has a live editor where users can input HTML and Tailwind utility classes. The platform instantly renders the results in an embedded preview pane.
- This is a custom-built environment where they use JavaScript to interpret the Tailwind classes you type and dynamically apply them in real-time.
-
On-Demand TailwindCSS Generation:
- As you type utility classes in Tailwind Play, it dynamically generates the CSS required for those classes.
- TailwindCSS is not precompiled — it's generated on the fly using Tailwind's JIT (Just-In-Time) engine. This compiles the classes in real-time and injects them into the page.
-
JIT Mode in Tailwind Play:
- TailwindCSS JIT allows for real-time utility class compilation. As you add new classes like
bg-red-500orp-4, Tailwind Play regenerates the styles and immediately applies them.
- TailwindCSS JIT allows for real-time utility class compilation. As you add new classes like
-
Custom JavaScript Handling:
- The platform has custom JavaScript that binds the input from the editor to a TailwindCSS parser. As you add classes, Tailwind Play dynamically regenerates and applies the CSS without a rebuild step.
-
Custom Integration:
- Tailwind Play is an in-browser real-time sandbox, unlike a traditional Next.js setup. It behaves like a code playground or IDE, where you're directly manipulating the DOM with Tailwind classes in real-time.
You're trying to mimic Tailwind Play, but your setup with Next.js + TailwindCSS differs because:
- Tailwind Play dynamically generates and applies classes in real-time.
- Your Next.js + TailwindCSS project builds the styles during the build or dev process, and you'll need to use JIT mode to mimic real-time CSS generation.
The closest way to mimic Tailwind Play in a production app like Next.js is by enabling Tailwind JIT mode. This compiles only the CSS for the classes you use and updates them as you modify your code.
-
Update
tailwind.config.js: Add the following to enable JIT mode in your Tailwind config:// tailwind.config.js module.exports = { mode: 'jit', purge: ['./src/**/*.{js,ts,jsx,tsx}', './public/index.html'], theme: { extend: {}, }, plugins: [], };