Sub topic 1
The utility-first approach
Tailwind CSS is a utility-first CSS framework. A utility is a small class that applies one CSS rule or closely related group of rules. You compose these classes in HTML instead of inventing a custom class for every component.
Prerequisites
Assume you know HTML, CSS selectors, the box model, and basic npm commands. Familiarity with components helps but is not required.
Utility-first styling
Traditional CSS might define .card, .card-title, and .card-button in a stylesheet. Tailwind puts those decisions in the markup:
<div class="max-w-sm rounded-xl bg-white p-6 shadow-md">
<h2 class="text-xl font-semibold text-slate-900">Card title</h2>
<p class="mt-2 text-slate-600">Supporting text.</p>
<button class="mt-4 rounded-lg bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700">
Read more
</button>
</div>Each class has a narrow purpose. This makes styles visible where the element is defined and encourages consistent values from Tailwind’s design scale. The trade-off is longer class attributes. Components or helper functions can contain repeated combinations when markup becomes difficult to read.
Setting up Tailwind and generating CSS
A build setup imports Tailwind’s CSS entry point. Tailwind scans configured source files for complete class names, then generates only the matching CSS. Unused utilities do not normally reach the production stylesheet.
For Tailwind v4, a minimal CSS entry file is:
@import "tailwindcss";Your npm script should run Tailwind’s build process in development and production. The exact command depends on the framework. The important flow is:
flowchart TD
A[Source files] --> B[Tailwind scans class names]
B --> C[Used utilities and variants]
C --> D[Generated CSS]
D --> E[Browser]Spacing, sizing, and typography
Spacing utilities use names such as p-6 for padding, mt-2 for top margin, and gap-4 for flex or grid gaps. Sizing utilities include w-full, max-w-sm, and min-h-screen. The numbers refer to the configured spacing scale rather than arbitrary pixels.
Typography utilities control several properties together. For example, text-xl sets font size, font-semibold sets weight, and text-slate-600 sets text color. Use these utilities consistently so headings, body text, and labels share predictable visual rules.
Exercise
What does mt-2 text-xl font-semibold apply to an element?
Show answer
It adds top margin from the spacing scale, increases the text size, and sets a semibold font weight.