Sub topic 10
Dividing Sibling Elements
This chapter assumes you know basic HTML and CSS, the Tailwind border utilities from earlier chapters, and how to apply utility classes in a working project.
What divide utilities do
Sometimes you need a line between items in a list, but not around the outside. You could put a bottom border on every item and then remove it from the last one. Tailwind's divide utilities avoid that work. You put them on the parent, and they style the borders between its direct children.
Divide utilities use a sibling selector internally. They add a border to every child except the first, so you never get a stray line at the edge.
Width and direction
divide-y draws horizontal lines between stacked children. divide-x draws vertical lines between children placed side by side. Both default to a 1px line. You can set a width with a number, as in divide-y-2 or divide-x-4.
<ul class="divide-y divide-slate-200 rounded-lg border border-slate-200">
<li class="p-3">Profile</li>
<li class="p-3">Billing</li>
<li class="p-3">Security</li>
</ul>Here divide-y creates the lines between the three items. divide-slate-200 sets their color. The border border-slate-200 rounded-lg classes add an outer frame. The divide utilities do not draw that frame.
Color, style, and opacity
Divide color uses the same palette and shade numbers as other color utilities, so divide-slate-200 and divide-indigo-500 both work. The slash opacity modifier works too, as in divide-black/10. Style utilities such as divide-dashed and divide-dotted change the line style.
| Utility | Effect |
|---|---|
| `divide-y` | 1px line between stacked children |
| `divide-x-2` | 2px vertical line between side-by-side children |
| `divide-slate-200` | Line color from the palette |
| `divide-black/10` | Line color at 10% opacity |
| `divide-dashed` | Dashed line style |
Using divide with flex layouts
Divide utilities work on flex rows too. This example places three stats in a row, with a vertical line between them.
<div class="flex divide-x divide-slate-300 rounded-lg border border-slate-300 text-center">
<div class="flex-1 p-4">12 open</div>
<div class="flex-1 p-4">48 closed</div>
<div class="flex-1 p-4">3 draft</div>
</div>The divide-x class is on the flex container, and each child gets a left border except the first.
Common errors
- Putting the class on the children.
divide-ybelongs on the parent. On a child it does nothing useful. - Expecting lines around grandchildren. Divide targets direct children only. If you wrap each item in another element, the lines appear around the wrappers.
- Expecting an outer border. Divide never draws lines on the outer edges. Add
borderto the parent if you want a frame. - Mixing divide with child borders. If a child has its own border classes, they can collide with the divide lines. Use one approach per list.
- Hidden children. A child that is hidden with
display: nonedoes not count as an element in the layout, but the divide border still applies to whichever visible child is not first in the DOM. If you toggle items, check the result.
Choosing between divide and borders
Use divide when all items are siblings and share the same separator. Use per-side borders when only some items need a line, or when each item needs different styling. Divide gives you less code and no special case for the last item.
Exercise
You want three side-by-side buttons in a flex row, separated by thin dashed gray lines, with no line before the first or after the last. Which classes go on the flex container?
Show answer
Use flex divide-x divide-dashed divide-slate-300. The divide-x class draws vertical lines between the children, divide-dashed sets the style, and divide-slate-300 sets the color. Divide utilities never draw lines on the outer edges.