Sub topic 2
Borders on Individual Sides
Assume you know basic HTML and CSS, understand Tailwind utility classes, and have a working Tailwind project. This chapter uses Tailwind’s default color and spacing scales.
Individual borders
Tailwind lets you style one side without changing the others:
border-t-2sets the top border width.border-r,border-b, andborder-ltarget the other sides.border-xtargets the left and right sides.border-ytargets the top and bottom sides.- Add a style or color, such as
border-dashedorborder-slate-300.
A side-specific color can override a general color:
<div class="border-2 border-dashed border-slate-300 border-b-emerald-500 p-4">
The bottom edge shows the main accent.
</div>The border-b-emerald-500 utility changes only the bottom border color.
Rounding corners
Use rounded-* utilities to control corner radius. For example, rounded-lg rounds every corner. You can target a group of corners with rounded-t-lg, rounded-r-lg, or rounded-bl-lg.
Radius changes shape, not spacing or border width. Use it with a border when you want the visible edge to follow the rounded shape.
Box shadows
Shadow utilities add depth without changing the element’s layout. The scale includes shadow-sm, shadow, shadow-md, shadow-lg, and larger values.
<div class="rounded-xl border border-slate-200 bg-white p-6 shadow-lg shadow-indigo-500/30">
A raised panel
</div>shadow-lg selects the size. shadow-indigo-500/30 gives the shadow an indigo color at 30% opacity. Use shadows carefully: too much contrast can make a flat interface harder to scan.
Hover and focus states
State variants add styles only when an interaction state is active. Prefix a utility with hover: or focus::
<button class="rounded-md border border-indigo-600 px-4 py-2 text-indigo-700 hover:bg-indigo-50 focus:outline-none focus:ring-2 focus:ring-indigo-500">
Save changes
</button>The hover background appears when the pointer is over the button. The focus styles appear when the button receives keyboard or pointer focus.
Rings and outlines
A ring is a box-shadow-based focus indicator outside the border. ring-2 sets its thickness, and ring-indigo-500 sets its color. If you remove the browser’s default outline with focus:outline-none, provide a visible replacement ring.
Prefer focus-visible: when you want the indicator mainly for keyboard navigation:
<input
type="text"
class="rounded-md border border-slate-300 px-3 py-2 focus-visible:border-indigo-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500"
/>Exercise
Which utility changes only the bottom border color, and which utility creates a two-pixel focus ring?
Show answer
Use border-b-emerald-500 for the bottom color and focus:ring-2 for the ring thickness.