Skip to Content
Module 4: Styling & Layout4.1 Tailwind CSS Mastery

Module 4.1: Tailwind CSS Mastery

Banner for Tailwind CSS Mastery module

🎯 Learning Objectives

By the end of this module, you’ll have mastered:

  • Utility-first Fundamentals: Grasp the mindset of building UIs with pre-built utility classes instead of writing custom CSS.
  • Flexbox & Grid Patterns: Apply the most common layout patterns used in real-world applications.
  • Responsive Design: Craft interfaces that adapt to any screen size (Mobile, Tablet, Desktop) using prefixes like sm:, md:, lg:.

1. Setting Up & Configuring Tailwind CSS v4 (Vite)

Setup Tailwind v4 for a Vite project

Tailwind CSS v4.0 introduces a fresh approach, deeply integrating into your build process and simplifying configuration.

1.1. Installing Dependencies

Open your terminal in the project directory and run:

npm install tailwindcss @tailwindcss/vite

1.2. Configuring the Vite Plugin

Open your vite.config.ts file and add the @tailwindcss/vite plugin:

import { defineConfig } from 'vite'; import tailwindcss from '@tailwindcss/vite'; export default defineConfig({ plugins: [tailwindcss()], });

Note: With Tailwind v4, you no longer need to create tailwind.config.js or postcss.config.js files like you did in v3. Configuration can be auto-detected or defined directly within your CSS.

1.3. Importing Tailwind into Your CSS

Open src/index.css (or your main CSS file), delete any existing content, and replace it with the following import:

@import 'tailwindcss';

1.4. Theme Configuration (New in v4)

Instead of theme.extend in a JS config file, v4 introduces the @theme directive directly within your CSS file. You can define variables in :root first, then incorporate them into @theme.

@import 'tailwindcss'; :root { --font-family-display: 'Satoshi', 'sans-serif'; --color-avocado-500: oklch(0.84 0.18 117.33); --color-avocado-600: oklch(0.79 0.16 117.33); } @theme { /* Link variables from :root into the Theme */ --font-display: var(--font-family-display); --color-avocado-500: var(--color-avocado-500); --color-avocado-600: var(--color-avocado-600); /* Or define them directly */ --breakpoint-3xl: 1920px; --ease-fluid: cubic-bezier(0.3, 0, 0, 1); }

Dive deeper into syntax and color palettes here:

1.5. Verification

Restart your development server (npm run dev) and add some test code to App.jsx:

<h1 className="text-3xl font-bold underline text-blue-600">Hello Tailwind v4!</h1>

2. The Utility-First Mindset

Instead of naming classes semantically (like .chat-notification) and writing custom CSS, you’ll use small, single-purpose utility classes (like p-6, max-w-sm, mx-auto).

Comparison of Utility-first vs Traditional CSS class naming

Why Use Tailwind?

  1. No More Class Name Brainstorming: Say goodbye to wrapper, container-inner, nav-left.
  2. Leaner CSS Files: Tailwind automatically purges unused classes during the production build.
  3. Safe Modifications: Changing a component’s style won’t accidentally break another part of your UI (since you’re not relying on global CSS).

3. Layout Patterns: Flexbox & Grid

3.1. Flexbox (Single-Axis Alignment)

Use Flexbox for arranging content horizontally (row) or vertically (col).

// Example: Navbar <nav className="flex justify-between items-center bg-gray-800 p-4 text-white"> <div className="text-xl font-bold">Logo</div> <ul className="flex gap-4"> <li>Home</li> <li>About</li> <li>Contact</li> </ul> </nav>

Master Flexbox: https://flexboxfroggy.com

3.2. Grid (Two-Dimensional Alignment)

Ideal for grid-based layouts and galleries.

// Example: Photo Gallery <div className="grid grid-cols-3 gap-4 p-4"> <div className="bg-blue-200 h-32">1</div> <div className="bg-blue-200 h-32">2</div> <div className="bg-blue-200 h-32">3</div> <div className="bg-blue-200 h-32">4</div> <div className="bg-blue-200 h-32">5</div> <div className="bg-blue-200 h-32">6</div> </div>

Master Grid: https://cssgridgarden.com

4. Responsive Design (Mobile-First)

Tailwind employs a Mobile-First breakpoint system. This means your default styles (without prefixes) apply to mobile screens, and prefixes like md: or lg: override those styles on larger screens.

Tailwind CSS responsive breakpoints visualized

Common Breakpoints:

  • sm: min-width 640px
  • md: min-width 768px (Tablet)
  • lg: min-width 1024px (Laptop)
  • xl: min-width 1280px (Desktop)

The Rule:

“Code for mobile first, then add styles for larger screens as needed.”

Example: A div with a red background on mobile, blue on tablet, and yellow on desktop.

<div className="bg-red-500 md:bg-blue-500 lg:bg-yellow-500 h-20">Resize your browser to see the color change</div>

Practical Example: A grid that’s 1 column on mobile and switches to 3 columns on desktop.

<div className="grid grid-cols-1 md:grid-cols-3 gap-4">{/* Your grid items here */}</div>

5. Hands-On Practice (Labs)

Lab 1: Modern Product Card (Hover & Responsive)

Build a “Shopee/Lazada-style” product card, but with a more refined aesthetic:

  • Responsive: Mobile (image on top, text below) vs. Desktop (image on left, text on right), or a grid layout.
  • Hover Effects: When you hover over the card, slightly scale up the image (scale-105) and reveal an “Add to Cart” button.

Example of a responsive product card with hover effects

💡 Pro-Tip:

  • Use Tailwind’s group and group-hover classes to control child element states when hovering over the parent.
  • For responsiveness, use flex flex-col md:flex-row to change the layout direction.
  • Employ object-cover for images to prevent distortion.

Lab 2: Responsive Admin Dashboard Shell

Create the basic layout structure (skeleton) for an Admin dashboard page.

  • Mobile: Header + a Hamburger Menu (the sidebar should be hidden).
  • Desktop: A fixed sidebar on the left (w-64), a sticky header at the top, and content that scrolls independently.

Layout of a responsive admin dashboard shell

💡 Pro-Tip:

  • Utilize CSS Grid with grid-cols-[auto_1fr] (sidebar takes its natural width, content fills the rest).
  • Alternatively, use Flexbox: Sidebar w-64 flex-shrink-0, Content flex-1.
  • Use classes like hidden md:block to show/hide the sidebar based on screen size.

6. Key Takeaways

  1. Utility-first: Leverage pre-built classes to build UIs rapidly, like assembling LEGO bricks.
  2. Flexbox: Ideal for single-axis layouts (navbars, centering elements, arranging items in a row or column).
  3. Grid: Perfect for two-dimensional layouts (product grids, dashboard structures).
  4. Responsive Design: Always remember the Mobile-First principle. Start with no prefixes, then add md:, lg:, and so on.

7. Further Reading

Last updated on