Component Patterns — Tailwind CSS

In Tailwind, a UI component (a part of the page used again and again, such as a card or a button) is a recipe: a set of classes that always go together. Card…

A profile card in any app holds much the same things: a round photo, a slightly bold name, a box with rounded corners and a light shadow. They look alike because the recipe really is the same every time.

A UI component is a part of the page that is used again and again, such as a card, a button, a badge (a small label) or an avatar (a small profile photo). In Tailwind, each component has a recipe: a set of classes that always go together. It's like a fried rice recipe: the ingredients stay the same (rice, garlic, soy sauce), but the amounts can change (spicy or not). A card recipe works the same way. The ingredients are the kinds of class: rounded corners, a border, padding and a light shadow. The amounts are the sizes in each class, for example 12px or 8px corners. Once you know the recipe, you only change the amounts.

A profile card, class by class

This is the same profile card as in the Lab and the exercise:

<div id="profile-card" class="rounded-xl border p-6 shadow-sm">
  <img id="avatar" class="h-16 w-16 rounded-full" src="https://picsum.photos/96/72" alt="avatar">
  <h3 id="name" class="text-lg font-semibold">Budi</h3>
</div>
Class CSS it generates Meaning
rounded-xl border-radius: 0.75rem; 12px rounded corners
border border-width: 1px; a 1px border
p-6 padding: 1.5rem; 24px of padding
shadow-sm box-shadow: 0 1px 3px ...; a light shadow under the card
h-16 w-16 height: 4rem; width: 4rem; a 64 × 64px photo
rounded-full border-radius: calc(infinity * 1px); corners rounded all the way
text-lg font-size: 1.125rem; 18px text
font-semibold font-weight: 600; semi-bold

Avatar: square first, then round

rounded-full gives the biggest radius possible. The browser then shrinks that radius to half the element's shortest side. On a 64 × 64px square, half a side is 32px in every direction, so the result is a circle.

Without h-16 w-16, the photo in this card shows at its real size, 96 × 72px, and doesn't become a circle. Its shortest side is 72px, so the radius shrinks to 36px. The left and right ends are half circles, and the middle is straight: a pill. That's why an avatar is always made square first with h-16 w-16, then rounded.

In Tailwind v4, rounded-full is written calc(infinity * 1px). In v3 it was 9999px. The result is the same.

Buttons: padding, colour, corners and state

This is the button recipe:

<button class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors">
  Save
</button>

Another card recipe you'll see a lot: bg-white rounded-lg p-6 shadow-md. Same pattern: background, rounded corners, padding and shadow.

One place for the classes

If the same card appears 10 times, don't copy its classes 10 times. Later, to change just one class, you'd have to change it in 10 places.

@import "tailwindcss";

@layer components {
  .btn {
    @apply px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors;
  }
}
@reference "../app.css";

.card {
  @apply p-4 rounded-xl;
}

@reference only borrows the list of classes from the main file, without copying all of Tailwind's CSS into that file.

⚠️ Squashed avatars and borders that come out too dark

TL;DR: In Tailwind, a UI component (a part of the page used again and again, such as a card or a button) is a recipe: a set of classes that always go together. Card: rounded-xl border p-6 shadow-sm (12px corners, 1px border, 24px padding, light shadow). Avatar (profile photo): h-16 w-16 rounded-full. Make it square first, then round, because rounded-full on a 96 × 72 photo becomes a pill. Name: text-lg font-semibold, because Tailwind removes the default heading styles. Button: px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors, and transition-colors makes the colour change smooth. A card that repeats is written once as a component in your code, for example in React or Vue. @apply (a CSS command that puts Tailwind classes into a class of your own) is only for when a component can't be used.

⚡ The profile card recipe

Write:

<div id="profile-card" class="rounded-xl border p-6 shadow-sm">
  <img id="avatar" class="h-16 w-16 rounded-full" src="https://picsum.photos/96/72" alt="avatar">
  <h3 id="name" class="text-lg font-semibold">Budi</h3>
</div>

Meaning: A component such as this card has a recipe: a set of classes that always go together. Card: rounded-xl border p-6 shadow-sm, that is rounded corners, border, padding and shadow. Avatar (profile photo): h-16 w-16 rounded-full. The name needs text-lg font-semibold, because Tailwind removes the default heading styles.

⚡ Square first, then round

Guess first: does rounded-full (corners rounded all the way) on a 96 × 72px photo make a circle?

No, a pill. The radius shrinks to half the shortest side, 36px, so the middle stays straight.

Meaning: Make the avatar square first with h-16 w-16 (64 × 64px), then round it.

In the exercise, the checker (the program that checks your answer) looks for a 64px height and width, and the rounded-full class on the photo.

⚡ A button with a smooth colour change

Write:

<button class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors">
  Save
</button>

Meaning: hover:bg-blue-600 makes the button a little darker while the pointer is over it, and transition-colors makes the colour change smooth. Without transition-colors, the colour jumps straight to the darker blue.

What you will learn