Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Nth-Child Checkered [closed]

I'm trying to use nth-child to change background colors of divs. Below is an image of how I want to change the background colors. How would I do this with nth-child?

enter image description here

Each box should be 25% width of it's parent container.

like image 504
cusejuice Avatar asked Apr 30 '14 17:04

cusejuice


People also ask

How do you get the nth child selector to skip hidden divs?

In order for the nth-child rules you've declared to work after a user clicks to hide divs, you need to remove the hidden divs from the DOM, so they no longer exist as siblings.

How do I change the Nth child in CSS?

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.

What's the difference between the nth-of-type () and Nth child () selector?

As a general rule, if you want to select an interval of a selector regardless of the type of element it is, use nth-child . However, if you want to select a specific type only and apply an interval selection from there, use nth-of-type .


1 Answers

Since it's a repeating pattern over 8 elements, you can use that to create the CSS:

div:nth-child(8n+1),
div:nth-child(8n+3),
div:nth-child(8n+6),
div:nth-child(8n) {
    background-color:black
}
div:nth-child(8n+2),
div:nth-child(8n+4),
div:nth-child(8n+5),
div:nth-child(8n+7) {
    background-color:gray
}

Fiddle: http://jsfiddle.net/RvbsL/2/

like image 54
andi Avatar answered Oct 30 '22 12:10

andi