HTML-CSS Menu

A multi level menu with HTML and CSS

by Ashiqur Rahman

HTML

<h2>
Multi level menu with HTML-CSS
</h2>
<nav>
  <ul class="nav">
    <li>First menu item</li>
    <li class="children">Parent item
      <ul class="nav">
        <li>First child</li>
        <li>Another child item</li>
      </ul>
    </li>
    <li>Another top level menu</li>
    <li>Last menu item</li>
  </ul>
</nav>

SCSS

/** SCSS code **/
nav {
  width: 300px;
  ul.nav {
    /** Style for main nav ul **/
    margin: 0;
    padding: 5px;
    display: inline-block;
    color: #fafafa;
    background: #000000;
    border-radius: 3px;
    li {
      /** Individual menu items **/
      margin: 0;
      padding: 5px 10px;
      cursor: pointer;
      white-space: nowrap;
      list-style: none;
      &.children {
        /** Menu item with children **/
        position: relative;
        ul.nav {
          /** Child menu segment hidden by default **/
          display: none;
          position: absolute;
          top: 0;
          left: calc(100% + 10px);
          &:before {
            /** Small arrow before the child segment **/
            position: absolute;
            border-top: 10px solid transparent;
            border-bottom: 10px solid transparent;
            border-right: 10px solid #000000;
            content: "";
            margin-top: 10px;
            margin-bottom: 10px;
            display: block;
            top: 0;
            left: -10px;
            padding-left: 5px;
          }
        }
        &:hover,
        &:active {
          ul.nav {
            /** Show the child segment on mouse over **/
            display: inline-block;
          }
        }
      }
    }
  }
}