html - 如何使用 CSS 仅选择前三个元素

标签 html css css-selectors

如何使用 :nth-child() 仅选择前三个元素选择器?

section > figure:nth-child( ? ) {
  /* ... */
}
<section>
  <figure>1</figure> <!-- select this -->
  <figure>2</figure> <!-- and this -->
  <figure>3</figure> <!-- and this -->
  <figure>4</figure>
  <figure>5</figure>
  <figure>6</figure>
</section>

最佳答案

你可以这样做:

section > figure:nth-child(-n+3) {background: Aqua}
<section>
  <figure>1</figure>
  <figure>2</figure>
  <figure>3</figure>
  <figure>4</figure>
  <figure>5</figure>
  <figure>6</figure>
</section>

关于html - 如何使用 CSS 仅选择前三个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406795/

相关文章:

javascript - 如何从 Javascript 初始化 Web 组件?

html - .css 或其他文件中的 <css> 标签

javascript - JQuery .offset() 没有设置 'top' 值

CSS nth-child() 和 :eq() doesn't work in my code

html - Bootstrap 4 col-sm-6 inside col-sm-6 不工作

css - 使用 javascript 将 div 的样式设置为 "none"或 "block"在 IE9 中不起作用,但在 Chrome 中有效

javascript - 将 CSS 样式应用于数据表子 tr

javascript - 悬停时使 "▶ "出现在链接之前?

CSS :not pseudo class is not taking effect

css - 为什么不能将特定于供应商的伪元素/类组合到一个规则集中?