Angular course Β· Module 13: UI and Styling

Tailwind CSS in Angular

2 min read Β·
In this lesson4

Tailwind is a utility-first CSS framework - like a well-organized arsenal.

Installation

# Tailwind CSS 3 (with the configuration shown below)
npm install -D tailwindcss@3 postcss autoprefixer
npx tailwindcss init

Tailwind CSS 4 installs differently: npm install tailwindcss @tailwindcss/postcss postcss, then a .postcssrc.json file with the @tailwindcss/postcss plugin and the line @import "tailwindcss"; in styles.css. You do not need tailwind.config.js there, and the configuration in the next section applies to version 3.

Configuration

// tailwind.config.js
module.exports = {
  content: [
    "./src/**/*.{html,ts}",
  ],
  theme: {
    extend: {
      colors: {
        'samurai-red': '#c41e3a',
        'samurai-gold': '#ffd700',
        'dojo-dark': '#1a1a2e',
      },
      fontFamily: {
        'japanese': ['Noto Sans JP', 'sans-serif'],
      },
    },
  },
  plugins: [],
}
// styles.scss
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  .btn-samurai {
    @apply bg-samurai-red text-white px-4 py-2 rounded-lg
           hover:bg-red-700 transition-colors duration-200
           focus:outline-none focus:ring-2 focus:ring-samurai-gold;
  }

  .card-dojo {
    @apply bg-white dark:bg-dojo-dark rounded-xl shadow-lg
           p-6 border border-gray-200 dark:border-gray-700;
  }
}

Usage in Components

@Component({
  template: `
    <div class="min-h-screen bg-gray-100 dark:bg-dojo-dark p-8">
      <header class="flex items-center justify-between mb-8">
        <h1 class="text-3xl font-bold text-samurai-red">Dojo</h1>
        <button class="btn-samurai">New Samurai</button>
      </header>

      <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        @for (samurai of samuraiList(); track samurai.id) {
          <article class="card-dojo hover:shadow-xl transition-shadow">
            <h2 class="text-xl font-semibold mb-2">{{ samurai.name }}</h2>
            <p class="text-gray-600 dark:text-gray-300">
              Clan: {{ samurai.clan }}
            </p>
            <div class="mt-4 flex gap-2">
              <button class="text-samurai-gold hover:underline">Edit</button>
              <button class="text-red-500 hover:underline">Delete</button>
            </div>
          </article>
        }
      </div>
    </div>
  `
})
export class DojoGridComponent {
  samuraiList = signal<Samurai[]>([]);
}

Combining with Angular Material

@Component({
  imports: [MatButtonModule, MatCardModule],
  template: `
    <mat-card class="max-w-md mx-auto shadow-xl">
      <mat-card-header class="bg-gradient-to-r from-samurai-red to-red-700 text-white rounded-t-lg">
        <mat-card-title class="px-4 py-2">Samurai Profile</mat-card-title>
      </mat-card-header>
      <mat-card-content class="p-6">
        <div class="space-y-4">
          <!-- Content with Tailwind -->
        </div>
      </mat-card-content>
    </mat-card>
  `
})
export class HybridStyledComponent {}

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do utility classes work in Tailwind CSS?

  2. 2. What does the md:grid-cols-2 class mean in Tailwind CSS?

These are 2 of 5 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Vertical ordering

    Order the Tailwind directives in the styles.scss file as shown in the lesson (top to bottom).

  • Code editor

    Complete the card component with Tailwind CSS classes: the card needs a white background and the image the same corner radius as the card.

  • Horizontal ordering

    Arrange on a single line an @apply rule that creates a custom class from Tailwind utilities.

  • Click in order

    Build the class attribute that builds a card with Tailwind: class="bg-white rounded-lg shadow-lg"

  • Horizontal ordering

    Arrange the Tailwind classes for a responsive grid: from a base of 1 column to 3 columns on large screens.

  1. QuestionTailwind classes
  2. QuestionTailwind responsive prefix
  3. Put in orderTailwind directives in styles.scss
  4. Coding taskPractice: Tailwind in Angular
  5. Put in orderThe @apply directive
  6. QuestionThe dark variant in Tailwind
  7. Click in orderTailwind card class
  8. QuestionComparing styling techniques
  9. Put in orderTailwind grid class
  10. QuestionThe :host-context selector
  11. VideoModule summary

Useful articles