TExcelTAD
Home › Tailwind CSS › Tailwind Exercises

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.

html · try it yourself
<!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
html
<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.

html · try it yourself
<!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
html
<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.

html · try it yourself
<!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
html
<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.

html · try it yourself
<!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
html
<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%.

html · try it yourself
<!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
html
<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.

html · try it yourself
<!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
html
<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.

html · try it yourself
<!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
html
<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.

html · try it yourself
<!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
html
<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.

html · try it yourself
<!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
html
<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?

  1. grid grid-cols-3 sm:grid-cols-1
  2. grid grid-cols-1 lg:grid-cols-3
  3. flex lg:flex-col
  4. grid max-lg:grid-cols-3

A label should only appear when a *previous sibling* checkbox is checked. What do you use?

  1. group + group-checked:
  2. peer + peer-checked:
  3. checked: on the label
  4. has-checked: on the checkbox