Page Widget How to - Create sub menu in CSS








Question

We would like to know how to create sub menu in CSS.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
ul.menu {<!--from  w w w. j  a v  a2 s. c om-->
  width: 200px
}

ul.menu li {
  position: relative;
}

ul.menu a {
  display: block;
  background: #CCC;
  padding: 8px 14px;
}

ul.menu li .sub {
  background: #777;
  position: absolute;
  top: 0;
  left: 200px;
  display: none;
  padding: 10px;
  width: 400px;
}

ul.menu li:hover .sub {
  display: block;
}
</style>
</head>
<body>
  <ul class="menu">
    <li><a href="">A</a>
      <div class="sub">Sub a</div></li>
    <li><a href="">B</a>
      <div class="sub">Sub B</div></li>
  </ul>
</body>
</html>

The code above is rendered as follows: