问题
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