DEV Community

Cover image for The Comprehensive Guide to Tailwind CSS
Royal Jain for CodeParrot

Posted on • Originally published at 10xdev.codeparrot.ai

The Comprehensive Guide to Tailwind CSS

WHAT IS TAILWIND CSS?

Tailwind CSS is a utility-first framework designed for swift application development. It enables users to create custom components by directly embedding utility classes in HTML, eliminating the need for separate CSS. With classes like text-xl and bg-primary, developers control design elements like text size and background color effortlessly. Tailwind promotes rapid prototyping, allowing users to focus on crafting visually appealing interfaces without extensive CSS writing. The framework's responsive design is achieved through breakpoints and responsive utility classes (sm:, md:, lg:, xl:). Overall, Tailwind CSS streamlines development by offering a vast set of utility classes for layout, color, spacing, and more. Its efficiency and customization options make it a popular choice among developers.

BENEFITS OF TAILWIND CSS

  • Less Custom CSS: Tailwind allows you to style elements by applying existing classes directly in HTML, reducing the need for writing extensive custom CSS.

  • Small CSS Files: With Tailwind's utility classes, most styles are reusable, keeping CSS files small and preventing them from becoming overly complex as you add features and components.

  • No Class Name Invention: Tailwind provides a predefined set of classes in its design system, eliminating the need to invent or agonize over specific class names for styles and components.

  • Safer Changes: Unlike traditional CSS changes that may affect the entire site, Tailwind's utility classes in HTML are local. This enables safer modifications without worrying about unintentional impacts on other parts of the site.

WHY TAILWIND CSS?

Imagine you're building a house. Frameworks like Bootstrap and Materialize are like getting pre-made rooms or even a whole house. They come with their own design, colors, and layout. You can change a few things, but mostly, you're stuck with what you get.

Now, Tailwind CSS is like getting a box of building blocks, each block being a basic utility - like a specific color, border, or spacing. Instead of giving you pre-made rooms, Tailwind lets you build each room from scratch using these blocks. You get to decide how each room looks, where the windows go, what color the walls are, etc.

This means with Tailwind, you have more control and can create a very unique and personalized house (or in web development terms, a website). It's like having a toolset to create custom designs easily, without having to make everything from scratch. For developers who like to have more hands-on control and want to make their website look exactly the way they want, Tailwind is a great choice. It simplifies the process of writing CSS because you're mostly assembling these utility blocks rather than writing lots of custom styles.

TAILWIND CSS EXAMPLES

Certainly! Here are examples of how Tailwind CSS utility classes can be applied in HTML, along with brief explanations for each:

  1. Text and Background Colors:
 <div class="text-blue-500 bg-gray-200 p-4">
       This div has blue text on a light gray background with 4 units of padding.
   </div>
Enter fullscreen mode Exit fullscreen mode

In this example, the text is styled with the class text-blue-500, the background with bg-gray-200, and padding with p-4. 2. Flexbox Layout:

<div class="flex justify-between">
       <div>Left Item</div>
       <div>Right Item</div>
   </div>

Enter fullscreen mode Exit fullscreen mode

The flex class creates a flex container, and justify-between evenly spaces its children, resulting in a left and right alignment.

  1. Responsive Design:
<p class="text-center sm:text-left lg:text-right">
    This text aligns differently based on the screen size.
</p>

Enter fullscreen mode Exit fullscreen mode

Using responsive utility classes (sm:, lg:), text alignment adjusts for different screen sizes.

  1. Buttons:
   <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
       Click me
   </button>
Enter fullscreen mode Exit fullscreen mode

This button has a blue background, white text, and rounded corners. The color changes on hover (hover:bg-blue-700).

  1. Spacing:
   <div class="m-4 p-8">
       This div has margin of 4 units and padding of 8 units.
   </div>

Enter fullscreen mode Exit fullscreen mode

The m-4 adds a margin of 4 units, and p-8 adds padding of 8 units to the div.

  1. Shadows:
   <div class="shadow-lg p-6">
       This div has a large shadow and padding of 6 units.
   </div>
Enter fullscreen mode Exit fullscreen mode

The shadow-lg class adds a large shadow to the div, and p-6 sets padding to 6 units.

  1. Borders:

This div has a 2-pixel gray border.

A 2-pixel gray border is added to this div using border-2 and border-gray-500 classes.

These examples illustrate how Tailwind CSS utility classes facilitate rapid styling and layout adjustments directly within HTML, contributing to efficient and customizable web development.

GETTING STARTED WITH TAILWIND

Tailwind CSS streamlines development by automatically scanning HTML, JavaScript, and templates for class names, generating matching styles. These styles are then compiled into a cohesive set and written to a static CSS file, eliminating the need for manual styling. This automated process ensures consistency across your project, making Tailwind CSS a powerful tool for efficient and cohesive web development.

  1. Install Tailwind CSS Install tailwindcss via npm, and create your tailwind.config.js file.
Terminal:
npm install -D tailwindcss
npx tailwindcss init
Enter fullscreen mode Exit fullscreen mode
  1. Configure your template paths Add the paths to all of your template files in your tailwind.config.js file.
Tailwind.config.js

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

Enter fullscreen mode Exit fullscreen mode
  1. Add the Tailwind directives to your CSS Add the @tailwind directives for each of Tailwind’s layers to your main CSS file.
src/input.css

@tailwind base;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode
  1. Start the Tailwind CLI build process

Run the CLI tool to scan your template files for classes and build your CSS.

Terminal

npx tailwindcss -i ./src/input.css -o ./src/output.css --watch
Enter fullscreen mode Exit fullscreen mode
  1. Start using Tailwind in your HTML

Add your compiled CSS file to the

and start using Tailwind’s utility classes to style your content.
src/index.html

<!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>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)