Page Widget How to - Create dropdown menus when hover








Question

We would like to know how to create dropdown menus when hover.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
body {<!--  w  w  w.  ja va2s. c om-->
  margin-left: 100px;
  margin-top: 100px;
}

ul>li {
  display: block;
  float: left;
  margin-right: 10px;
  position: relative;
  padding: 0.5em;
  line-height: 1em;
  background: #999;
  color: #FFF;
  width: 150px;
}

ul ul {
  display: none;
  width: 150px;
  position: absolute;
  top: 2em;
  left: 0;
  padding: 0;
}

ul ul>li {
  float: none;
}

ul>li:hover>ul, ul>a:hover+ul {
  display: block;
  margin-left: 0px;
}
</style>
</head>
<body>
  <ul>
    <li><a href="#">Item #1</a>
      <ul>
        <li><a href="">Sub-Item #1</a></li>
        <li><a href="">Sub-Item #2</a></li>
        <li><a href="">Sub-Item #3</a></li>
      </ul></li>
  </ul>
</body>
</html>

The code above is rendered as follows: