Sub topic 10
Dividing sibling elements
This chapter covers the divide-* utilities, which draw lines between the children of a container. It assumes you know how Tailwind border width, style, and color utilities work (covered earlier in this tutorial), and that you can read the flex and flex-col utilities.
What divide utilities do
A list of settings, a menu, or a row of stats usually needs thin lines between its items. You could put border-b on every item and remove it from the last one. The divide-* utilities do this from the parent instead. You add one class to the container, and Tailwind adds borders between its direct children.
<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>Two classes on the ul do the work:
divide-yadds a horizontal line between each pair of stacked children.divide-slate-200sets the color of those lines.
The outer border border-slate-200 is a normal border on the ul itself. Divide utilities never draw an outer edge, only the lines between children.
The utility family
Each divide utility has a border counterpart. The difference is that it is set on the parent and applies to the children.
| Class | Effect |
|---|---|
| `divide-y` | 1px line between vertically stacked children |
| `divide-x` | 1px line between horizontally arranged children |
| `divide-y-2`, `divide-x-4`, `divide-x-8` | Thicker lines, same scale as `border-2`, `border-4`, `border-8` |
| `divide-y-0`, `divide-x-0` | Removes the lines |
| `divide-solid`, `divide-dashed`, `divide-dotted`, `divide-double`, `divide-none` | Line style |
| `divide-slate-200`, `divide-indigo-500/40` | Line color, with the same palette and slash opacity syntax as other color utilities |
| `divide-y-reverse`, `divide-x-reverse` | Corrects the line position when the visual order is reversed |
Here is a horizontal row of stats with dashed, thicker lines:
<div class="flex divide-x-2 divide-dashed divide-slate-300 rounded-lg border border-slate-200">
<div class="flex-1 p-4 text-center">12 open</div>
<div class="flex-1 p-4 text-center">48 closed</div>
<div class="flex-1 p-4 text-center">3 blocked</div>
</div>divide-x works with any layout where the children sit side by side. In this example the container is a flex row.
How it works internally
Divide utilities are shortcuts for a child selector. In Tailwind v3, divide-y generates CSS close to this:
.divide-y > :not([hidden]) ~ :not([hidden]) {
border-top-width: 1px;
}The selector means "any visible direct child that has a visible earlier sibling". The first child has no earlier sibling, so it gets no line. Every other child gets a border on its top edge. Tailwind v4 changed the generated selector and puts the line on the bottom of every child except the last, but the visible result is the same. The generated CSS is the authority for your version, so check it if a layout surprises you.
Three practical consequences follow from this:
- Only direct children are affected. A
divwrapped around the items will receive the lines instead of the items. - The line is a real border on the child, so it takes up space and is part of the child's box. It is not drawn in the container's
gap. - The border is a child's border, so utilities on the child (such as
border-0or aborder-red-500class) can interact with it. Check specificity if a child's own border classes seem ignored or overridden.
Setting the color explicitly
Always set a divide color. In Tailwind v3 the default border color is gray-200. In v4 the default is currentColor, which makes unstyled lines as dark as your text. Writing divide-slate-200 removes that difference and also lets you pair it with dark mode:
<ul class="divide-y divide-slate-200 dark:divide-slate-700">
<li class="py-3">Inbox</li>
<li class="py-3">Drafts</li>
<li class="py-3">Sent</li>
</ul>Opacity uses the slash modifier: divide-slate-900/10 gives a faint line that adapts to whatever background is behind it. In v3 you may also see divide-opacity-50, which still works there but is the older approach.
Responsive and conditional dividers
Divide utilities accept variants like any other class. A common pattern is a stack of cards on small screens that becomes a row on wider screens. The divider must change direction too:
<div class="flex flex-col divide-y divide-slate-200 md:flex-row md:divide-x md:divide-y-0">
<div class="flex-1 p-4">Plan</div>
<div class="flex-1 p-4">Usage</div>
<div class="flex-1 p-4">Invoices</div>
</div>Without md:divide-y-0, the horizontal lines would remain on wide screens in addition to the new vertical lines. The divide-y-0 class resets the width of the Y lines to zero.
Reversed order
When you reverse the visual order with flex-col-reverse or flex-row-reverse, the first child in the HTML appears last on screen. The line is still drawn on the same side of each child as before, so it lands on the wrong side. The -reverse utility moves it to the other side:
<ol class="flex flex-col-reverse divide-y divide-y-reverse divide-slate-200">
<li class="py-2">Step 1 (shown at the bottom)</li>
<li class="py-2">Step 2</li>
<li class="py-2">Step 3 (shown at the top)</li>
</ol>Note that divide-y and divide-y-reverse are used together. The second class only changes which side gets the border.
Common errors
- Lines appear at the container's edge, or not at all. Divide utilities only act between children. If you want an outer frame, add
borderon the parent. If one child is the only element, there is nothing to divide. - Lines do not appear where you expect. Check that the items are direct children. A wrapper element between the container and the items means the wrapper is the only child.
- A hidden item still affects layout. In v3, elements with the
hiddenattribute are skipped by the selector. An element hidden only with thehiddenutility class (display: none) is skipped for display but not by the selector in v3, which can produce a missing or doubled line. Remove the element or use the attribute if you toggle items dynamically, and test it. - Rounded corners look wrong. The lines are borders on the children. If a child has its own background and the parent is rounded, add
overflow-hiddento the parent so child backgrounds do not poke out of the corners. - Padding on the container pushes lines inward. Put padding on each child (
p-3), not on the parent, if you want the lines to reach the container edges.
Divide versus per-item borders
Divide is not always the best choice. This table compares the options:
| Approach | Best for | Limits |
|---|---|---|
| `divide-y` on the parent | Uniform lists where every separator looks the same | Hard to style one separator differently |
| `border-b last:border-b-0` on each child | Items that need different borders, or items generated from a component | More classes, repeated on every item |
| `space-y-*` | Empty space between items with no visible line | Draws nothing |
| `gap-*` with bordered cards | Separate cards with a visible gap | Each card has a full border |
If every separator is identical, use divide. If one item needs a thicker or colored line, switch to per-item borders or override a single child with a utility such as border-t-indigo-500 on it.
Summary
Divide utilities put line styling on a container and apply it to the gaps between direct children. divide-x and divide-y set the direction, numeric suffixes set the thickness, divide-{style} sets the style, and divide-{color} with the slash opacity modifier sets the color. Set the color yourself, handle direction changes at breakpoints, and use -reverse when visual order is flipped.
Exercise
A settings panel is a div that stacks three rows vertically on mobile and lays them out side by side from the lg breakpoint. You want a 1px slate-200 line between the rows in both layouts. Write the container classes.
Show answer
Use flex flex-col divide-y divide-slate-200 lg:flex-row lg:divide-x lg:divide-y-0. The divide-y gives horizontal lines when stacked. At lg, lg:divide-x adds vertical lines and lg:divide-y-0 removes the horizontal ones, so both do not show together.
Exercise
You render a list with flex flex-col-reverse divide-y divide-slate-200, and the separators appear on the wrong side of each item. What do you add, and why?
Show answer
Add divide-y-reverse. Reversing the visual order moves each item, but the border is still placed on the original side. divide-y-reverse swaps the side that receives the border so the lines sit between the items on screen.