How to Add Separators Between CSS List Items with :not(:last-child)
To draw a separator line only between list items, add border-bottom to li:not(:last-child). The last item matches :last-child, so :not() excludes it and no line is drawn below it. You can use this wherever an extra line would appear after the last item, such as a news list, a settings screen, or a footer menu with items separated by "|".
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the HTML in Playwright and read each element's border width and ::after
content with getComputedStyle(), and read line positions with getBoundingClientRect(). I did not test Firefox or Safari.
- 1. Drawing a Line Below Every Item Except the Last with li:not(:last-child)
- 2. Drawing the Line Above with li + li Looks the Same
- 3. Hiding an Item with hidden Can Leave a Stray Line at the Edge
- 4. When Another Element Follows the Items, Count Only the Items
- 5. Watch the Specificity When Removing One Line Later
Drawing a Line Below Every Item Except the Last with li:not(:last-child)
I tested this with a list of four items.
<ul>
<li>項目1</li>
<li>項目2</li>
<li>項目3</li>
<li>項目4</li>
</ul>
li:not(:last-child) {
border-bottom: 1px solid #ccc;
}
A line appeared below "Item 1" through "Item 3", and none appeared below "Item 4". As the MDN description of :not() explains, :not() matches elements that do not match the selector in its parentheses. :last-child matches the last element among its siblings, so li:not(:last-child) means "every li that is not the last one."

The same idea works when you want a "|" between items in a horizontal menu. Add text with ::after, and exclude the last item.
.menu {
display: flex;
}
.menu li:not(:last-child)::after {
content: "|";
margin-inline: 0.5em;
}
The ::after of "Home" and "Articles" contained "|", and nothing was added after "Contact".
Drawing the Line Above with li + li Looks the Same
You can also draw the separators above the second and later items. Using the next-sibling combinator
+, li + li matches any li that directly follows another li, which means every item from the second onward.
li + li {
border-top: 1px solid #ccc;
}
I measured the line positions in the same list. The lines drawn by li:not(:last-child) below the items and the lines drawn by li + li above them were both at y=37, 75, and 113. Both versions look the same. The difference appears when some items are hidden, as the next section shows.
Hiding an Item with hidden Can Leave a Stray Line at the Edge
When you hide an item with the hidden attribute, for example when filtering a list, the hidden item stays in the HTML. It still counts for :last-child and +. I added hidden to the last item, "Item 4", to test this.
<ul>
<li>項目1</li>
<li>項目2</li>
<li>項目3</li>
<li hidden>項目4</li>
</ul>
With li:not(:last-child), a line also appeared below "Item 3", which is the last visible item. This happens because :last-child matches the hidden "Item 4".
If you want to count only the visible items to find the last one, use of with :nth-last-child(). :nth-last-child(1 of :not([hidden])) matches the first element counting from the end among the siblings that do not have hidden.
li:not(:nth-last-child(1 of :not([hidden]))) {
border-bottom: 1px solid #ccc;
}
With this selector, lines appeared only below "Item 1" and "Item 2", and not below "Item 3".
![Two screens, one above the other. In the top screen, using li:not(:last-child), Item 4 is hidden, so a pink line remains below Item 3; the label next to it (in Japanese) means "the line remains." In the bottom screen, using li:not(:nth-last-child(1 of :not([hidden]))), light blue lines appear only below Item 1 and Item 2, and none below Item 3.](https://neko-note.org/wp-content/uploads/2026/11/css-not-last-separator-hidden-last.png)
The li + li version has the opposite problem when the first item is hidden: a line remains at the edge. When I added hidden to "Item 1", the first visible item, "Item 2", also got a line above it, and that line appeared at the top of the list (y=0). The hidden "Item 1" comes just before "Item 2", so "Item 2" matches li + li. With li:not(:last-child), hiding the first item still left lines only between the three visible items.
If the hidden item is at the end, use :nth-last-child(1 of :not([hidden])), which counts only visible items. If an item at the start might be hidden, use li:not(:last-child) for lines below the items rather than li + li for lines above them.
When Another Element Follows the Items, Count Only the Items
If an element other than the items, such as a "See more" link, comes last in the same parent, :last-child matches that element instead of the last item.
<div class="list">
<p class="item">記事A</p>
<p class="item">記事B</p>
<p class="item">記事C</p>
<a class="more" href="#">もっと見る</a>
</div>
With .item:not(:last-child), a line also appeared below "Article C", the last item. To fix this, use of again so that only .item elements are counted.
.item:not(:nth-last-child(1 of .item)) {
border-bottom: 1px solid #ccc;
}
Now the lines appear only below "Article A" and "Article B". In this HTML, the items are p elements and the last element is an a, so .item:not(:last-of-type) gave the same result. However, :last-of-type counts by element name (tag name), so it does not work if the last element is also a p. Counting with a class on the items means your selector is not affected when the element names of other elements change.
Watch the Specificity When Removing One Line Later
The specificity of :not() is the specificity of the selector inside its parentheses. If several selectors are listed, it is the highest of them (specificity rules in Selectors Level 4). li:not(:last-child) has the specificity of one element name and one pseudo-class. As a result, if you try to remove a line with .no-line, which has the specificity of one class, the rule loses.
li:not(:last-child) {
border-bottom: 1px solid #ccc;
}
.no-line {
border-bottom: none; /* no effect */
}
I added class="no-line" to the second item, but the line below "Item 2" stayed. Using either of the following forms removed the line below "Item 2".
/* Wrap in :where() so the specificity of :not(:last-child) becomes 0 */
li:where(:not(:last-child)) {
border-bottom: 1px solid #ccc;
}
.no-line {
border-bottom: none;
}
/* List the selectors you want to exclude inside :not() */
li:not(:last-child, .no-line) {
border-bottom: 1px solid #ccc;
}
The specificity of :where() is always 0, so the specificity of li:where(:not(:last-child)) is only that of li, and .no-line wins. Listing several selectors with commas inside :not() has the same meaning as :not(:last-child):not(.no-line), as the MDN description explains. If you already know which items to exclude, this form works with a single rule.





