Tailwind Exercises
Time to practise. Each exercise has a starter playground: only edit the class attributes (and, where stated, a <style type="text/tailwindcss"> block) until the result matches the goal. Then compare your answer with the solution. There are usually several correct answers — if yours looks right, it is right.
| # | Topic |
|---|---|
| 1 | Colours, spacing and typography |
| 2 | Borders, radius and shadow |
| 3 | Flexbox alignment |
| 4 | Grid with spanning |
| 5 | Hover and transitions |
| 6 | Responsive layout |
| 7 | group and peer |
| 8 | Dark mode |
| 9 | Theme variables |
Exercise 1 — Colours, spacing and typography
Style the alert: light blue background (sky-50), dark blue text (sky-900), 1rem padding, the title bold and slightly larger (text-lg), and 0.25rem space between title and text.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-6">
<div class="">
<p class="">Update available</p>
<p class="">Version 4.1 is ready to install.</p>
</div>
</body>
</html>Show solution
<div class="bg-sky-50 p-4 text-sky-900">
<p class="text-lg font-bold">Update available</p>
<p class="mt-1">Version 4.1 is ready to install.</p>
</div>Exercise 2 — Borders, radius and shadow
Turn the box into a card: white background, a 1px gray-200 border, rounded-xl corners, a medium shadow, 1.5rem padding and a maximum width of max-w-xs. Give the page a gray-100 background.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-6">
<div class="">
<h2 class="font-semibold">Weekly summary</h2>
<p class="text-sm text-gray-600">You completed 12 lessons this week. 🎉</p>
</div>
</body>
</html>Show solution
<body class="bg-gray-100 p-6">
<div class="max-w-xs rounded-xl border border-gray-200 bg-white p-6 shadow-md">
…
</div>
</body>Exercise 3 — Flexbox alignment
Make the toolbar a single row: the title on the left, the two buttons on the right, everything vertically centred, with 0.5rem between the buttons.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-6">
<div class="rounded-lg bg-slate-800 p-3 text-white">
<h2 class="text-lg font-bold">Documents</h2>
<div class="">
<button class="rounded bg-slate-600 px-3 py-1 text-sm">Upload</button>
<button class="rounded bg-sky-500 px-3 py-1 text-sm">New</button>
</div>
</div>
</body>
</html>Show solution
<div class="flex items-center justify-between rounded-lg bg-slate-800 p-3 text-white">
<h2 class="text-lg font-bold">Documents</h2>
<div class="flex gap-2">
…
</div>
</div>Exercise 4 — Grid with spanning
Create a 3-column grid with a 0.75rem gap. "Featured" should span 2 columns and 2 rows; "Footer" should span the whole row.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-4">
<div class="text-sm font-semibold text-white *:rounded-lg *:p-4">
<div class="bg-fuchsia-600">Featured</div>
<div class="bg-fuchsia-400">A</div>
<div class="bg-fuchsia-400">B</div>
<div class="bg-fuchsia-800">Footer</div>
</div>
</body>
</html>Show solution
<div class="grid grid-cols-3 gap-3 text-sm font-semibold text-white *:rounded-lg *:p-4">
<div class="col-span-2 row-span-2 bg-fuchsia-600">Featured</div>
<div class="bg-fuchsia-400">A</div>
<div class="bg-fuchsia-400">B</div>
<div class="col-span-full bg-fuchsia-800">Footer</div>
</div>Exercise 5 — Hover and transitions
On hover, the button should turn a darker green (emerald-700), move up by 0.25rem and get a large shadow. All changes must animate over 200ms. When pressed it should shrink to 95%.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-8">
<button class="rounded-lg bg-emerald-600 px-5 py-2.5 font-semibold text-white">
Hover me
</button>
</body>
</html>Show solution
<button class="rounded-lg bg-emerald-600 px-5 py-2.5 font-semibold text-white transition duration-200 hover:-translate-y-1 hover:bg-emerald-700 hover:shadow-lg active:scale-95">
Hover me
</button>Exercise 6 — Responsive layout
The three feature boxes should be stacked in one column on phones, two columns from sm, and three columns from lg. The heading should be text-2xl on phones and text-4xl from md. Resize the preview to test.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-4">
<h1 class="font-bold">Why choose us</h1>
<div class="mt-3 gap-3 *:rounded-lg *:bg-indigo-100 *:p-4">
<div>⚡ Fast</div>
<div>🔒 Secure</div>
<div>💬 Friendly support</div>
</div>
</body>
</html>Show solution
<h1 class="text-2xl font-bold md:text-4xl">Why choose us</h1>
<div class="mt-3 grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3 *:rounded-lg *:bg-indigo-100 *:p-4">
…
</div>Exercise 7 — group and peer
Part A: when the user hovers anywhere on the list item, the arrow should move right by 0.25rem and turn indigo-600.
Part B: the "Terms accepted ✓" text must be hidden until the checkbox is checked.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="space-y-6 p-6">
<a href="#" class="flex w-64 items-center justify-between rounded-lg border border-gray-200 p-3 hover:bg-gray-50">
<span>Account settings</span>
<span class="text-gray-400 transition">→</span>
</a>
<label class="flex items-center gap-2">
<input type="checkbox" class="size-4">
I accept the terms
<span class="text-sm font-semibold text-emerald-600">Terms accepted ✓</span>
</label>
</body>
</html>Show solution
<a href="#" class="group flex w-64 items-center justify-between rounded-lg border border-gray-200 p-3 hover:bg-gray-50">
<span>Account settings</span>
<span class="text-gray-400 transition group-hover:translate-x-1 group-hover:text-indigo-600">→</span>
</a>
<label class="flex items-center gap-2">
<input type="checkbox" class="peer size-4">
I accept the terms
<span class="hidden text-sm font-semibold text-emerald-600 peer-checked:inline">Terms accepted ✓</span>
</label>Exercise 8 — Dark mode toggle
Make the button switch the card between light and dark. Add a @custom-variant so that dark: depends on the dark class, then add dark: classes: card slate-800 background with white heading and slate-300 text.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
<style type="text/tailwindcss">
/* add the custom variant here */
</style>
</head>
<body class="p-6">
<button onclick="document.documentElement.classList.toggle('dark')" class="mb-3 rounded bg-gray-900 px-3 py-1 text-sm text-white">Toggle theme</button>
<div class="rounded-xl bg-white p-5 shadow-md ring-1 ring-gray-200">
<h2 class="font-bold text-gray-900">Night owl</h2>
<p class="text-sm text-gray-600">I should go dark when you press the button.</p>
</div>
</body>
</html>Show solution
<style type="text/tailwindcss">
@custom-variant dark (&:where(.dark, .dark *));
</style>
…
<div class="rounded-xl bg-white p-5 shadow-md ring-1 ring-gray-200 dark:bg-slate-800 dark:ring-white/10">
<h2 class="font-bold text-gray-900 dark:text-white">Night owl</h2>
<p class="text-sm text-gray-600 dark:text-slate-300">I should go dark when you press the button.</p>
</div>Exercise 9 — Theme variables
Define a brand colour --color-brand (#7c3aed) and a font --font-display (Georgia, serif) in @theme, then use bg-brand, text-brand and font-display on the elements.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
<style type="text/tailwindcss">
@theme {
/* your variables */
}
</style>
</head>
<body class="p-6">
<h1 class="text-3xl font-bold">Brand heading</h1>
<button class="mt-3 rounded-lg px-4 py-2 text-white">Brand button</button>
</body>
</html>Show solution
<style type="text/tailwindcss">
@theme {
--color-brand: #7c3aed;
--font-display: Georgia, serif;
}
</style>
…
<h1 class="font-display text-3xl font-bold text-brand">Brand heading</h1>
<button class="mt-3 rounded-lg bg-brand px-4 py-2 text-white">Brand button</button>Test yourself
Which classes stack items on phones and show three columns from 1024px?
grid grid-cols-3 sm:grid-cols-1grid grid-cols-1 lg:grid-cols-3flex lg:flex-colgrid max-lg:grid-cols-3
A label should only appear when a *previous sibling* checkbox is checked. What do you use?
group+group-checked:peer+peer-checked:checked:on the labelhas-checked:on the checkbox