Page Widget How to - Create Pure CSS Hover Menu








Question

We would like to know how to create Pure CSS Hover Menu.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
ul {<!--from   w  ww  .  j a v  a2  s  .c o m-->
  list-style-type: none;
}

ul li {
  padding: 5px;
  margin: 3px;
  border: 1px solid black;
}

ul li:hover {
  background-color: lime;
}

li ul {
  display: none;
}

li:hover ul {
  display: block;
}

li ul li:hover {
  background-color: red;
}
</style>
</head>
<body>
  <ul>
    <li>Item 1
      <ul>
        <li>Subitem 1</li>
        <li>Subitem 2</li>
      </ul>
    </li>
    <li>Item2</li>
    <li>Item 3
      <ul>
        <li>Subitem 1</li>
      </ul>
    </li>
  </ul>
</body>
</html>

The code above is rendered as follows: