Html 仅为列表中的某些项目设置样式

Html 仅为列表中的某些项目设置样式,html,css,Html,Css,有没有什么方法可以在html列表中设置一些项目的样式 比如说 有没有办法在列表中的最后两项周围加上边框?尝试了以下方法: :last-child, :nth-last-child(2) { ruleset } 可以使用在列表中的最后两个列表项(li元素)周围放置样式 li:nth-last-child(-n+2) { your styles go here } 请注意,IE8中不支持:nth-last-child()。您可以使用:not选择器: div ul:not(:firs

有没有什么方法可以在html列表中设置一些项目的样式

比如说

有没有办法在列表中的最后两项周围加上边框?

尝试了以下方法:

:last-child,
:nth-last-child(2) {
  ruleset
}
可以使用在列表中的最后两个列表项(
li
元素)周围放置样式

li:nth-last-child(-n+2) {
    your styles go here
}

请注意,IE8中不支持
:nth-last-child()

您可以使用
:not
选择器:

div ul:not(:first-child) {
    background-color: #900;
}
这将选择除第一个项目外的所有项目,而不管项目数量如何


此外,如果要在HTML中为循环构造一个
,则可以将类添加到forloop计数器中除第一个之外的所有项。

如果列表的长度已知,还可以使用第n个子伪类,例如:

li:nth-child(n+2) {
    border: 1px solid red;
}

这将在列表中除第一项外的所有项周围放置边框。

您也可以为第二个子元素设置边框

li:first-child + li  {
border: 1px
    li:first-child + li + l1 {
border: 1px
}

此选项用于将边框放置到第三个子元素

li:first-child + li  {
border: 1px
    li:first-child + li + l1 {
border: 1px

}

是的,这是可能的,您尝试过什么?