选择最后一个直接孩子

时间:2012-10-27 14:25:48

标签: css

我正在尝试选择一组元素的最后一个直接子元素,例如,最后一个.container:

enter image description here

问题在于它没有应用样式。这是我正在尝试使用的代码:

#allthecontent  > .container:last-child {
  overflow: auto;
  padding-bottom: 150px;
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

虽然不可能匹配.container的最后一次出现,除非它恰好是其父项的最后一个子项,但您可以使用nth-last-child选择器来指定范围。

#allthecontent > ​.container:nth-last-child(-n + 2) {
  overflow: auto;
  padding-bottom: 150px;
}

这将匹配任何具有container类的元素,该类出现在父元素的最后2个元素中。但是,此选择器在IE8及更低版本中不起作用。

相关问题