CSS Trick: How to target all elements besides the first

By gdEg6gO1wybjfkUX5h9q, 9 January, 2019

This CSS trick is quite simple. Let's say you have:

  <li> list item </li>
  <li> list item </li>
  <li> list item </li>

If you want to target the first <li>, you can do it as follows:

ul li { background-color: #FFFFFF; /* applies to every li */ }

ul li:first-child { background-color: blue; /* targets only the first li */ }

This works in all major browsers.

For :first-child to work in IE8 and earlier, a <!DOCTYPE> must be declared.


Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.