HTML Element Style How to - Style the first and last li with CSS








Question

We would like to know how to style the first and last li with CSS.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
li:first-child {<!--from   w w  w.  ja  v a2 s  .c  o m-->
  color: lime;
}

li+li+li+li+li+li {
  color: pink;
}
</style>
</head>
<body>
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
    <li>Item 6</li>
  </ul>
</body>
</html>

The code above is rendered as follows: