Tailwind Buttons
Buttons are the first component most people build with Tailwind. A good button combines nearly everything you've learnt so far: spacing, colour, typography, borders, states and transitions.
Anatomy of a button
<button class="rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white shadow-xs
hover:bg-indigo-500 focus-visible:outline-2 focus-visible:outline-offset-2
focus-visible:outline-indigo-600 active:bg-indigo-700 disabled:opacity-50">
Save changes
</button>| Concern | Classes |
|---|---|
| Shape | rounded-lg (or rounded-full for pills) |
| Size | px-4 py-2 text-sm |
| Colour | bg-indigo-600 text-white |
| Feedback | hover: / active: shades one step lighter / darker |
| Accessibility | a visible focus-visible: outline for keyboard users |
| Disabled | disabled:opacity-50 disabled:cursor-not-allowed |
Variants: primary, secondary, ghost, danger
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="flex flex-wrap items-center gap-3 p-6">
<button class="rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white shadow-xs transition hover:bg-indigo-500 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 active:bg-indigo-700">Primary</button>
<button class="rounded-lg bg-white px-4 py-2 text-sm font-semibold text-gray-900 shadow-xs ring-1 ring-gray-300 transition ring-inset hover:bg-gray-50">Secondary</button>
<button class="rounded-lg px-4 py-2 text-sm font-semibold text-indigo-600 transition hover:bg-indigo-50">Ghost</button>
<button class="rounded-lg bg-red-600 px-4 py-2 text-sm font-semibold text-white shadow-xs transition hover:bg-red-500">Delete</button>
<button disabled class="rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white disabled:cursor-not-allowed disabled:opacity-50">Disabled</button>
</body>
</html>Tip: A
ring-1 ring-insetborder on the secondary button adds no width, so it lines up perfectly with the primary button next to it.
Sizes
Change only padding and font size; keep everything else identical.
| Size | Classes |
|---|---|
| Extra small | px-2 py-1 text-xs |
| Small | px-3 py-1.5 text-sm |
| Medium | px-4 py-2 text-sm |
| Large | px-5 py-2.5 text-base |
| Extra large | px-6 py-3 text-lg |
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="flex flex-wrap items-center gap-3 p-6">
<button class="rounded-md bg-teal-600 px-2 py-1 text-xs font-semibold text-white">XS</button>
<button class="rounded-md bg-teal-600 px-3 py-1.5 text-sm font-semibold text-white">Small</button>
<button class="rounded-lg bg-teal-600 px-4 py-2 text-sm font-semibold text-white">Medium</button>
<button class="rounded-lg bg-teal-600 px-5 py-2.5 text-base font-semibold text-white">Large</button>
<button class="rounded-xl bg-teal-600 px-6 py-3 text-lg font-semibold text-white">X-Large</button>
<button class="w-full rounded-lg bg-teal-600 py-2.5 font-semibold text-white">Full width (w-full)</button>
</body>
</html>Icons, pills and loading states
Use inline-flex items-center gap-2 so icons and text line up. A spinner is just a bordered circle with animate-spin.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="flex flex-wrap items-center gap-3 p-6">
<button class="inline-flex items-center gap-2 rounded-lg bg-gray-900 px-4 py-2 text-sm font-semibold text-white hover:bg-gray-700">
<svg viewBox="0 0 20 20" class="size-4 fill-current"><path d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z"/></svg>
New file
</button>
<button class="rounded-full bg-pink-500 px-5 py-2 text-sm font-semibold text-white shadow-lg shadow-pink-500/30 hover:bg-pink-400">Pill button</button>
<button class="flex size-10 items-center justify-center rounded-full bg-amber-400 text-lg hover:bg-amber-300" aria-label="Like">❤️</button>
<button disabled class="inline-flex items-center gap-2 rounded-lg bg-sky-600 px-4 py-2 text-sm font-semibold text-white opacity-80">
<span class="size-4 animate-spin rounded-full border-2 border-white/40 border-t-white"></span>
Saving…
</button>
</body>
</html>Button groups
Join buttons by removing the inner corners and overlapping borders:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-6">
<div class="inline-flex rounded-lg shadow-xs *:-ml-px *:bg-white *:px-4 *:py-2 *:text-sm *:font-medium *:text-gray-700 *:ring-1 *:ring-gray-300 *:ring-inset *:first:ml-0 *:first:rounded-l-lg *:last:rounded-r-lg *:hover:bg-gray-50 *:focus:z-10">
<button>Years</button>
<button>Months</button>
<button>Days</button>
</div>
</body>
</html>The *: variant applies each class to every child, so the buttons themselves need no classes. Stacked variants like *:first: read as "every child, if it is the first".
Gradient and 3D buttons
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="flex flex-wrap items-center gap-5 p-8">
<button class="rounded-xl bg-linear-to-r from-fuchsia-500 to-orange-400 px-6 py-3 font-bold text-white shadow-lg transition hover:scale-105 hover:shadow-xl">Gradient</button>
<button class="rounded-xl border-b-4 border-emerald-700 bg-emerald-500 px-6 py-3 font-bold text-white transition-all active:translate-y-1 active:border-b-0">3D press</button>
</body>
</html>Note: Use
<button>for actions and<a>for navigation, even when they look identical. Screen readers and keyboards treat them differently.
Test yourself
Which classes keep an icon and label vertically centred with a gap inside a button?
block text-centerinline-flex items-center gap-2grid grid-cols-2float-left mr-2
Why prefer focus-visible: over focus: for a button's focus outline?
- It works in older browsers
- It shows the outline for keyboard focus but not after a mouse click
- It is required for
<button> - It disables the hover style
To create size variants of the same button, what should usually change?
- Colours and shadows
- Padding and font size
- Border radius only
- The HTML element