Reputation: 1670
I want to use the pseudo selectors odd end even. But if sequence of element is divided by another element, e.g. a heading the order of odd and even is broken. How can I continue the initial order?
HTML
<div class="fig">
<figcaption>t</figcaption>
<figcaption></figcaption>
<div>headline</div>
<figcaption></figcaption>
<figcaption></figcaption>
<figcaption></figcaption>
</div>
CSS
.fig figcaption:nth-child(even) {
background:green;
}
.fig figcaption:nth-child(odd) {
background:blue;
}
Upvotes: 1
Views: 171
Reputation: 206121
simply use
figcaption:nth-of-type(odd)
figcaption:nth-of-type(even)
https://developer.mozilla.org/en/docs/Web/CSS/:nth-of-type
Upvotes: 7
Reputation: 3220
:nth-child (odd)
doesn't check no of figcaption
and calculate odd and even based on it. It checks based on your Parent element div
:nth-of-type(odd)
it check the no of figcaption
and calculate odd and even based on them.
Upvotes: 1
Reputation: 807
Wrapping each figure sets in another div will work as you expect
<div class="fig">
<div>
<figcaption>t</figcaption>
<figcaption></figcaption>
</div>
<div>headline</div>
<div>
<figcaption></figcaption>
<figcaption></figcaption>
<figcaption></figcaption>
</div>
</div>
Upvotes: -3