【发布时间】:2016-05-29 10:12:22
【问题描述】:
我有一些与提供的 sn-p 行为方式相同的东西。我有一个项目列表,当一个项目被选中时,我希望那个项目显示在列表的顶部,我可以通过弹性排序轻松做到这一点。一次只能选择一个。但是由于每个列表元素都有边框,所以最后一个元素不应该有边框。在大多数情况下,只需使用 li:last-of-type 即可正常工作,除非它是最后一个被选中的列表项。
我已经尝试了几个选择器,它们应该选择没有给定类的无序列表中的最后一个列表项,但 last-of-type 选择器的行为似乎不正常。
如果代码中不清楚,我指的选择器是.selection li:not(.selected):last-of-type。问题是列表底部的双边框。它应该是来自无序列表元素的单个边框。
.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>
我还创建了这个Codepen snippet 来演示这个问题。
我感觉这是:last-of-type 选择器的问题,也许我用错了,但在我看来,上面的选择器应该可以工作。
任何有关如何解决此问题的帮助或见解,或选择元素的其他方式,或对为什么这不起作用的见解将不胜感激。
【问题讨论】:
-
这听起来就像
nth-of-class的问题,当然,不存在。 -
您还应该知道
nth-of-type与 DOM 一起使用,但 flexboxorder是纯粹的视觉。 -
嗯,它更像是
last-of-not-class。 -
Potayto / Potahto :)
-
我真的很喜欢 /** 将其置于上下文中的非重要样式 **/ .. 我希望所有发布问题的人都可以这样做(而不是与问题无关的一行行代码)
标签: html css css-selectors flexbox