Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Created March 3, 2015 16:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecilemuller/82357e04dfbe5f99c100 to your computer and use it in GitHub Desktop.
Save cecilemuller/82357e04dfbe5f99c100 to your computer and use it in GitHub Desktop.
CSS styles based on number of children
<div class="list">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
</div>
<div class="list">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
<div>Item 6</div>
</div>
/* less than 6 children = green */
.list > * {
background: green;
}
/* 6+ children = orange */
.list > *:nth-last-child(n+6),
.list > *:nth-last-child(n+6) ~ * {
background: orange;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment