Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the three first divs

In CSS I want to select the three first divs. I have this code:

div:nth-child(3n) {
  background: red;
}
<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore laborum necessitatibus nobis obcaecati, mollitia, eos sint dolor odit. Possimus dolores recusandae sed totam, voluptatibus, voluptatum. Voluptatibus minus aut, quam ratione.
</div>

<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates perferendis et saepe omnis nemo, dolores quia ipsam ea blanditiis quaerat autem aut id itaque magnam recusandae sint architecto! Error, consequuntur.
</div>

<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem doloremque quis perspiciatis vel odio impedit itaque laborum eveniet quasi aperiam, autem cumque vero recusandae, voluptates et nesciunt quibusdam aliquid! Deleniti.
</div>

<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem doloremque quis perspiciatis vel odio impedit itaque laborum eveniet quasi aperiam, autem cumque vero recusandae, voluptates et nesciunt quibusdam aliquid! Deleniti.
</div>

I try above CSS but it does not work.

like image 465
Corentin Branquet Avatar asked Feb 11 '16 16:02

Corentin Branquet


People also ask

How do you select the first element in a Div?

Using the :first-child selector, you can target or select the very first element inside an element, like a DIV. You can use any other element as a container like <section> or <article> etc. Here’s an example. I have three child elements inside the parent <div> element.

How do you select the first element in a group?

The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).

What is the difference between the first and first child selector?

The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent).

What is a first of type selector in CSS?

CSS :first-of-type Selector 1 Definition and Usage. The :first-of-type selector matches every element that is the first child, of a particular type,... 2 Browser Support. The numbers in the table specifies the first browser version that fully supports the selector. 3 CSS Syntax. More ...


1 Answers

It's not working as expected because :nth-child(3n) will select every third element.

You need to select every element starting from the third one counting backwards, therefore you could use -n + 3 or -1n + 3.

In other words, given the pattern an+b, a should be 1 (or omitted) since you don't want to skip over elements. In addition, a should also be negative and b should be 3 since you're starting at the third element.

div:nth-child(-1n + 3) {
  background: #f00;
}
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>

It's worth pointing out that div:nth-child(-1n + 3) will only select the element if it is one of the first three elements and also an element type div. In other words, if the third element is a span, only the first two div elements will be selected:

div:nth-child(-1n + 3) {
  background: red;
}
<div>First div</div>
<div>Second div</div>
<span>Span</span>
<div>Third div</div>

If the element types vary (like above), then you should use :nth-of-type() instead:

div:nth-of-type(-1n + 3) {
  background: red;
}
<div>First div</div>
<div>Second div</div>
<span>Span</span>
<div>Third div</div>
like image 74
Josh Crozier Avatar answered Nov 06 '22 09:11

Josh Crozier