HTML Element Style How to - Style the first item in UL li list








Question

We would like to know how to style the first item in UL li list.

Answer


<!--from w w w.  j  a  va2 s . com-->
<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
li:first-child {
  color: blue;
}

li:hover {
  color: red;
}

ul:hover li:first-child {
  color: black;
}

ul:hover li:first-child:hover {
  color: red;
}
</style>
</head>
<body>
  <ul>
    <li>1111</li>
    <li>2222</li>
    <li>3333</li>
  </ul>
</body>
</html>

The code above is rendered as follows: