How would I target this div using CSS?
In the code below, how would I target the second
div.b using CSS?
In this particular instance, you could use the adjacent sibling combinator,
The above assumes there are no more than two, adjacent
.b elements. If this wasn’t the case, the general sibling combinator,
~, would be more useful assuming there are still only two
As an alternative, you could also use the following, which will work with multiple
.b elements, regardless of position. Initially, use
.b ~ .b to set the styling of the second, targeted element. You would then need to use
.b ~ .b ~ .b to reset the styling of the
.b elements following the second
This should theoretically work in all instances, for example:
As a more practical example, you could use something like this: