Sub topic 5
Borders on individual sides
This chapter assumes you know how border, border-2, border-dashed, and the color utilities work. It also assumes you are comfortable with the CSS box model. Here you will learn how to put borders on one side, one axis, or one logical edge, and how to color each side separately.
Side and axis utilities
The plain border class sets the width on all four sides. To limit it, add a side letter after border-.
| Class | Applies to |
|---|---|
| `border-t`, `border-r`, `border-b`, `border-l` | Top, right, bottom, left |
| `border-x` | Left and right |
| `border-y` | Top and bottom |
| `border-s`, `border-e` | Start and end (logical sides) |
Each side utility also takes a width: border-t-2, border-b-4, border-x-8, and so on. The scale is the same as for border-*: 0, 2, 4, 8, plus the bare class for 1px.
The logical utilities border-s and border-e follow the text direction. In a left-to-right layout, border-s is the left edge. In a right-to-left layout, it is the right edge. Use them when the same component must work in both directions.
<div class="border-b-2 border-slate-300 p-4">
Only the bottom edge has a border
</div>Coloring and styling sides
Color and style utilities work on all sides by default. To color one side, add the side letter: border-t-red-500, border-b-emerald-500, and so on. Side colors override the general color for that side only.
<div class="rounded-lg border-2 border-dashed border-slate-300 border-b-emerald-500 p-4">
Dashed border with a green bottom edge
</div>In this example, border-2 sets the width on all sides. border-dashed sets the style on all sides. border-slate-300 sets the color on all sides, and border-b-emerald-500 replaces the color of the bottom side only.
Common errors
- A side color appears to do nothing. A color utility only shows if that side has a width.
border-t-red-500on its own draws nothing, because the default border width in the browser is 0 once Tailwind's reset is applied. Addborder-tas well. - The wrong side colors win. A side color such as
border-b-emerald-500overrides the generalborder-slate-300for that side. If the general color seems to win, check that the side-specific class is actually spelled correctly and is not being purged or mistyped. - Corners look odd with different colors. Where two sides with different colors meet, the browser draws a diagonal join. With thick borders, this looks like a mitered corner. Reduce the width or use a single color if you do not want it.
- Rounded corners with a single-side border. A
rounded-lgelement with onlyborder-l-4produces a curved stripe that tapers at the corners. This is how CSS works. For a clean accent bar, consider a pseudo-element or a separate child element.
A common pattern: accent edge
A left accent border is a frequent way to mark alerts and quotes.
<div class="border-l-4 border-indigo-500 bg-indigo-50 p-4 text-slate-800">
Your changes were saved.
</div>Here border-l-4 gives a 4px left border and border-indigo-500 colors it. No other side has a width, so only the left edge is drawn.
Exercise
You want a card with a 4px red border on the top edge only. You write class="border-t-red-500 p-4" and see no border. What is missing, and what is the fix?
Show answer
The top side has no width, so no color is drawn. Add border-t-4, giving class="border-t-4 border-t-red-500 p-4". You can also use border-t-4 border-red-500, because only the top side has a width.
Exercise
Which utility would you use for a divider line on the left edge that flips to the right edge in a right-to-left layout?
Show answer
border-s, for example border-s-2. It uses the start edge, which depends on the text direction.