I've got something that behaves the same way as the provided snippet. I've got a list of items, that when one is selected, I want that one to be displayed at the top of the list, which I can easily do with flex ordering. Only one can be selected at a time. But since each of the list elements have borders, the last element shouldn't have a border. Simply using the li:last-of-type
works fine for most cases, except for when the it's the last list item that is selected.
I've tried several selectors that should select the last list item in a unordered list that doesn't have a given class, but the last-of-type
selector doesn't seem to be behaving properly.
In case it isn't clear in the code, the selector I'm referring to is .selection li:not(.selected):last-of-type
. And the problem is the double border at the bottom of the list. It should be a single border coming from the unordered list element.
.selection ul {
display: flex;
flex-direction: column;
border: .15rem solid #666;
}
.selection li {
order: 2;
border-bottom: .15rem dashed #666;
}
.selection li.selected {
order: 1;
}
/** This selector should work in my mind, but it doesn't **/
.selection li:not(.selected):last-of-type {
border-bottom: none;
}
/** Non-important styles to put it into context **/
ul { list-style: none; padding: 0; width: 25%; margin: 2rem auto; }
li { padding: 1rem 2rem; background-color: #ebebeb; }
li:hover { background-color: #ccc; }
a { color: inherit; text-decoration: none; }
<div class="selection">
<ul>
<li><a href="">1</a></li>
<li><a href="">2</a></li>
<li class="selected">3</li>
</ul>
</div>
I've also created this Codepen snippet to demonstrate the problem.
I have a feeling this is a problem with the :last-of-type
selector, and maybe I'm using it wrong, but in my mind, the above selector should work.
Any help or insight into how to fix this, or another way to select the element, or insight into why this doesn't work would be appreciated.