How to use Tailwind CSS with Angular
Integrating Tailwind CSS with Angular provides utility-first styling, rapid development, and consistent design systems for modern web applications. As the creator of CoreUI, a widely used open-source UI library, I’ve integrated Tailwind CSS in numerous Angular projects for rapid prototyping, theme customization, and responsive design in enterprise applications. From my expertise, the most effective approach is to install Tailwind CSS and configure it with Angular’s build system. This method provides access to Tailwind’s utility classes while maintaining Angular’s component architecture and build optimization features.
Install Tailwind CSS and configure it with Angular CLI for utility-first styling in components.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
# Configure tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,ts}"],
theme: { extend: {} },
plugins: []
}
# Add to src/styles.scss
@tailwind base;
@tailwind components;
@tailwind utilities;
Tailwind CSS integrates seamlessly with Angular CLI’s build system through PostCSS. Install Tailwind as a development dependency and initialize the configuration file. Configure the content
array to include all HTML and TypeScript files for proper purging. Add Tailwind directives to your global stylesheet (styles.scss). Use utility classes directly in Angular templates: <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
. Angular’s build process automatically processes Tailwind directives and purges unused styles for optimized bundles.
Best Practice Note:
This is the same approach we use when integrating Tailwind with CoreUI Angular projects for rapid customization.
Create custom component classes using @apply
directive in component stylesheets: .btn-primary { @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded; }
for reusable component styling.