html - :before to all children except :first-child

标签 html css

我有一个水平导航。我试图用“|”分隔每个导航项(管道)仅使用 css。

HTML

<ul>
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
    <li>Fourth</li>
    <li>Fifth</li>
</ul

现在看起来像这样

First   Second   Third   Fourth   Fifth

我希望它看起来像这样

First | Second | Third | Fourth | Fifth

我可以使用 css 来放置一个“|”在每个 <li> 之前

li:before {
    content:"|";
}

结果如何

|  First | Second | Third | Fourth | Fifth

如何在不添加“|”的情况下执行此操作到第一项?

最佳答案

您可以使用 :not伪类:

li:not(:first-child):before {
  content: "|";
}

ul {
  display: flex;
  list-style: none;
}
li:not(:first-child):before {
  content: "|";
  padding: 5px;
}
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
  <li>Fifth</li>
</ul>

关于html - :before to all children except :first-child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533174/

相关文章:

jquery 自动调整大小的 div 集

jquery - 根据表格单元格值更改行的颜色

jquery - 如何保持侧面菜单打开

html - 为什么 flex 元素不缩小过去的内容大小?

javascript - 在多列 DIV 中使用 CSS 工具提示

html - 如何使图像上的多个单选按钮响应?

html - 当我尝试选择任何元素时,下拉菜单会隐藏。??(CSS)

javascript - asp 隐藏单选按钮标签

asp.net - 页面文档模式

html - 连续对齐 3 个部分标签