1

I've got this HTML structure and I want to target divs in CSS like this :

<div></div>
<div></div>
<div></div> <!-- SPECIFIC STYLE -->
<div></div>
<div></div>
<div></div> <!-- SPECIFIC STYLE -->
<div></div>
<div></div>
<div></div> <!-- SPECIFIC STYLE -->

I don't know how to target these divs in CSS, using nth-child ?

1
  • div:nth-child(3n); Commented Mar 14, 2017 at 10:29

3 Answers 3

1

Since you want to change style of every third element.You can do:

div:nth-child(3n+0) {
    color: red;
}
<div>1</div>
<div>2</div>
<div>3</div> <!-- SPECIFIC STYLE -->
<div>4</div>
<div>5</div>
<div>6</div> <!-- SPECIFIC STYLE -->
<div>7</div>
<div>8</div>
<div>9</div> <!-- SPECIFIC STYLE -->

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the css, as your div sibling styles is applied to multiple of 3.. ie third, sixth, nineth....

div:nth-child(3n) {
    background: red;
}

Comments

0

<!DOCTYPE html>
<html>
<head>
<style>
div{background-color:red;}
div:nth-child(3n){background-color:black;}
</style>
</head>
<body>
<div>A</div>
<div>B</div>
<div>C</div> <!-- SPECIFIC STYLE -->
<div>D</div>
<div>E</div>
<div>F</div> <!-- SPECIFIC STYLE -->
<div>G</div>
<div>H</div>
<div>I</div> <!-- SPECIFIC STYLE -->

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.