Introduction
TailwindCSS is a popular CSS framework for a Web application. It provides various utility classes like flex, pt-4, text-center, and rotate-90 that can be used to build any design, directly in your HTML.
It is different from other CSS frameworks like bootstrap, the major difference is it allows us to automatically remove all unused CSS when building an application, which means our final CSS bundle is the smallest it could possibly be.
In this article, we will see How to use TailwindCSS with Angular?
Angular 11.2 has added support for the TailwindCSS. Before we see that, let's see how we were using TailwindCSS before angular v11.2.
Using TailwindCSS before Angular v11.2
Before angular v11.2, It was a bit difficult to use TailwindCSS in Angular, as We need to manually set up following this,
- Customize the angular build with Angular Custom Builder.
- Requires Custom webpack configuration for processing the tailwindcss. Need to set up postcss-loader in webpack config.
We will not go more into it. Check out
here for more details.
Using TailwindCSS in Angular is Easy from v11.2
We just need to take the following actions to use TailwindCSS in Angular.
Install tailwindcss into the Angular workspace or Project
- npm install -D tailwindcss
or,
Create tailwindcss configuration file
Create a tailwindcss configuration file (tailwind.config.js) in either the workspace root or the project root. A configuration file in the project root takes precedence over one in the workspace root.
You can do this with,
This will create tailwind.config.js with default and empty configuration as shown below,
- module.exports = {
- purge: [],
- darkMode: false,
- theme: {
- extend: {},
- },
- variants: {
- extend: {},
- },
- plugins: [],
- }
When both conditions are met, the Angular CLI will initialize and integrate Tailwind CSS into the stylesheet build pipeline. Both global and component stylesheets will be processed when enabled.
Adding tailwindcss imports in styles.css or styles.scss
If you are using styles.css add the following statements,
- @tailwind base;
- @tailwind components;
- @tailwind utilities;
@tailwind is a custom directive exposed by Tailwind to insert its base, components, utilities styles into styles.css.
If you are using styles.scss, add the following import statements.
- //styles.scss
-
- @import 'tailwindcss/base';
- @import 'tailwindcss/components';
- @import 'tailwindcss/utilities';
We are done with the basic setup, Now let's test it.
Test the TailwindCSS Setup
Copy the following code snippet in your app.component.html.
- <div class="m-5 shadow-md rounded ">
- <form class="flex-auto">
- <div class="flex flex-wrap p-5 bg-gray-50 mb-2">
- <img class="w-14 rounded-full mr-5" src="https://i.pravatar.cc/300">
- <div>
- <h1 class="flex-auto text-xl font-semibold">
- John Doe
- </h1>
- <div class="w-full flex-none text-sm font-medium text-gray-500 mt-2">
- Designer, Developer
- </div>
- </div>
- </div>
- <div>
- <p class="px-6 pb-2 font-semibold text-center text-xl">"Rapidly build modern websites without ever leaving your HTML."</p>
- <p class="px-6 pb-2 text-center">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.</p>
-
- <img class="w-full h-64 object-cover" src="https://images.pexels.com/photos/546819/pexels-photo-546819.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260"/>
- </div>
- <div class="flex">
- <button class="bg-gradient-to-b from-blue-800 to-green-500 p-2 rounded-full text-white m-5 px-5"> 👍 Like</button>
- <button class="bg-gradient-to-b from-green-400 to-blue-500 p-2 rounded-full text-white my-5 px-5"> 📤 Share</button>
- </div>
- </form>
- </div>
Serve your application and verify it on the browser, you will get the following output,
Great!!! you are done with the basic setup of TailwindCSS in Angular.
Purge the unused CSS in the build
By default, all the tailwindcss styles are included in the build, which will increase the bundle size of the application. This is not acceptable for any application,
As you can see below, it creates styles.css of 2.78 MB
in angular build. Let's fix this,
TailwindCSS provides a feature to remove the unused CSS in build for that we need to specify files in purge property of tailwind.config.js as shown below,
-
-
- module.exports = {
- purge: {
- enabled: true,
- content: [
- './src/**/*.{html,ts}',
- ]
- },
- darkMode: false,
- theme: {
- extend: {},
- },
- variants: {
- extend: {},
- },
- plugins: [],
- }
It will check all the component's typescript and HTML files and remove the unused CSS. Take a new build and verify.
Now we have styles.css with only 4.30 kB,
Great !!! We are done with the implementation, testing, and production build optimization.
Summary
In this article, We have seen How to use TailwindCSS in an Angular application? We have seen the step-by-step implementation of it in the angular v11.2 application.
I hope you like this article, if you feel this is useful to you, please like and share 🙂