Sub topic 2
Colors and Backgrounds
Assume you know basic HTML, CSS selectors, the box model, and how to run a package-based frontend project. Tailwind CSS generates styles from utility classes placed in your markup.
Colors and backgrounds
Tailwind’s default color scale uses names such as slate, indigo, and rose, followed by a shade from 50 to 950. Use bg-* for backgrounds, text-* for text, and border-* for borders.
<article class="max-w-sm rounded-xl bg-white p-6 text-slate-900 shadow-md">
<p class="text-sm font-medium text-indigo-600">Featured</p>
<h2 class="mt-2 text-xl font-semibold">Utility classes</h2>
<p class="mt-2 text-slate-600">
Small classes compose the complete design.
</p>
<button class="mt-4 rounded-lg bg-indigo-600 px-4 py-2 text-white">
Read more
</button>
</article>Color choice is part of the design system. Reuse a small set of shades instead of choosing unrelated values for every element.
Borders, rounded corners, and shadows
Use border to add a one-pixel border, then add a color such as border-slate-200. Radius utilities include rounded, rounded-lg, and rounded-full. Shadow utilities add elevation, from shadow-sm to shadow-xl.
<div class="rounded-lg border border-slate-200 bg-white p-4 shadow-sm">
A quiet, raised panel
</div>Layout with flexbox and grid
Tailwind maps common layout properties to short utilities. flex enables Flexbox, items-center aligns items on the cross axis, and justify-between distributes free space. grid, grid-cols-*, and gap-* create grid layouts.
<nav class="flex items-center justify-between p-4">
<a href="/" class="font-bold">Brand</a>
<div class="flex gap-4">
<a href="/docs">Docs</a>
<a href="/blog">Blog</a>
</div>
</nav>
<section class="grid grid-cols-1 gap-4 sm:grid-cols-2">
<div class="rounded bg-slate-100 p-4">One</div>
<div class="rounded bg-slate-100 p-4">Two</div>
</section>Responsive and state variants
Tailwind uses a mobile-first model. An unprefixed class applies at every width. A prefixed class applies from that breakpoint upward. For example, sm:grid-cols-2 changes a one-column grid to two columns on small screens and wider screens.
State variants add a prefix and colon. Use hover:bg-indigo-700 for pointer interaction and focus:ring-2 for keyboard or programmatic focus.
<button class="rounded-lg bg-indigo-600 px-4 py-2 text-white
hover:bg-indigo-700 focus:outline-none focus:ring-2
focus:ring-indigo-500">
Save
</button>Keep focus styles visible. Removing them without providing another clear indicator harms keyboard users.
Exercise
What does grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 do?
Show answer
It uses one column by default, two columns from the sm breakpoint, and three columns from the lg breakpoint.