CSS: select every odd DIV-only

1.3k views Asked by At

I have a HTML structure like this:

<h3>h3</h3>
<div>Content</div>
<h3>h3</h3>
<div>Content</div>
<div>Content</div>
<div>Content</div>
<h3>h3</h3>
<div>Content</div>
<div>Content</div>
<h3>h3</h3>
<div>Content</div>
...

I would like to select every second div (ignoring the h3s). Is that possible with CSS?

Thanks!

2

There are 2 answers

1
m4n0 On BEST ANSWER

Use nth-of-type(even)

div:nth-of-type(even) {
    color: tomato;
}
<h3>h3</h3>
<div>Content</div>
<h3>h3</h3>
<div>Content</div>
<div>Content</div>
<div>Content</div>
<h3>h3</h3>
<div>Content</div>
<div>Content</div>
<h3>h3</h3>
<div>Content</div>

1
technoY2K On

You can use this on the parent wrapper.

div:nth-child(even) {

    //code here
}