Unicorn Platform

World's best and simplest website builder powered by AI

Build your website
Friday, October 27, 2023

Master Tailwind CSS with this step-by-step tutorial

Introduction

Tailwind CSS has become one of the most popular CSS frameworks for rapidly building modern websites and web apps. With its utility-first design philosophy, Tailwind removes the need to write custom CSS for every little style change by providing a wide range of pre-defined CSS utility classes.

In this comprehensive beginner's guide, you'll learn how to start using Tailwind CSS through practical examples and tutorials. We'll walk through core concepts like responsive design, hover states, dark mode, and more. By the end, you'll be able to create beautiful full-page designs just using Tailwind's class names!

Here's what you can expect to learn:

  • How to install and configure Tailwind CSS for a project from scratch
  • Understanding the utility-first methodology and how it speeds up styling
  • Using Tailwind's responsive modifiers to easily adapt layouts for mobile
  • Styling elements like text, spacing, sizing, images, and more
  • Applying useful utility classes for hover, focus, dark mode, etc.
  • Creating reusable components with @apply and custom CSS
  • Building a complete responsive landing page entirely with Tailwind

We'll be building a mockup project step-by-step to help reinforce your skills. And don't worry, no prior experience with Tailwind CSS is required! I'll provide all the code examples on GitHub so you can follow along. Let's dive in and start mastering Tailwind CSS!

Tailwind CSS Basics

Tailwind CSS is a utility-first CSS framework that makes it faster to build custom user interfaces. Instead of opinionated pre-designed components like Bootstrap, Tailwind provides low-level utility classes for styling elements.

For example, to make text bold you would add the font-bold class instead of having to write custom CSS:

<span class="font-bold">Bold text</span>

This eliminates the need to come up with class names like .bold-text and maintain separate CSS files. With Tailwind, you style directly in your HTML using pre-defined utilities.

Why Use Tailwind CSS?

Here are some of the key benefits of using Tailwind CSS for your projects:

  • Speeds up development by reducing time spent writing custom CSS. Prototyping is blazingly fast with Tailwind's utility classes.
  • Provides utility classes for rapid prototyping. Build UI mockups and proof-of-concepts quickly.
  • Highly customizable and flexible system. Customize colors, typography, spacing, and more.
  • Encourages reusable component design with @apply. Create and share components.
  • Excellent for responsive web design with mobile-first workflow. Easily adapt layouts for any screen size.
  • Integrates well with JavaScript frameworks like React, Vue, Angular etc. UI libraries provide out-of-the-box integrations.

Tailwind removes the mental tax of coming up with class names and lets you focus on building. The intuitive utility classes make it fast to translate design mockups into real code.

Tailwind vs. Bootstrap

While both are CSS frameworks, Tailwind and Bootstrap have some notable differences:

  • Tailwind uses utility classes instead of pre-made components
  • More flexibility and control with Tailwind's methodology
  • Bootstrap has prebuilt UI elements like navigation bars, cards, modals etc.
  • Bootstrap relies heavily on JS whereas Tailwind is CSS-focused
  • Tailwind encourages writing CSS directly in HTML files

In general, Tailwind gives you lower-level primitives and finer control compared to Bootstrap's pre-designed components. Both have their use cases, but Tailwind offers faster prototyping and customization.

Installing Tailwind CSS

Let's install Tailwind CSS into a new project using npm:

# Using npm
npm install tailwindcss

# Using Yarn
yarn add tailwindcss

Next we'll configure Tailwind by creating a tailwind.config.js file:

// tailwind.config.js
module.exports = {
  // Configuration options
}

Then generate our CSS using the @tailwind directive:

/* Input CSS */

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

This will produce all our utility classes! For production, we can purge unused CSS for better performance.

Integrating with Frameworks

It's easy to integrate Tailwind with React, Vue, Angular and more:

// React example
import React from 'react'
import './index.css'

function App() {
  return <h1 className="text-3xl">Hello world!</h1> 
}

Frameworks like Next.js have Tailwind built-in or via plugins.

Core Tailwind CSS Features

Now that we've installed Tailwind, let's explore some of the core features for styling content. We'll look at utilities for typography, spacing, sizing, responsive design, hover states, and more.

Text and Typography

Tailwind includes extensive text and typography utilities for styling fonts:

<p class="text-lg font-medium text-center text-gray-700">
  This text is styled with Tailwind!
</p>

We can control:

  • text-color
  • font-size
  • font-weight
  • text-align
  • Responsive variants like md:text-left

And much more for line-height, letter-spacing, text transforms, etc.

Spacing and Sizing

Elements can be spaced and sized intuitively:

<div class="p-4 mx-auto max-w-xl">
  <img class="w-full rounded-lg" src="...">
</div>

Tailwind provides utilities for:

  • Padding - p-
  • Margins - m-
  • Width - w-
  • Height - h-
  • Display - block, inline-flex etc.

Percentages, rem, em units are also available!

Responsive Breakpoints

One of Tailwind's killer features is responsive design. We can modify styling at user-defined breakpoints:

<h1 class="text-3xl md:text-5xl">Hello World!</h1>

This will style the <h1> as:

  • text-3xl by default
  • text-5xl at the md breakpoint and above

Tailwind makes responsive layouts easy without media queries!

Container Classes

Tailwind includes component classes for fixed or fluid width containers:

<div class="container mx-auto px-4">...</div>

<div class="container max-w-xl mx-auto">...</div>

These are pre-made responsive components for wrapping content sections.

Tailwind Components, Variants and Directives

In addition to utility classes, Tailwind provides useful features like prebuilt components, variants for interactions, customization directives, and more.

Tailwind comes with responsive navigation bar, dropdown, and modal components:

<!-- Navigation -->
<nav class="bg-gray-800">
  <div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
    <!-- Logo and menu -->
  </div>
</nav>

<!-- Modal -->
<div class="fixed z-10 inset-0 overflow-y-auto" aria-labelledby="modal-title" role="dialog" aria-modal="true">
  <div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">

    <!-- Modal content -->
  </div>
</div>

These can be customized or used as-is to build UIs faster.

@layer and Custom Components

To generate our own component classes, we can use the @layer directive:

@layer components {
  .btn-blue {
    @apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
  }
}

Now .btn-blue can be used anywhere in our HTML. We can add variants, responsive versions, and publish these components as plugins.

Hover, Focus and More with @variants

Tailwind generates hover, focus, responsive variants automatically with @variants:

<button class="bg-blue-500 hover:bg-blue-700 ...">
  Button 
</button>

No extra CSS needed for :hover and :focus states!

Dark Mode Support

Tailwind makes switching to dark mode easy:

<html class="dark">
</html>

Just by adding the dark class, colors switch based on config!

Advanced Features and Customization

We've covered the fundamentals, but Tailwind offers many advanced features through configuration, plugins, and frameworks integrations.

Tailwind Configuration

Almost all default styles can be customized in tailwind.config.js:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        brand: '#345CBF',
      }
    }
  }
}

Now bg-brand can be used for custom blue color. We can customize fonts, spacing scales, breakpoints, variants, and more.

Plugins and Tools

Tailwind has a rich ecosystem of plugins and tools:

  • First-party plugins like Typography, Forms, Aspect Ratio
  • Third-party plugins like Headless UI, Heroicons UI, Tailwind CSS IntelliSense
  • Play CDN for rapid prototyping without installing Tailwind
  • Tailwind CLI and PurgeCSS for optimizing in production

These extend Tailwind's functionality for large projects.

Frameworks and Templates

Tailwind plays nicely with popular JS frameworks like React, Vue, and Angular. There are also starters, themes, and open source templates available for frameworks like Next, Gatsby, Nuxt, and more.

For CMSs like WordPress, integration plugins make using Tailwind easy. There are also SaaS products like Float UI that provide pre-made Tailwind components.

Building a Complete Project

Now that we've covered the basics, let's start building a real-world project with Tailwind CSS. We'll create a simple landing page with a header, hero, features section, testimonials, and more.

Project Overview

Our landing page will have these sections:

  • Header - Navigation bar and logo
  • Hero - Tagline, image, and primary call to action
  • Features - Grid with icons and feature descriptions
  • Testimonials - Customer reviews
  • Contact - Get in touch CTA

We'll make the page fully responsive for mobile devices. Let's start coding it!

Coding the Header Section

Building the header with Tailwind is easy:

<!-- Navigation -->
<nav class="bg-indigo-600">
  <div class="mx-auto max-w-7xl px-6 lg:px-8">
    <!-- Logo and menu -->
  </div> 
</nav>

We can use flexbox, component classes, and modifiers like lg:px-8 to style the navbar efficiently.

Building the Hero Section

The hero showcases the product and value proposition:

<!-- Hero -->
<section class="bg-white">
  
  <!-- Image -->
  <img src="hero-image.jpg" class="w-full lg:w-1/2">

  <!-- Content -->
  <div class="px-4 py-16 lg:w-1/2 lg:py-32 lg:px-8">
    <h1 class="text-4xl font-bold">Build your next project faster</h1>
    
    <!-- Button -->
    <button class="bg-indigo-500 text-white px-4 py-2 rounded-lg mt-8">Get Started</button> 
  </div>
  
</section>

Tailwind's grid and flexbox utilities make layout easy. We can later add responsiveness.

Completing the Page

Let's quickly complete the other sections:

<!-- Features -->
<section id="features" class="py-8">

  <div class="max-w-md mx-auto px-4">
    <h2 class="mb-4 text-lg font-medium">Key Features</h2>

    <!-- Feature details -->

  </div>

</section>

<!-- Testimonials -->

<section id="reviews" class="bg-gray-50 py-16">

  <div class="max-w-md mx-auto px-4">

    <!-- Reviews -->

  </div>

</section> 

<!-- Contact -->

<section id="contact" class="py-16">

  <div class="max-w-md mx-auto px-4">
    
    <h2 class="text-lg font-medium mb-4">Get in touch</h2>

    <!-- Form -->

  </div>
  
</section>

Some smoothing scrolling, transitions, and the site is complete!

Linking to Products and Services

To learn more about rapid web development with Tailwind CSS, explore integrated solutions like Float UI's premium components and templates.

Conclusion

And that wraps up this step-by-step introduction to Tailwind CSS! We covered utility-first styling, responsive design, hover states, customization features, and even built out a complete landing page.

Key takeaways:

  • Tailwind speeds up CSS development through utility classes
  • Mobile-friendly styles are easy with responsive modifiers
  • Minimal custom CSS needed when using utilities
  • Flexible theming and customization options
  • Integrates well with React, Vue, and more

Hopefully you now feel confident to start using Tailwind CSS in your own projects! Be sure to check out the official docs, plugins, examples, and tutorials to learn more. Feel free to reach out with any questions.

Happy coding!