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 |
- The first four classes go on
#profile-card, the parent of the photo and the name. These four are the card recipe: corners, border, padding and shadow. h-16 w-16 rounded-fullgo on the photo. These three are the avatar recipe.- Preflight, the base CSS that Tailwind adds, removes the browser's default heading styles. With no classes, an
<h3>looks like plain text: 16px and not bold. That's why the name needstext-lg font-semibold.
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>
px-4 py-2: 16px of padding left and right, 8px top and bottom.bg-blue-500 text-white: a blue button with white text.rounded-lg: 8px rounded corners.hover:bg-blue-600: the button gets a little darker while the pointer is over it.transition-colors: makes the colour change smooth, over 150 milliseconds. Without this class, the colour jumps straight to blue 600.
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.
- Use a component. In React, Vue or a template such as Blade (tools that build a page from pieces of code), write the card once as a component, then use it again and again with different data. The classes stay in one place.
@applywhen you can't use a component, such as HTML from another library that you can't give classes to.@applyis a Tailwind command in a CSS file: it puts Tailwind's classes into a class of your own. In the CSS file:
@import "tailwindcss";
@layer components {
.btn {
@apply px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors;
}
}
@applycopies the CSS of those classes into.btn, so<button class="btn">looks the same as the Save button above.@layer componentsgroups.btnwith the other component rules. Ordinary Tailwind classes in the HTML can still override it.- Use it sparingly. If everything becomes
@apply, you're back to writing CSS by hand, and you lose what Tailwind gives you. @applyin a separate CSS file (a<style>block in Vue or Svelte, or a CSS module) fails with the error "Cannot apply unknown utility classp-4". The reason: that file is processed apart from your main CSS file, so Tailwind's classes don't exist in it yet. Write@referenceon its first line, pointing to your main CSS file:
@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
- A non-square avatar.
rounded-fullon a 96 × 72 photo becomes a pill. Addh-16 w-16first. In the exercise, the checker looks for a height of64px, a width of64pxand therounded-fullclass on the photo. - A squashed photo. Forcing a 96 × 72 photo into 64 × 64 squeezes the picture. Add
object-cover(object-fit: cover), so the photo is cropped neatly instead of squeezed. - In v4,
borderfollows the text colour. In Tailwind v4 the default border colour iscurrentColor, the element's text colour. Soborderalone draws a line as dark as the text. (In v3 the default was a light grey.) For a soft line, addborder-gray-200. - A name that isn't bold. An
<h3>withoutfont-semiboldisn't bold in Tailwind. In the exercise, the checker looks for afont-weightof600on#name.
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.