Sub topic 3
Styling with core utilities
This chapter covers the utilities you will use most: spacing, typography, color, borders and shadows. It assumes you can read React components and JSX, know the CSS box model, and already have Tailwind running in a Vite project from the setup chapter.
The working model
Each utility class sets one CSS declaration, or a small group of related ones. You compose a design by listing classes on an element. Most core utilities follow a pattern of a short property prefix and a value from the theme scale.
| Prefix | Controls | Example | Resulting CSS |
|---|---|---|---|
| `p-`, `px-`, `pt-` | Padding | `p-6` | `padding: 1.5rem` |
| `m-`, `mt-`, `mx-` | Margin | `mt-1` | `margin-top: 0.25rem` |
| `text-` | Font size or color | `text-lg`, `text-gray-900` | Size or color |
| `font-` | Font weight or family | `font-semibold` | `font-weight: 600` |
| `bg-` | Background color | `bg-white` | `background-color: #fff` |
| `border-` | Border width or color | `border-gray-200` | Border color |
| `rounded-` | Border radius | `rounded-lg` | Rounded corners |
| `shadow-` | Box shadow | `shadow-sm` | Small shadow |
The spacing scale is based on a single step of 0.25rem. So p-4 is 1rem, p-6 is 1.5rem and p-8 is 2rem. Because the scale is shared, padding, margin, gap and width all line up on the same grid.
A worked example
This component uses only utilities.
export function ProfileCard({ name, role }: { name: string; role: string }) {
return (
<div className="max-w-sm rounded-lg border border-gray-200 bg-white p-6 shadow-sm">
<h2 className="text-lg font-semibold text-gray-900">{name}</h2>
<p className="mt-1 text-sm text-gray-600">{role}</p>
</div>
)
}Read the outer div in groups:
- Size:
max-w-smcaps the width at 24rem. - Surface:
bg-whitesets the fill. - Edge:
bordersets a 1px width, andborder-gray-200sets its color. - Shape and depth:
rounded-lgandshadow-sm. - Inside space:
p-6adds 1.5rem on all sides.
The heading combines size (text-lg), weight (font-semibold) and color (text-gray-900). The paragraph uses mt-1 to separate it from the heading by 0.25rem.
Colors
Color utilities pair a name with a shade from 50 (lightest) to 950 (darkest), such as gray-200 or blue-600. The same color value works with different prefixes: text-blue-600, bg-blue-600, border-blue-600. To make a color translucent, add an opacity modifier after a slash, as in bg-black/50.
A practical rule is to keep text at shade 600 or darker on light backgrounds. Check contrast when you pick lighter shades.
Spacing: padding, margin and gap
Padding sits inside the border and margin sits outside it. Use directional prefixes when you need one side or one axis: px-4 for left and right, py-2 for top and bottom, mt-1 for top only.
Prefer spacing the parent controls over margins on children. A parent with space-y-2 or a flex or grid gap-2 spaces its children evenly, so you do not have to remember a margin on every item. The next chapter covers gap in layout.
Common errors
- Writing
text-lgwhen you meant a color.text-is shared by size and color, and Tailwind tells them apart by the value.text-lgis a size andtext-red-600is a color. - Forgetting the border width.
border-gray-200alone only sets a color. Addborderto get a visible 1px line. - Mixing arbitrary pixel values into the scale too early. Stick to scale values until the design needs something else, which a later chapter covers.
- Expecting
m-andp-to behave the same on inline elements. Vertical margin has no effect on a plain inline element such asspan. Addinline-blockorblock.
Exercise
A div has the classes border-red-500 p-4 rounded-md. No border appears. What is missing, and what would you add to make the border 2px wide?
Show answer
The element has a border color but no border width. Add border for a 1px border, or border-2 for a 2px border.