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 best solutions below

1
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
On

You can use this on the parent wrapper.

div:nth-child(even) {

    //code here
}