🥳
WindFrame is here: Design, prototype and code
sale icon End of Summer Sale 🎁 Get Contrast PRO UI library + admin template with 5 admin dahsboards, 23 pages and 10000+ components at 60% off 🎉
postImage

How to use Tailwind CSS in HTML

blogImage

By Emmanuel Chinonso

Technical Writer

Tailwind CSS is a utility-first CSS framework. It provides you with CSS classes that you can use to create designs for your web pages quickly and with ease.

This blog will walk you through the different ways you can add the Tailwind CSS framework in your HTML project, what the limitations could be, what developers that have worked extensively with the Tailwind CSS framework recommend and why, and finally how to properly process Tailwind CSS.

Table of content

  • Adding tailwind CSS CDN
  • Steps in installing tailwind CSS using package manager
  • Adding tailwind CSS using the package manager
  • Installing Node
  • Installing the latest Tailwind CSS
  • Adding tailwind CSS directives to a CSS file
  • Setting up Tailwind configuration file
  • Tailwind CSS Processing
  • Conclusion

There are usually two ways of adding the Tailwind CSS framework in your HTML project.

  • Using the Tailwind CSS CDN
  • Adding Tailwind CSS via the package manager and using PostCSS to process the configuration file.

Tailwind CSS using the CDN

Adding the Tailwind CSS in your project via the CDN is easier and very beginner friendly. This method allows you to use the Tailwind CSS framework without the need to worry about PostCSS configurations. It is great way to jumpstart your project. However with these perks comes some limitations, some of these limitations are:

  • You cannot install third-party plugins
  • You cannot customize Tailwind's default theme.
  • You get access to all the files even the files you do not need.
  • You cannot use any directives like @apply, @variants.
  • You cannot enable additional variants like group-focus.

For the Tailwind's styles to work properly and a great experience with the CSS framework you need to use the HTML5 doctype and include the meta tags for responsiveness on all devices

1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <title>Document</title>
8</head>
9<body>
10
11</body>
12</html>

To use the Tailwind via the CDN in your project, add this at the head section of your HTML project.

1<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.CSS" rel=" stylesheet">

With this we get access to the Tailwind CSS classes that allows us to use it's predefined styles in our project.

Tailwind CSS using the Package Manager.

Now, I would suggest if you are building a world application it would be best to install your tailwind CSS using the package manager. This is because most frameworks make use of PostCSS already like the autopicker.

In this part of the tailwind CSS tutorial, we are going to look at how to install Tailwind CSS using the package manager.

Prerequisites

To get the most out of this section of the tutorial, you need the following:

  • Node and it’s package manager, npm. You can install it from here. Run the command node -v && npm -v to verify you have them installed.
  • Alternatively, we can use another package manager, Yarn.

Steps in installing tailwind CSS using package manager

  • Creating our package.json file
  • Install Tailwind CSS
  • Adding tailwind CSS directives to a CSS file
  • Setting up Tailwind configuration file
  • Tailwind CSS Processing

Creating our package.json file

A package.json file is a file that lives in the root directory of our project. It holds important information about your project like the project's name, the project's description, dependencies required by the project and how npm should run your project etc.

To create a package.json file in your project, go to your terminal, change the file directory to the folder you want your package.json file to be created in.

To change file directory run this command in yor terminal.

1cd <path to your folder>

Next we run this npm or yarn ( if you decided to use the yarn package manager ) command.

1npm init
2
3# or
4
5yarn init

With this you would be asked a couple questions, you can skip these questions by pressing the enter key.

Installing latest Tailwind CSS

Next, we install the tailwind CSS along with the latest PostCSS and autoprefixer using the command below

1npm install -D tailwindcss@latest postcss@latest autoprefixer@latest

Adding tailwind CSS directives to a CSS file

The next step in this tailwind CSS tutorial is to create a file called main.css in the root folder of your application and write the following code to give your application the tailwind CSS utilities.

1/* ./your-css-folder/main.css */
2
3@tailwind base;
4@tailwind components;
5@tailwind utilities;

Setting up Tailwind configuration file

The chances are that you would want to use your configuration when building your application. The configuration file makes it easy to customize the classes in Tailwind CSS by changing any fonts, colour, spacing, etc.

To override the default classes when using Tailwind, run the following command in your terminal to create a tailwind.config.js file.

1npx tailwind css init

In the new config file you can go ahead to configure them by writing the following code on them.

1// tailwind.config.js
2module.exports = {
3  purge: [],
4  darkMode: false, // or 'media' or 'class'
5  theme: {
6    extend: {},
7  },
8  variants: {},
9  plugins: [],
10}

Tailwind CSS Processing

The final step is to include the Tailwind CSS in the HTML file using the package manager is to add plugins inside the configuration file and run them, which will process the configuration file and the style.css file.

1module.exports = {
2  plugins: {
3    tailwindcss: {},
4    autoprefixer: {},
5  }
6}

Now, you can go ahead to run the following command on your terminal.

1next tailwind CSS build style.css –o output.css

This will generate a new CSS file called output.css. We then include this output.css file in our HTML file, the same way we include a normal CSS file by adding this line of code

1<link href = “./output.CSS” rel = “stylesheet”>

With that, you can now use Tailwind CSS in HTML files. you can go ahead to build your applications.

Conclusion

In this Tailwind CSS tutorial, we covered how to install Tailwind CSS on your HTML file using different methods like through the CDN and Package manager. I know you will be able to easily jump start using this framework in a matter of minutes.

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro

ad-banner

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts

Comments

...