I have a number of divs of a particular class .box for which I want to set an alternating background color. However, some of the divs are placed inside another div .inner-co
You can still apply an alternating background with CSS only here. Here's a Fiddle of it in action.
There's 2 ways you can do it:
Method 1:
Target the each level of The method above targets only the first level children of the parent To target the Method 2: Add a second class for the nested .container > .box:nth-child(even) {
/* stuff */
}
.container. This won't effect the .inner-container.
.inner-container separately, we use the same trick again, but start with a different parent container:
.inner-container > .box:nth-child(even) {
/* stuff */
}