// jade: compressed
mixin example(code)
figcaption
code= code
figure&attributes(attributes)
- for (var x = 0; x < 25; x++)
i.fa.fa-circle
br
br
// Output HTML
h1.title :nth-child
main
code :nth-child
| is a CSS3 selector, geared towards selecting children of an element depending on criteria.
br
br
| What does that mean? In a nutshell, it means we can style elements depending on where they are in the DOM. This is great for selecting ranges of elements, or every "x" amount of elements!
br
br
| In CSS, this looks like:
pre
| parent :nth-child( expression ) {
| *CSS*
| }
br
br
| Let's get down to some practical applications.
br
br
hr
| Styling a single element:
+example( ":nth-child(9)" ).ex1
| Styling above a certain element:
+example( ":nth-child(n + 5)" ).ex2
| Styling below a certain element:
+example( ":nth-child(-n + 5)" ).ex3
| Styling a range of elements:
+example( ":nth-child(n + 10):nth-child(-n + 20)").ex4
| Styling odd elements:
+example( ":nth-child(odd)" ).ex5
| Styling every x elements:
+example( ":nth-child(5n + 1)").ex6
| Style every odd element in a range:
+example( ":nth-child(odd):nth-child(n + 10):nth-child(-n + 20)").ex7
hr
| As you can see, nth-child has some powerful applications. But remember, as always, with great power comes great responsibility; these selectors can be slow if applied too liberally!
br
br
| Browser Campatability is pretty excellent; everything except IE8 has full support for the selector.
a(href="http://caniuse.com/#search=nth-child") Can-I-Use
xxxxxxxxxx
// Imports //
@import url("//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css");
// Block Elements //
body
background #222
color #ccc
font-family "Open Sans", sans-serif
line-height 1.3em
font-size 1em
a
color #09c
text-decoration none
transition text-shadow .2s, color .2s
&:hover
color #2be
text-shadow 0 0 1px @color
code
padding .1em .3em
background #111
font-family monospace
border-radius .3em
pre
@extends code
line-height 1em
hr
margin 2em 0
border none
border-top 1px solid #333
h1
margin .5em 0
font-size 2.5em
font-weight 300
line-height 1em
text-align center
text-shadow 0 0 2px white
&.title
margin .5em .5em .75em .5em
font-size 4em
h2
padding-left 1em
margin .5em 0
font-size 2em
font-weight 300
text-shadow 0 0 1px white
main
max-width 800px
padding-top 2em
margin 0 auto 2em auto
border-top 1px solid #333
figure
box-sizing border-box
width 100%
padding 1em
background #111
text-align center
border-radius .3em
figcaption
line-height 1em
i.fa.fa-circle
margin .5em
color #444
figure.ex1 :nth-child( 9 ),
figure.ex2 :nth-child( n + 5 ),
figure.ex3 :nth-child( -n + 5 ),
figure.ex4 :nth-child(n + 10):nth-child(-n + 20),
figure.ex5 :nth-child(odd),
figure.ex6 :nth-child( 5n + 1 ),
figure.ex7 :nth-child(odd):nth-child(n + 10):nth-child(-n + 20)
color #999
text-shadow 0 0 1em white