<div class="question_container">
<div class="views">
<div>10</div>
</div>
<div>Something else</div>
</div>
<div class="question_container">
<div class="views">
<div>10</div>
</div>
<div>Something else</div>
</div>
<div class="question_container">
<div class="views">
<div>10</div>
</div>
<div>Something else</div>
</div>
How can I style every second class views in pure css.
In jquery I would do
$('*[class=views]:even').addClass('views');
But how can I do this CSS?
You use the general sibling selector (~) in combination with :hover . The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.
The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.
The :nth-of-type selector allows you select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.
You can use the :nth-child
property for this:
Example:
.question_container:nth-child(2n) .views{
color: red;
}
:nth-child(2)
would select only the second item, while :nth-child(2n)
will select every second item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With