Erick Arbe 1 minute read
January 9, 2014

CSS: Select the Last Odd Child

How did I never see this before? In my need for random pieces of code throughout the day, I had this problem arise. I needed a responsive navigation pattern to format correctly when I had an odd number of links. I was using this "clean grid" pattern and I originally wrote that you need an even amount of links in order for this pattern to look good. Not anymore!

Thanks to the magic of CSS3, we can target two things at once: :nth-last-child(1) - then we combine it with :nth-child(odd) to target both a last child and an odd selector. Pretty slick, huh? So put it together and we get this: ul.nav li:nth-last-child(1):nth-child(odd) a

This let me target the last element ONLY if it were an odd number. If it wasn't an odd number, then no harm no foul.

Here's the reference I found with a bit more info:

Whoops, looks like something went wrong.

1/1 ErrorException in Filesystem.php line 81: file_put_contents(): Only 0 of 278 bytes written, possibly out of free disk space

  1. in Filesystem.php line 81
  2. at HandleExceptions->handleError('2', 'file_put_contents(): Only 0 of 278 bytes written, possibly out of free disk space', '/home/devbeta9/public_html/', '81', array('path' => '/home/devbeta9/public_html/', 'contents' => 'a:4:{s:6:"_token";s:40:"HZqpKsmMOiTIbMqdQlCxNkiRYzlPp6PGT0taPqpN";s:9:"_previous";a:1:{s:3:"url";s:41:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1516107721;s:1:"c";i:1516107721;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', 'lock' => true))
  3. at file_put_contents('/home/devbeta9/public_html/', 'a:4:{s:6:"_token";s:40:"HZqpKsmMOiTIbMqdQlCxNkiRYzlPp6PGT0taPqpN";s:9:"_previous";a:1:{s:3:"url";s:41:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1516107721;s:1:"c";i:1516107721;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', '2') in Filesystem.php line 81
  4. at Filesystem->put('/home/devbeta9/public_html/', 'a:4:{s:6:"_token";s:40:"HZqpKsmMOiTIbMqdQlCxNkiRYzlPp6PGT0taPqpN";s:9:"_previous";a:1:{s:3:"url";s:41:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1516107721;s:1:"c";i:1516107721;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', true) in FileSessionHandler.php line 83
  5. at FileSessionHandler->write('978e0bfe7443847058afde8ac4198aea2170af83', 'a:4:{s:6:"_token";s:40:"HZqpKsmMOiTIbMqdQlCxNkiRYzlPp6PGT0taPqpN";s:9:"_previous";a:1:{s:3:"url";s:41:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1516107721;s:1:"c";i:1516107721;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}') in Store.php line 262
  6. at Store->save() in StartSession.php line 88
  7. at StartSession->terminate(object(Request), object(Response)) in Kernel.php line 145
  8. at Kernel->terminate(object(Request), object(Response)) in index.php line 160