How to Setup and Start Using TailWind
Tags: tailwindcss, html, css, javascript
Published: 2022-05-05 13:48:40

Tailwind has grown massively in popularity the past couple years for its ability to streamline front-end design for web pages.

TailwindCSS is a utility-first CSS framework. This means it provides users with a set of CSS helper classes to build their HTML pages. In addition to these CSS classes, Tailwind provides a complete package with the CLI and various theming and configuration options. These options enable users to adapt and scale this framework for any development need. In today’s article, we’ll see how to get started with TailwindCSS.

---

###Installation and Configuration

The simplest way to install Tailwind is [using the Tailwind CLI,](https://tailwindcss.com/docs/installation) which can be installed as a node package:

`npm install tailwindcss`

**Step 2 - Include Tailwind in the CSS file**

Next, include @tailwind directives for the base, components, and utilities in the primary CSS file of your project. These directives will be converted into the appropriate Tailwind CSS code during the build process of Tailwind.

`@tailwind base;
@tailwind components;
@tailwind utilities;
`

Now we’ll generation a configuration file to allow users to customize TailwindCSS. For instance, you can include additional classes and settings in the CSS. We can use the following command to [generate the tailwind.config.js file](https://tailwindcss.com/docs/configuration#creating-your-configuration-file):

`npx tailwindcss init`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1y8tuyskd0onkn3ssdc.png)

Next, modify the content section of the config file to include all the template files of your site. The following configuration will account for all the HTML and JS files in the src folder.

{% embed https://gist.github.com/b6aecb4c9bf95f52cb59a5e503d6acec.git %}

---

###TailwindCSS Build Process

Now we need to build our css to process all the defined CSS files and insert the styles according to the configurations.
 
The build process can be carried out either using [PostCSS, which enables users to transform CSS with JavaScript,](https://github.com/postcss/postcss#usage) or using the Tailwind CLI. We will use the Tailwind CLI to carry out this build process. Users can directly run the following command in the CLI.:

`npx tailwindcss -i css/main.css -o public/main.css --watch`

The -i flag indicates the input CSS file, while -o flag indicates the output CSS file. The –watch flag will continuously watch the file and rebuild the CSS with each change.

Since we have provided the content path in the tailwind.config.js file, TailwindCSS will watch the files within the provided location and create only the CSS classes referenced in these files. It helps to reduce the size of the CSS files as they include only the absolute necessary classes.

Users will need to carry out this build process each time a style change occurs. So a better way to manage it is to add this command to the [script section of the package.json files. ](https://nodejs.dev/learn/the-package-json-guide#scripts)Then it will automatically build the CSS files each time we build the application.

"scripts": {
"build": "npx tailwindcss -i css/main.css -o public/main.css"
},

This will ensure that tailwind will generate the necessary CSS files at the build stage.

---
###Iterating TailwindCSS with HTML File

The final step for using TailwindCSS is to integrate the built CSS file within your HTML. The only requirement for this step is to link the newly created CSS file as a stylesheet.
 
Have a look at the following code block. There, we have added the CSS file and configured some classes within the HTML elements to get the necessary styling.

**Index.html**

{% embed https://gist.github.com/f2ab67165cd43ce6de9d943717b1f886.git %}

If we look at the live preview of the page now, you can see that all the referenced CSS styles are applied to the HTML file.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ir4jb7hkb8d6ir0oego.png)

That's it, and we have created a styled HTML page using Tailwind! These predefined classes provide the flexibility to facilitate any design without adhering to any framework-specific workflows or configurations

What are you going to use TailWind for? Let us know down below!

As always, happy coding from your friends at [Codesphere](https://link.codesphere.com/LL), the all-in-one development platform!