Skip to main content

How to Setup TailwindCSS in Angular?

In this article, we will discuss "How to Setup TailwindCSS in Angular". "TailwindCSS" is known as utility first CSS framework, we can rapidly build modern websites without ever leaving the HTML. TailwindCSS is different from other CSS frameworks like Bootstrap, etc. You can create whatever you want as per the required UI using the given utility class (CSS class).

Setup Angular Project


You can use the following "ng new" command into the terminal to setup the Angular project.
ng new ng-project

You can skill this step if already have a running Angular project.

Install tailwindcss


Install tailwindcss via the following npm command.
npm install -D tailwindcss postcss autoprefixer

Then run the "npx init" command to generate a tailwind.config.js file.
npx tailwindcss init

Configure template paths


Update the following code snippet to tailwind.config.js.
content: [
"./src/**/*.html,ts",
],

Update style.css


Open "scr/style.css" and add the "Tailwind directives to your CSS".
@tailwind base;
@tailwind components;
@tailwind utilities;

Serve Angular Application


Let's serve the Angular app.
ng serve

Test tailwindCSS


Open the app.component.html and add the following code.
<div class="container">
<h1 class="text-3xl font-bold underline bg-black text-white">
Hello world!
</h1>
</div>

Recommendations


Personally, I don't recommend it if you are new to CSS, not because is hard but because it makes you lazier. If you are new, then please write your CSS manually so you can understand what's working behind it. Here, lots of utility (CSS) classes are available which you can add to your HTML to make your desired layout. Also, when we add classes into the HTML. It makes our HTML very dirty. To overcome this issue, you have to put the TailwindCSS classes in your CSS/SCSS files. Please check the following example.
.btn 
@apply py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-red-400

Notice I'm using the "@apply". After that, you can use the ".btn" class only with your HTML. Using this way our HTML looks clean.
<button class="btn">Hello</button>

Install TailwindCSS plugins (Optional)


TailwindCSS provides some additional plugins, that are optional to install as per your requirements.

You can install those plugins using the following npm command.
npm install -D @tailwindcss/typography @tailwindcss/forms @tailwindcss/line-clamp @tailwindcss/aspect-ratio

Configure tailwindCSS plugins


Update tailwind.config.js file as follows.
module.exports = 
theme:
// ...
,
plugins: [
require('@tailwindcss/typography'),
require('@tailwindcss/forms'),
require('@tailwindcss/aspect-ratio'),
require('@tailwindcss/line-clamp'),
// ...
],

Optional fix for error


If you face the following error in creating the build.
⠸ Generating browser application bundles (phase: building)...(node:7416) UnhandledPromiseRejectionWarning: HookWebpackError: Transform failed with 1 error:
error: Invalid version: "15.2-15.3"
at makeWebpackError (E:\ng-project\node_modules\webpack\lib\HookWebpackError.js:48:9)
at E:\ng-project\node_modules\webpack\lib\Compilation.js:3055:12
at eval (eval at create (E:\ng-project\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:54:1)
at processTicksAndRejections (internal/process/task_queues.js:93:5)

You have to add the following to the end of the ".browserslistrc" file located at the root of the project.
not IE 9-10 # Angular support for IE 9-10 has been deprecated and will be removed as of Angular v11. To opt-in, remove the 'not' prefix on this line.
not IE 11 # Angular supports IE 11 only as an opt-in. To opt-in, remove the 'not' prefix on this line.
not ios_saf 15.2-15.3
not safari 15.2-15.3

For more details, you can refer to this angular-CLI issue.

Conclusion


In this article, we are discussing "How to Setup TailwindCSS in Angular". Hope you like this article. Please feel free to add comments if any queries or suggestions.

Keep learning & stay safe :)




You may like:

How to Manage Password Strength – Angular

How to use trackBy in Angular with Example

Comments

Popular posts from this blog

Laravel Logging Guzzle Requests in File

In this article, we will discuss “Laravel Logging Guzzle Requests in File”. In the multiple scenarios, we have to use third-party API’s in our project and test those API using postman or any other tool. But also required to maintain a log for each request, such as what we send (request)? and what we receive (response)? Today, […] Read out the full post at here

How to Use SSH with AWS EC2 Instance?

In this article, we will discuss "How to Use SSH with AWS EC2 Instance?" . As I already explain the setup of EC2 Instance and the use of an Elastic IP in my previous article. Today, we will learn how to connect an EC2 instance using SSH. If still, you have not read my previous articles then I recommend checking them once for a better understanding. Prerequisites A running EC2 Instance . Elastic IP (Optional for this article) ".pem" file, which is downloaded when setup the EC2 Instance. If not having the ".pem" file, then you have to create new key/value pairs. Connect via Terminal or WSL(Window Subsystem for Linux) Open your terminal and go to the directory where you downloaded your ".pem" file. Use the following command to connect with the server. ssh -i "****.pem" username@<publicDNS> or <IP Address> The same terminal command can be used in the windows Linux terminal. I'm using ubuntu on my windows machine...

How to Setup and Install Next.js App?

In this article, we will discuss "How to Setup and Install Next.js App" from scratch. What is React.js and Next.js? "React.js" is a JavaScript library for building user interfaces (elements that users see and interacting on-screen). Basically, React provide us some helpful functions to build UI, and leaves it on us where to use those functions in the application. "Next.js" is a React framework. It is maintained by "Vercel" . Next.js features to solve common application requirements such as routing, data fetching, integrations - all while improving the developer and end-user experience. Why we use Next.js ? Next.js comes with the some additional features to solve come application requirements such as: We can build SSG (Static Site Generation), SSR (Server-Side Rendering) and SPA (Single Page Application) apps. Hot code Reloading: Reload the page when it detects any change saved. Routing: No need to configure any route. Files put in the pages fol...