Posted on / by Vivan Web Solution / in Uncategorized

How to implement tailwind CSS

In the ever-evolving world of web development, Tailwind CSS has emerged as a powerful and flexible utility-first CSS framework. It allows developers to build custom designs without leaving their HTML, making the development process more efficient and maintainable. In this blog, we’ll walk you through the steps to implement Tailwind CSS in your project, whether you are starting from scratch or integrating it into an existing setup

Tailwind CSS is a utility-first CSS framework that makes it easy to build custom designs directly in your HTML. The simplest and fastest way to get up and running with Tailwind CSS from scratch is by using the Tailwind CLI tool. The CLI is also available as a standalone executable, allowing you to use it without the need to install Node.js.

In this blog post, we’ll walk you through the process of setting up Tailwind CSS using the CLI tool.

1. Install Tailwind CSS

First, you’ll need to install Tailwind CSS via npm and create your tailwind.config.js file.

Open your terminal and run the following commands:

npm install -D tailwindcss
npx tailwindcss init

2. Configure Your Template Paths

Next, you need to configure the paths to all your template files. This helps Tailwind CSS to purge unused styles in production, making your CSS file as small as possible.

Edit your tailwind.config.js file to include the paths to your HTML and JavaScript files:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

3. Add the Tailwind Directives to Your CSS

Create a CSS file (e.g., src/input.css) and add the @tailwind directives for each of Tailwind’s layers:

/* src/input.css */

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

4. Start the Tailwind CLI Build Process

Now, you need to run the CLI tool to scan your template files for classes and build your CSS.

Run the following command in your terminal:

npx tailwindcss -i ./src/input.css -o ./src/output.css --watch

This command tells Tailwind to take src/input.css as input and output the processed CSS to src/output.css. The --watch flag makes sure Tailwind rebuilds your CSS whenever you make changes to your template files.

5. Start Using Tailwind in Your HTML

Finally, include your compiled CSS file in your HTML and start using Tailwind’s utility classes to style your content.

Here’s an example of what your src/index.html file might look like:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="./output.css" rel="stylesheet">
</head>
<body>
  <h1 class="text-3xl font-bold underline">
    Hello world!
  </h1>
</body>
</html>

Conclusion

And that’s it! You’ve successfully set up Tailwind CSS using the CLI tool. Now you can start using Tailwind’s utility-first classes to style your HTML. This setup ensures a smooth and efficient development process, allowing you to focus more on building your design and less on writing custom CSS.

Leave a Reply

×