Page Widget How to - Create Pure CSS dropdown menus








Question

We would like to know how to create Pure CSS dropdown menus.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
ul>li {<!--   ww  w .  j av  a 2  s  . co  m-->
  display: block;
  float: left;
  margin-right: 10px;
  position: relative;
  background: Red;
  padding: 0.5em;
  line-height: 1em
}

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

ul ul>li {
  float: none;
}

ul>li:hover>ul, ul>a:hover+ul {
  display: block
}
</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: