滥用 CSS3 的 nth-child 选择器来发明新的选择器

Avatar of Chris Coyier
Chris Coyier

DigitalOcean 为您旅程的每个阶段提供云产品。立即开始使用 $200 免费额度!

Matt Mastracci 介绍了如何以有趣的方式组合现有的位置选择器,以创建您可能从未想到过的逻辑。例如,选择所有元素,仅当它们有五个,使用

span:first-child:nth-last-child(5),
span:first-child:nth-last-child(5) ~ span { 
  /* select the span if its BOTH first and 5th from the end, then all spans after it. */
}

其用处在于应用特殊的布局以适应未知的标记。

让我想起了我看到 Estelle Weyl 做的一个旧的 CSS 技巧,她可以在不知道有多少列的情况下完美地调整列大小。

直接链接 →