[Tailwind CSS Angular] Install Tailwind CSS in an Angular project
Tailwind CSS and Angular
Angular is an application design framework and development platform for creating efficient and sophisticated single-page apps.
Tailwind CSS is a utility-first CSS framework packed with classes like flex, pt-4, text-center and rotate-90 that can be composed to build any design, directly in your markup to make rapidly build modern websites without ever leaving your HTML.
Installation
Install Angular CLI
You use the Angular CLI to create projects, generate application and library code, and perform a variety of ongoing development tasks such as testing, bundling, and deployment.
To install the Angular CLI, open a terminal window and run the following command:
1 | npm install -g @angular/cli |
Create a workspace and initial application
You develop apps in the context of an Angular workspace.
To create a new workspace and initial starter app:
Run the CLI command ng new and provide the name my-app, as shown here:
1 | ng new my-app |
The ng new command prompts you for information about features to include in the initial app. Accept the defaults by pressing the Enter or Return key.
The Angular CLI installs the necessary Angular npm packages and other dependencies. This can take a few minutes.
1 | cd my-app |
Install tailwindcss, postcss and autoprefixer
you’ll want to install Tailwind and its peer-dependencies via npm.
1 | npm install -D tailwindcss@latest postcss@latest autoprefixer@latest |
Configuration
If you’d like to customize your Tailwind installation, generate a config file for your project using the Tailwind CLI utility included when you install the tailwindcss npm package:
1 | npx tailwindcss init -p |
This will create a minimal tailwind.config.js
file at the root of your project:
1 | // tailwind.config.js |
And create a minimal postcss.config.js
file at the root of your project too:
1 | module.exports = { |
Learn more about configuring Tailwind in the configuration documentation.
Include Tailwind in your CSS
Create a CSS file if you don’t already have one, and use the @tailwind directive to inject Tailwind’s base, components, and utilities styles:
1 | /* ./your-css-folder/styles.css */ |
Tailwind will swap these directives out at build-time with all of the styles it generates based on your configured design system.
Building your CSS
How you actually build your project will depend on the tools that you’re using. Your framework may include a command like npm run dev
to start a development server.
When building for production, be sure to configure the purge option to remove any unused classes for the smallest file size:
1 | // tailwind.config.js |
See Removing unused CSS - https://tailwindcss.com/docs/optimizing-for-production#removing-unused-css to learn how to purge all the unused classes.
Read our separate guide onoptimizing for production - https://tailwindcss.com/docs/optimizing-for-production to learn more about tree-shaking unused styles for best performance.
FAQS
initial exceeded maximum budget
1 | npm run build:ssr |
Configure the purge option to remove any unused classes for the smallest file size:
1 | // tailwind.config.js |
References
[5] Optimizing for Production - Tailwind CSS - https://tailwindcss.com/docs/optimizing-for-production
[7] Angular - https://angular.io/
[8] postcss/postcss: Transforming styles with JS plugins - https://github.com/postcss/postcss