Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Practice, Common Mistakes and Cheat Sheet

Practice Tailwind CSS with three guided exercises, learn the ten most common mistakes and keep a compact cheat sheet of breakpoints, variants, theme namespaces and directives.

You have now met the whole toolbox: utilities, layout, typography, variants, themes, components, animation and build tools. Skill comes from using them without looking things up, so this last lesson gives you three exercises, the mistakes beginners make most often, and a cheat sheet to keep open while you work.

A reliable workflow

Phone first Write base classes for the smallest screen.
Add breakpoints Layer sm, md and lg only where the design needs it.
Use tokens Colors, fonts and radii come from the theme.
Check states Hover, focus, disabled, dark and reduced motion.
1 / 2

Exercise 1: a responsive pricing row

Goal: three cards that stack on phones, sit in a row from md, and make the middle card stand out with a ring, a raised look and a "Best value" badge.

Requirements: grid layout, gap, rounded corners, the middle card lifted only on md and up, hover effect on all cards.

Exercise 1 starter

Try it yourself first. When you are done, compare with this solution:

HTML
<div class="grid gap-6 p-6 md:grid-cols-3">
  <div class="rounded-2xl bg-white p-6 shadow transition hover:-translate-y-1 hover:shadow-lg">
    <h3 class="font-semibold">Basic</h3>
    <p class="mt-2 text-slate-600">$5 / month</p>
  </div>
  <div class="relative rounded-2xl bg-white p-6 shadow-lg ring-2 ring-indigo-500 transition hover:shadow-xl md:-translate-y-2">
    <span class="absolute -top-3 left-6 rounded-full bg-indigo-600 px-3 py-0.5 text-xs font-semibold text-white">Best value</span>
    <h3 class="font-semibold">Standard</h3>
    <p class="mt-2 text-slate-600">$12 / month</p>
  </div>
  <div class="rounded-2xl bg-white p-6 shadow transition hover:-translate-y-1 hover:shadow-lg">
    <h3 class="font-semibold">Premium</h3>
    <p class="mt-2 text-slate-600">$25 / month</p>
  </div>
</div>

Stretch goal: remove the repeated card classes by writing a card() function (see Reusing Styles with Components).

Exercise 2: a dark-mode notification

Goal: a notification with an icon, title, text and a close button. It should have light and dark looks, switchable with a button that toggles a dark class, and the close button needs hover and focus-visible styles.

Exercise 2 starter

Solution sketch:

HTML
<div class="flex max-w-sm items-start gap-3 rounded-xl bg-white p-4 shadow ring-1 ring-slate-200 dark:bg-slate-800 dark:ring-slate-700">
  <div class="grid size-8 shrink-0 place-items-center rounded-full bg-emerald-100 text-emerald-700 dark:bg-emerald-500/20 dark:text-emerald-300">&#10003;</div>
  <div class="grow">
    <p class="font-medium text-slate-900 dark:text-white">Backup complete</p>
    <p class="text-sm text-slate-600 dark:text-slate-400">Your 2,314 files are safe.</p>
  </div>
  <button aria-label="Dismiss" class="rounded p-1 text-slate-400 hover:bg-slate-100 hover:text-slate-700 focus-visible:outline-2 focus-visible:outline-indigo-500 dark:hover:bg-slate-700 dark:hover:text-white">&times;</button>
</div>

The page itself should also change: add dark:bg-slate-900 to the scene so the card does not float on a white page.

Exercise 3: your own design tokens

Goal: define a brand palette and two custom utilities, then use them to restyle a button and a heading. This practices @theme, @utility and @custom-variant.

Exercise 3 starter

Solution sketch:

CSS
@theme {
  --color-brand-500: oklch(0.65 0.18 30);
  --color-brand-700: oklch(0.5 0.17 30);
}
@utility text-balance-tight {
  text-wrap: balance;
  letter-spacing: -0.02em;
}
@custom-variant hocus (&:hover, &:focus-visible);
HTML
<h1 class="text-balance-tight text-3xl font-bold text-brand-700">Make this headline balanced</h1>
<button class="rounded-lg bg-brand-500 px-5 py-2.5 text-white hocus:bg-brand-700">Brand button</button>

The ten most common mistakes

  1. Reading sm: as "small screens only". Prefixes are minimum widths. Write the phone style unprefixed.
  2. Building class names from fragments. bg-${color}-500 never reaches the generated CSS. Use complete names in a lookup table.
  3. Conflicting utilities on one element, like p-2 p-4. Only one should remain; use tailwind-merge in components.
  4. Forgetting the viewport meta tag, which makes phones render at desktop width.
  5. Hiding inputs with hidden in peer patterns. Use sr-only so they stay accessible.
  6. Missing focus styles. If you remove an outline, replace it with focus-visible: styles.
  7. Relying on color alone for errors, status or links.
  8. Using @apply for everything. It is a tool for content you cannot mark up, not a replacement for components.
  9. Unlayered custom CSS that overrides utilities unexpectedly. Put it in @layer base or @layer components.
  10. Shipping the browser script instead of a build.

Cheat sheet

Breakpoints and container queries

Prefix Min width Container version
sm 640px @sm (384px)
md 768px @md (448px)
lg 1024px @lg (512px)
xl 1280px @xl (576px)
2xl 1536px @2xl (672px)

Variants you will use daily

Group Variants
Interaction hover: focus: focus-visible: active: disabled:
Forms checked: invalid: user-invalid: placeholder: required:
Structure first: last: odd: even: empty: *:
Related state group-hover: peer-checked: has-checked: in-focus: not-hover:
Environment dark: motion-safe: motion-reduce: print: supports-[...]:
Attributes aria-expanded: data-[state=open]: open:
Pseudo-elements before: after: selection: marker: file:
Entry starting:

Theme namespaces

Namespace Creates
--color-* bg-*, text-*, border-*, ring-*, fill-*
--font-* font-* families
--text-* text-* sizes
--spacing all spacing and sizing numbers
--breakpoint-* responsive variants
--radius-*, --shadow-* rounded-*, shadow-*
--ease-*, --animate-* ease-*, animate-*

Directives

Directive Purpose
@import "tailwindcss" load Tailwind
@theme / @theme inline / @theme static design tokens
@utility your own utility
@custom-variant / @variant / @slot your own variant; use a variant in CSS
@layer base | components place your CSS in the cascade
@apply inline utilities into a rule
@source / @source inline() extra scanned paths and safelist
@plugin / @config plugins; legacy JS config
@reference access the theme from a separate style block

Syntax patterns

Need Write
Arbitrary value w-[37rem], bg-[#0ea5e9], grid-cols-[1fr_2fr]
CSS variable bg-(--panel-bg)
Arbitrary property [mask-type:luminance]
Arbitrary variant [&_p]:mt-2, [@media(min-aspect-ratio:16/9)]:hidden
Opacity bg-black/50
Important font-bold!
Named group group/card + group-hover/card:
Prefix (optional) @import "tailwindcss" prefix(tw); then tw:flex

Where to go next

  • Rebuild a page you admire from memory, using only utilities and your own tokens.
  • Combine Tailwind with a component framework, such as the React or Vue tutorials on this site, and practice variant maps.
  • Read the generated CSS of your own project once; it demystifies everything.
  • Return to the Tailwind CSS tutorial index to revisit any chapter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which workflow order does this course recommend?

    Unprefixed utilities are the phone layout and larger breakpoints layer on top.
  2. 2. Which is the best way to switch a badge between green and red from data?

    Complete names are found by the scanner; fragments are not.
  3. 3. Where should your own component CSS go so utilities can override it?

    Layered CSS loses to utilities; unlayered CSS beats everything.
  4. 4. What does @theme inline help with when building multiple themes?

    inline puts var(--surface) straight into the utility, so each scope resolves its own value.

Summary

  • Practice by building: a responsive pricing row, a dark-mode notification and your own tokens cover the main skills.
  • Avoid the ten classic mistakes: fragments in class names, conflicts, missing viewport tag and focus styles, over-using @apply, unlayered CSS and shipping the browser script.
  • Keep the cheat sheet for breakpoints, variants, theme namespaces, directives and syntax patterns nearby.

Back to the course: Tailwind CSS