Tailwind CSS is a powerful and flexible CSS framework that is characterized by a functional approach to styles. Rather than providing pre-built components, as many traditional frameworks (like Bootstrap) do, Tailwind CSS offers many utilitarian classes that allow you to build interfaces from scratch by combining these classes.

This approach, called “functional CSS,” gives developers incredible freedom, minimizing the need to write custom styles for each element and improving performance. In this article, we’ll look at the basic principles of Tailwind CSS and how to start using it in your project.

What is a functional approach?

The functional approach in CSS is to use small, over-used classes to specify styles. In contrast to traditional approaches, where styles are written from scratch for each element, Tailwind offers a set of classes that can be combined to get the style you want.

Example of a traditional approach:

Welcome
.header {
font-size: 2rem;
color: #333;
padding: 10px;
}

With this example, to apply these styles to another element, you would need to create a new class with the same styles.

Tailwind CSS Example:

Welcome

Here, instead of creating a new class in a separate CSS file, you apply pre-made classes directly in the HTML markup. The text-2xl, text-gray-800, and p-2 classes are responsible for font sizes, text color, and indentation.

Steps to get started with Tailwind CSS

Installing Tailwind CSS

There are several ways to install Tailwind CSS into a project. The easiest way is to use a CDN, but for larger projects it is recommended that you install Tailwind via npm and set up a build using PostCSS.

To install Tailwind using npm, run the following commands in your project:

npm install -D tailwindcss
npx tailwindcss init

This will create the tailwind.config.js file, where you can customize the framework to suit your project’s needs.

Customization files

Create a basic CSS file, such as styles.css, and add the base directives for Tailwind there:

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

These directives will connect the base level styles, components, and utility classes.

CSS generation

Using PostCSS or other builders such as Webpack, you can customize the generation of the final CSS file with the desired classes.

Run the following command to generate the final file:

npx tailwindcss build styles.css -o output.css

This will create an output.css file that will contain only the styles used in your project.

Using Tailwind classes

Now that Tailwind is installed, you can start using its classes in your HTML. For example:

Hello, world!

This element will have a blue background, white text, internal indents, and rounded corners.

Basic Utilities of Tailwind

Colors: Tailwind includes a wide palette of colors that makes it easy to change the colors of the background, text, and other elements.

Example:

Red background

Indents and Sizes: Utilities for indents (margins and padding), widths and heights allow you to precisely control the positioning of elements.

Example:

Element indented

Fonts: Easily change the font, its size, thickness, and line spacing using classes such as text-lg, font-bold, and leading-tight.

Example:

Title

Responsiveness: Tailwind uses classes for adaptability, which allows you to change the style of elements based on the width of the screen.

Example:

Text changes based on screen size

Shadows and Effects: Easily add shadows, transformations and other effects to create interactive elements.

Example:

<button class=“bg-blue-500 text-white p-2 rounded shadow-md hover:shadow-lg”>
  Button
</button>

Benefits of using Tailwind CSS

  • Flexibility and control: You are not limited to pre-built components like in other frameworks. You create everything from scratch with complete freedom.
  • Performance: By using utilitarian classes and minimizing CSS, your project will load faster.
  • Customization: Tailwind gives you the ability to easily customize the framework for your project’s specific needs through a configuration file.
  • Scalability: When working on large projects, Tailwind helps keep your code clean and readable by reducing the number of custom styles.

Conclusion

Tailwind CSS is a great tool for those who want to have full control over styles using a simple and straightforward functional approach. By starting with basic utility classes, you will be able to create beautiful and adaptive interfaces without wasting time writing repetitive CSS styles. Try to start using Tailwind in your next project and you will immediately feel how it speeds up the development process!