【问题标题】:How can I select specific number of last child using CSS?如何使用 CSS 选择特定数量的最后一个孩子?
【发布时间】:2017-12-25 01:17:19
【问题描述】:

例如,总共有 12 个元素。 我只想在总共 12 个元素的最后 n (1, 2, 3...) 个子元素上应用我的样式;我该怎么做?

【问题讨论】:

  • 发布您的代码,以便我们重现问题。
  • 约瑟夫解决了。无论如何,这里是代码: &:not(:nth-last-child(-n+#{$column_number}))

标签: css css-selectors


【解决方案1】:

我认为您正在寻找的是 css 伪类 :nth-last-child(参见 MDN

只用一个负的n值,从末尾添加索引(代表元素个数)

 .parentClass > * :nth-last-child ( -n + 3 ) {
    /* CSS here for last 3 elements */
 }

https://jsfiddle.net/evm0mg8j/

【讨论】:

  • 问题已解决;谢谢! &:not(:nth-last-child(-n+#{$column_number}))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 2011-09-18
  • 2019-03-28
  • 1970-01-01
  • 2019-10-01
  • 2018-07-16
相关资源
最近更新 更多