html - 将显示应用于某些第 n 个 child

标签 html css css-selectors

我想显示所有值小于或等于 4 的第 n 个 child 。是否有适用于我要显示的条件的 CSS if 语句?

谢谢

最佳答案

是的。 :nth-child 接受一个 n 参数,该参数将采用所有非负整数值。

:nth-child(-n+4)
  • n=0:选择第 4 个 child
  • n=1:选择第 3 个 child
  • n=2:选择第二个 child
  • n=3:选择第一个 child
  • n>3:没有

例子:

div {
  margin: 10px;
}
div:nth-child(-n+4) {
  background: green;
}
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>

关于html - 将显示应用于某些第 n 个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890691/

相关文章:

html - 在边框 css 上显示文本

javascript - 代码在不该运行的时候运行?

html - 带有 <br/> 的文本框之间的间距不正确

html - CSS 不适用于带有 Gridview 的 div

php - 根据选择器特异性排序 CSS

jQuery:.get 之后的 dom 插入选择器问题

html - ionic4 ionic 选择占位符不透明度无法更改

javascript - 带有可滚动下拉菜单的输入字段

jQuery 使用 CSS 属性查找 <li>

html - 没有 href 属性的样式 <a>