CSS :not(:last-child):after selector

标签 css css-selectors pseudo-element

我有一个元素列表,其样式如下:

ul {
    list-style-type: none;
    text-align: center;
}

li {
    display: inline;
}

li:not(:last-child):after {
    content:' |';
}
<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
    <li>Five</li>
</ul>

输出 一个 |两个 |三 |四 |五个 | 而不是 一个 |两个 |三 |四 |五

有人知道如何使用 CSS 选择除最后一个元素以外的所有元素吗?

可以看到:not()选择器的定义here

最佳答案

如果是not选择器的问题,可以全部设置,覆盖最后一个

li:after
{
  content: ' |';
}
li:last-child:after
{
  content: '';
}

或者如果你之前可以使用,就不需要last-child

li+li:before
{
  content: '| ';
}

关于CSS :not(:last-child):after selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5449872/

相关文章:

html - 有没有理由不使用单个非嵌套类名作为 CSS 选择器?

css - :before pseudo element not displaying image on input element

css - (CSS) 高度拉伸(stretch)与顶部 : 0 and bottom: 0 with Pseudo-Element?

jquery - 使用 fullpage.js 滚动到新 anchor 时移动导航

jquery - 如何让加载器等待特定的div

css - Flexbox 中的 Bootstrap 4 文本中心不起作用

html - 子元素的 ID 选择器

html - CSS 显示 :inline is not working

jquery - css 有没有办法选择倒数第二个 child ?

css - 我可以使用 :first-letter and the :hover selector together? 吗