How to select only first three elements using CSS

倖福魔咒の 提交于 2020-11-29 04:09:29

问题


How can I select only first three elements with the :nth-child() selector?

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>

回答1:


You can do it like this:

section > figure:nth-child(-n+3) {
  border: 1px solid;
}
<section>
  <figure>1</figure>
  <figure>2</figure>
  <figure>3</figure>
  <figure>4</figure>
  <figure>5</figure>
  <figure>6</figure>
</section>


来源:https://stackoverflow.com/questions/50406795/how-to-select-only-first-three-elements-using-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!