Tailwind CSS Basics: Style with Utility Classes
Colours, Type and Borders
In this lesson you will
- Apply background, text and border colours with the palette and opacity modifiers
- Set font size, weight, line height and tracking
- Use rounded corners, borders, rings and shadows
Introduction
Colour, typography and surface styling are where a page starts to look designed. Tailwind provides a built-in palette and sensible scales, so you can get good results quickly.
Colours
Colour utilities have the form property-colour-shade, for example bg-blue-500 or text-gray-700. The palette has families such as gray, red, orange, amber, green, teal, blue, indigo, purple and pink, each with shades from 50 (very light) to 950 (very dark).
HTML
<p class="bg-blue-50 text-blue-900">Light background, dark text</p>
<p class="bg-blue-600 text-white">Strong background, white text</p>The same colours work for text (text-*), background (bg-*), borders (border-*), rings (ring-*), outlines, fills and more. In the output, bg-blue-500 becomes background-color: var(--color-blue-500), and text-gray-700 becomes color: var(--color-gray-700).
Opacity with a slash
Add / and a number to make a colour transparent: bg-blue-500/50 is 50 percent opaque. I compiled it and it produced a color-mix declaration with 50 percent of the colour and the rest transparent. In v4, the old bg-opacity-50 utilities were removed. The slash replaces them, and bg-opacity-50 generates nothing.
Quick quiz
Which class gives a black background at 50 percent opacity in Tailwind v4?
Contrast matters
Colour choices must stay readable. Text and background need enough contrast (WCAG asks for at least 4.5 to 1 for normal text). Light grays on white often fail. Prefer darker text shades, such as text-gray-700 or darker on light backgrounds, and test with a contrast checker.
Typography
| Utility | Examples | Notes |
|---|---|---|
| Size | text-xs, text-sm, text-base, text-lg, text-2xl | each sets font size and a default line height |
| Weight | font-normal, font-medium, font-semibold, font-bold | |
| Family | font-sans, font-serif, font-mono | from the theme |
| Line height | leading-6, leading-tight, leading-relaxed | |
| Letter spacing | tracking-tight, tracking-wide | |
| Alignment | text-left, text-center | |
| Other | uppercase, italic, underline, truncate, text-balance |
text-lg compiles to a font size and a matching line height from the theme. You can set both at once with a slash: text-sm/6 sets the font size to text-sm and line height to 6 spacing steps (1.5rem).
HTML
<h1 class="text-3xl font-bold tracking-tight text-gray-900">Welcome</h1>
<p class="mt-2 text-base leading-relaxed text-gray-600">
Short paragraphs are easier to read.
</p>text-balance makes headings wrap more evenly, and truncate cuts a single line with an ellipsis.
Corners and borders
- Rounded corners:
rounded-sm,rounded-md,rounded-lg,rounded-xl,rounded-full(circles and pills). - Borders:
borderadds a 1px border, andborder-2thicker. Set a colour withborder-gray-200. - Individual sides:
border-b,border-t.
Important v4 detail: the default border colour is currentcolor (the text colour), not light gray as in v3. Always set a border colour yourself:
HTML
<div class="rounded-lg border border-gray-200 p-4">Card</div>Shadows and rings
- Shadows:
shadow-xs,shadow-sm,shadow-md,shadow-lg. In v4 the names shifted one step:shadow-smis now whatshadowused to be, andshadow-xsis the smallest. - Rings:
ringdraws a 1px ring usingbox-shadow,ring-3a 3px ring, andring-blue-500colours it. I compiledringandring-3and confirmed the widths of 1px and 3px, withcurrentcoloras the default colour. - Outlines:
outline-2 outline-blue-500.outline-hiddenremoves the default outline visually but keeps one in forced-colors mode, which is better for accessibility thanoutline-none.
HTML
<button class="rounded-lg bg-blue-600 px-4 py-2 font-semibold text-white shadow-sm focus:outline-2 focus:outline-offset-2 focus:outline-blue-600">
Save
</button>Never remove focus styles without replacing them, since keyboard users rely on them.
Try it yourself
Build a profile card: a white card with rounded-xl border border-gray-200 p-6 shadow-sm, a bold name (text-lg font-semibold text-gray-900), a gray subtitle and a coloured badge (rounded-full bg-green-100 px-2 py-1 text-xs font-medium text-green-800). Check the contrast of each text colour.
Summary
- Colours follow
property-colour-shade, and a slash sets opacity, for examplebg-blue-500/50. - Type utilities set size, weight, family, line height and tracking, and
text-sm/6sets size and line height together. - In v4 the default border colour is
currentcolor, shadow and ring names shifted, and visible focus styles remain essential.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading