🥳
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 set up your first Tailwind CSS Project

blogImage

By Emmanuel Chinonso

Technical Writer

Unlike any other CSS framework, Tailwind CSS is not opinionated and allows you to create your components and design your elements for any project. It is not the only utility-first CSS framework, but it’s by far the most popular.

At the end of this tailwind CSS tutorial, you should be able to set up a tailwind CSS project quickly, configure your tailwind CSS by yourself and build a simple card using the tailwind CSS framework.

Table of content

  • Introduction to tailwind CSS
  • Prerequisite
  • Setting up Tailwind CSS to your project
  • Adding tailwind to your stylesheet
  • Configuration and processing your CSS with Tailwind
  • Tailwind CSS Processing
  • Adding Tailwind CSS first project
  • Building a simple card using Tailwind CSS
  • Conclusion

Before we can get on with the tailwind CSS tutorial, below is the image of the project we are going to create.

Introduction to Tailwind CSS

Tailwind CSS was first released on the 1st of November,2017 on Github. It was an open-source project. By the beginning of August 2020, over 10 million people have downloaded the framework.

These made it one of the fastest-growing CSS frameworks. In September 2020, over 1 million developers world-wide accessed the official Tailwind documentation.

Tailwind CSS is different from other CSS frameworks because it’s a utility-first CSS framework, unlike Bootstrap that is an object-oriented one. You can check out my post comparing the two frameworks here. These new ways of building a user interface make the framework highly customizable.

Prerequisite

Meanwhile, before we can continue we are going to look at some of the skills you require to use the tailwind CSS on a project.

  • Basic HTML
  • Basic CSS
  • Basic JavaScript

Setting up Tailwind CSS to your project

There are several ways you can set up your Tailwind CSS project. Although the fastest way is to use the CDN, it is recommended you use the package manager method. You can check out my post on using the CDN method on your Tailwind CSS project.

Using the package manager will allow you to make use of the configuration via PostCSS.

In this tailwind tutorial, I will show you how to include the latest version of Tailwind CSS using NPM and create a Tailwind configuration file. To use this method, you must make sure you have Node and NPM installed in your machine.

Then you can go to your terminal to create your project folder. To create your project folder, run the following command.

1$ mkdir tailwind-project-01

Now, change into the new directory

1$ cd tailwind-project-01

You can now go-ahead to create a new package.json by using the following NPM command

1$ npm init

What the command above does is help us manage the Node Package Manager and the dependencies. The next step is to install the latest version of tailwind CSS and save the dependency. You can do this by running the following command on your terminal.

1npm install @latest tailwindcss –save

This command makes the Tailwind CSS available in our node_modules folder.

Adding tailwind to your stylesheet

After installing the tailwind CSS project, the next step is to inject the tailwind’s base, components, and utility-style into a CSS file you created. This is done by using the custom @tailwind directives. Moreover, you must first create a new CSS file called style.css (or any name of your choice) and add the following lines of code

1@tailwind base;
2@tailwind components;
3@tailwind utilities;

This line of codes is swapped up using Tailwind with the actual CSS styles from Tailwind when processing the file.

Configuration and processing your CSS with Tailwind

As we earlier stated, that the most recommended way to set up the Tailwind CSS project is to install Tailwind CSS using the package manager. This is because you can’t configure your components the way you want when you use other methods However, the package manager allows us to create a configuration file. we can go ahead next to create a configuration file. You can do this by running the following command on your terminal

1npx tailwindcss init

The above command will generate a file called tailwind.config.js. The file will contain the following

1// tailwind.config.js
2Module.exports = {
3future: { },
4purge: { },
5theme: { },
6extend: { },
7 },
8 variants: { },
9 plugins: [ ],
10}

You can go ahead to add the following plugins

1 plugins: {
2 tailwindcss: { },
3 autoprefixer: { },
4 }
5}

Tailwind CSS Processing

At this stage, we can go ahead to compile our style.css file to create the main.css file. The output.css file will be included in our project. To do this, we can go ahead to run the following command npx tailwindcss build main.css –o main.css After running this command, you will see that a new main.css file was generated in your folder structure. This file will hold the default Tailwind CSS utility classes based on the configuration that you first created.

Adding Tailwind CSS first project

We are going to add Tailwind CSS to our first project. You can go ahead to create an index.html file in your root project directory. Inside the HTML file, include the main.css to the head tag of your HTML file project. It should look something like this.

1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5 <meta charset="UTF-8">
6 <meta http-equiv="X-UA-Compatible" content="IE=edge">
7 <meta name="viewport" content="width=device-width, initial-scale=1.0">
8 <link rel="stylesheet" href="styles.css">
9 <title>tailwind cards</title>
10</head>

Excellent, you have successfully added Tailwind CSS to your first project.

In the next section, we will create a simple card using the project we just created and employ some Tailwind CSS utility classes to it.

Building a simple card using Tailwind CSS

In this section, we will demonstrate how to use the Tailwind CSS utility in your project. The simple card we are going to build will look like the image below.

In the index.html we initially created, we will create a wrapper div that will contain the cards.

1<div class="flex space-x-10 justify-center mt-4"></div>

Now that we have created the wrapper, we can go ahead to create cards inside the wrapper.

We are going to include the utility classes in our elements.

1<body>
2<div class="flex space-x-10 justify-center mt-4">
3    <div class="max-w-md py-4 px-8 bg-white shadow-lg rounded-lg my-20">
4        <div class="flex justify-center md:justify-end -mt-16">
5            <img
6 class="w-20 h-20 object-cover rounded-full border-2 border-indigo-500"
7 src="image/profile one.jpg">
8        </div>
9        <div>
10            <h2 class="text-text-3xl font-semibold text-indigo-500">Programmer</h2>
11            <p class="mt-2 text-gray-600">
12 Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repellat
13               asperiores
14               explicabo alias a tenetur amet quae minima neque dicta voluptas eaque recusandae ab, corrupti est.
15            </p>
16        </div>
17        <div class="flex justify-end mt-4">
18            <a href="#"
19 class="text-xl font-medium text-indigo-500">
20 David Paul
21 </a>
22        </div>
23    </div>
24
25    <div class="max-w-md py-4 px-8 bg-white shadow-lg rounded-lg my-20">
26        <div class="flex justify-center md:justify-end -mt-16">
27           <img class="w-20 h-20 object-cover rounded-full border-2 border-indigo-500"
28 src="image/profile two.jpg">
29        </div>
30        <div>
31            <h2 class="text-text-3xl font-semibold text-indigo-500">
32 Designer
33 </h2>
34            <p class="mt-2 text-gray-600">
35 Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repellat
36                asperiores
37                explicabo alias a tenetur amet quae minima neque dicta voluptas eaque recusandae ab, corrupti est.
38            </p>
39        </div>
40        <div class="flex justify-end mt-4">
41            <a href="#" class="text-xl font-medium text-indigo-500">Daphene Coles</a>
42        </div>
43    </div>
44</div>
45</body>
46</html>

Our last project should look like the image below.

Conclusion 

In our Tailwind CSS tutorial, you learned how to install Tailwind CSS using the package manager, how to build a simple card using Tailwind CSS utility classes, and how to include Tailwind CSS in your project.  

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

...