This guide walks you through getting started with TailwindCSS v4 programmatic API.
npm install tailwindcss@4.1.18The core API is the compile() function that processes CSS with TailwindCSS features.
import { compile } from 'tailwindcss';
// Define your CSS with TailwindCSS directives
const css = `
@theme {
--color-primary: #3b82f6;
--color-secondary: #8b5cf6;
}
.button {
@apply bg-primary text-white px-4 py-2 rounded;
}
`;
// Compile the CSS
const result = await compile(css, {
base: process.cwd(),
});
// Build final CSS with candidate classes found in your HTML/templates
const candidates = ['bg-primary', 'text-white', 'px-4', 'py-2', 'rounded'];
const output = result.build(candidates);
console.log(output);When using @apply, you need to either:
Example with @theme:
const css = `
@theme {
--color-primary: #3b82f6;
}
.button {
@apply bg-primary text-white;
}
`;
const result = await compile(css);Example with @source:
const css = `
@source "./src/**/*.{js,jsx,ts,tsx}";
.button {
@apply bg-blue-500 text-white px-4 py-2;
}
`;
const result = await compile(css);Plugins extend TailwindCSS with custom utilities, variants, and components.
import plugin from 'tailwindcss/plugin';
const myPlugin = plugin(function ({ addUtilities, theme }) {
addUtilities({
'.scrollbar-hide': {
'-ms-overflow-style': 'none',
'scrollbar-width': 'none',
'&::-webkit-scrollbar': {
display: 'none',
},
},
});
});
// Use in configuration
export default {
plugins: [myPlugin],
};Create a tailwind.config.js file:
export default {
content: ['./src/**/*.{js,jsx,ts,tsx}'],
theme: {
extend: {
colors: {
primary: '#3b82f6',
secondary: '#8b5cf6',
},
},
},
plugins: [],
};