How Tailwind CSS Utility Classes Work, With Examples to Try

Learn the utility-first idea behind Tailwind CSS, read class names like p-4 and md:flex, use variants and @apply, and experiment safely in the browser.

Updated September 29, 2026 5 min read

Use the free Tailwind CSS PlaygroundTry Tailwind CSS classes live with the Play CDN in your browser

Tailwind CSS divides opinion like few other tools. To some developers, a button with fifteen class names looks like a mess. To others, it is the fastest way they have ever styled an interface, because they never leave the HTML and never invent another class name like .card-wrapper-inner. Either way, if you join a project that uses it, you need to read and write those class names fluently.

This guide explains the utility-first approach, how Tailwind's class names are built, how responsive and state variants work, when @apply makes sense, and how to experiment in a browser without installing anything.

The utility-first idea

Traditional CSS usually starts with a semantic class and writes rules for it:

<div class="profile-card">…</div>
.profile-card {
  padding: 1.5rem;
  border-radius: 1rem;
  background: #fff;
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
}

Tailwind provides small, single-purpose classes and you compose them in the markup:

<div class="p-6 rounded-2xl bg-white shadow-lg">…</div>

Each class does one thing and maps to one or two CSS declarations. The benefits are speed (no switching files), consistency (values come from a shared scale) and smaller CSS in production, because only the classes you actually use are generated. The costs are longer class attributes and a vocabulary to learn.

Reading Tailwind class names

Most classes follow a property-value pattern:

Class CSS it produces (Tailwind v3 defaults)
p-4 padding: 1rem
mt-2 margin-top: 0.5rem
text-xl Font size 1.25rem with a matching line height
font-bold font-weight: 700
bg-indigo-600 Background colour from the indigo palette, shade 600
rounded-lg border-radius: 0.5rem
flex, grid display: flex, display: grid
gap-3 gap: 0.75rem
w-full width: 100%

The spacing scale is based on 0.25rem steps, so p-1 is 0.25rem, p-4 is 1rem and p-8 is 2rem. Colours run from 50 (lightest) to 900 or 950 (darkest). When the scale does not have what you need, square brackets create an arbitrary value: w-[327px] or bg-[#0f766e].

Variants: responsive and state prefixes

Prefixes apply a class only in certain conditions:

  • Responsive: md:flex means display: flex from the md breakpoint upwards. The default breakpoints in v3 are sm 640px, md 768px, lg 1024px, xl 1280px and 2xl 1536px. Tailwind is mobile-first: unprefixed classes apply to all sizes, and prefixes override them at larger widths.
  • State: hover:bg-indigo-700, focus:ring-2, disabled:opacity-50.
  • Dark mode: dark:bg-gray-900, depending on the project's dark mode setting.

Prefixes can be combined: md:hover:underline.

Try it in the Tailwind CSS Playground

Our Tailwind CSS Playground renders your HTML with Tailwind in a sandboxed preview, so you can learn by changing classes and watching the result.

  1. Open the Tailwind CSS Playground. A profile card example is loaded.
  2. Edit the HTML tab. Change from-indigo-500 to from-emerald-500 on the banner, or rounded-2xl to rounded-none, and watch the preview update.
  3. Open the Tailwind CSS tab to write custom CSS that Tailwind processes. @apply, @layer and theme() work here; the sample defines a .btn-primary component with @apply.
  4. Use the Tablet (768px) and Phone (375px) buttons to check responsive prefixes. Add md:flex to a container and see the layout change between tablet and phone widths.
  5. Check the Console under the preview for script output and errors.
  6. Click Download to save the page as tailwind-page.html.

The preview loads the Tailwind CSS v3 Play CDN from cdn.tailwindcss.com, which generates styles in the browser as you type. That is ideal for learning and prototyping but not for production.

When to use @apply

@apply lets you bundle utilities into your own class:

@layer components {
  .btn-primary {
    @apply px-4 py-2 rounded-lg bg-indigo-600 text-white font-medium hover:bg-indigo-700;
  }
}

It is useful for small, genuinely repeated elements that are not already components in your framework, such as a button style used in plain HTML templates. Overusing it recreates traditional CSS with extra steps. If you are working in React, Vue or similar, extracting a component is usually better than extracting a class.

Other ways to try Tailwind

The official Tailwind Play

Tailwind's own online playground supports configuration and multiple versions and lets you share links. It is the best option for reproducing issues to report to the Tailwind team.

A local project

For real work, install Tailwind with its CLI, PostCSS or your framework's integration. The build step scans your files and outputs only the CSS you use, giving you a small, cacheable stylesheet. Check the current official documentation for installation, because setup differs between major versions.

Converting existing designs

If you are migrating a site, it can help to prototype the same component in plain CSS in the HTML CSS JS Editor, then rewrite it in utilities. Comparing the two side by side makes the mapping clear.

Tips and pitfalls

Classes built from strings do not work

Tailwind finds classes by scanning your source files as text. A class assembled at runtime, such as "bg-" + colour + "-500", never appears in full, so it is not generated in a production build. Write complete class names, even if that means a small lookup object. (The Play CDN is more forgiving, which can hide this problem until you build.)

Conflicting classes

p-4 p-6 on the same element does not mean "the last one wins" in the markup. The winner is whichever rule comes later in the generated CSS. Avoid conflicting utilities rather than relying on order.

Long class lists

Group classes in a consistent order (layout, spacing, typography, colour, states) and extract components when a pattern repeats. Editor plugins can sort classes automatically.

Comparing with other frameworks

Bootstrap gives you ready-made components; Tailwind gives you building blocks. Try the same layout in the Bootstrap Playground to feel the difference. Layout concepts carry over either way, and the Flexbox Playground and CSS Gradient Generator help with the underlying CSS.

Frequently asked questions

Is the Tailwind Play CDN suitable for production?

No. It generates CSS in the browser at runtime, which is slower and heavier than a build. Tailwind recommends it only for development and prototyping.

What does md: mean in Tailwind?

It applies the class from the medium breakpoint upwards, 768 pixels by default in v3. Without a prefix, a class applies at every width.

Can I use custom colours in the playground?

Yes, with arbitrary values such as bg-[#0f766e] or text-[rgb(15,118,110)], which work without changing the configuration.

Does @apply work with hover and responsive variants?

Yes. You can include variants such as hover:bg-indigo-700 in an @apply rule, as in the sample button.

Which version of Tailwind does the playground use?

The preview uses the Tailwind CSS v3 Play CDN. Class names are largely the same in newer versions, but configuration and some defaults differ, so check the official documentation for your project's version.

Try it now: Tailwind CSS Playground

Loading tool…