<!--
  Drop down menu example

  Note: this demo is not 
  currently setup for any
  specific responsive behaviors.
-->

<div class="page">

  <header role="banner" aria-label="Primary">

    <nav role="navigation" aria-label="Primary" id="nav" class="nav">

      <ul id="nav_inner" class="nav__list">
        <li class="has-drop">
          <a href="#!">
            Drop Down
          </a>

          <ul class="nav__list__drop">
            <li>
              <a href="#!">
                Item 1
              </a>
            </li>
            <li>
              <a href="#!">
                Item 2
              </a>
            </li>
          </ul>
        </li>

        <li>
          <a href="#!">
            Normal
          </a>
        </li>

        <li class="has-drop">
          <a href="#!">
            Drop Down 2
          </a>

          <ul class="nav__list__drop">
            <li>
              <a href="#!">
                Item 1 has a long name
              </a>
            </li>
            <li>
              <a href="#!">
                Item 2
              </a>
            </li>
          </ul>
        </li>
      </ul>

    </nav>
  </header>

</div>
/*
  General cleanup
*/

*,
*:before,
*:after {
  box-sizing: border-box;
}

html,
body {
  font-family: helvetica, arial, sans-serif;
  font-size: 18px;
  margin: 0;
  padding: 0;
}

nav ul,
nav ol {
  list-style: none;
  padding: 0;
  margin: 0;
}


.sr-only {
  position: absolute !important;
  clip: rect(1px, 1px, 1px, 1px);
  font-size: 1px;
}


/*
  Demo
*/
.show-nav {
  position: fixed;
  top: 0;
  right: 0;
}

.nav__list {
  background: #fafafa;
  border-bottom: 1px solid #444;
  text-align: center;
  position: relative;
  z-index: 2;
}

.nav__list > li {
  display: inline-block;
}

.nav__list a {
  display: block;
  padding: 20px;
  position: relative;
  z-index: 2;
}

.nav__list a:hover,
.nav__list a:focus {
  background: #222;
  color: #fff;
}

.has-drop {
  position: relative;
}


.nav__list__drop {
  left: 0;
  margin: 0;
  position: absolute;
  text-align: left;
  top: 100%;
  opacity: 0;
  transform: translateY(-20px);
  height: 1px;
  transition: transform .2s ease-in-out,
              opacity .1s ease-out;
  overflow: hidden;
  z-index: 1;
}

.nav__list__drop {
  background: #fafafa;
  border: 1px solid #444;
  border-top: 0;
  min-width: 100%;
}

.nav__list__drop a {
  padding: 12px 20px;
  white-space: nowrap;
}

.nav__list a:focus + .nav__list__drop,
.has-drop:hover .nav__list__drop,
.nav__list__drop.has-focus {
  opacity: 1;
  transform: translateY(0px);
  height: auto;
  z-index: 1;
}

.no-js .nav__list__drop {
  display: none;
}

.no-js .has-drop:hover .nav__list__drop {
  display: block;
}

(function ( w, doc ) {

  // Enable strict mode
  "use strict";

  // Local object for method references
  var DropNav = {};

  // Namespace it up yo
  DropNav.ns = "Drop Navigation";

  // the main event...err..function
  DropNav.init = function () {

    var hasDrop = doc.querySelectorAll('.has-drop'),
      hasDropLinks = doc.querySelectorAll('.nav__list__drop a'),
      hasDropCount = hasDrop.length,
      hasDropLinksCount = hasDropLinks.length,
      i;

    if ( hasDropCount > 0 ) {

      for ( i = 0; i < hasDropCount; i++ ) { // i++  =  i = i + 1 

        var drop = hasDrop[i],
          firstDropLink = drop.querySelectorAll('.nav__list__drop a')[0];

        firstDropLink.innerHTML = ' <span class="sr-only">Sub menu, </span>' + firstDropLink.innerHTML; //*

      } //for

      for ( i = 0; i < hasDropLinksCount; i++ ) {

        var dropLinks = hasDropLinks[i];

        dropLinks.addEventListener('focus', function ( e ) {
          this.parentNode.parentNode.classList.add('has-focus');
        });

        dropLinks.addEventListener('blur', function ( e ) {
          this.parentNode.parentNode.classList.remove('has-focus');
        });

      } //for

    } //if

  }; //init

  DropNav.init();

})( this, this.document );

/* 
  Created Aug 22, 2016
  
  Revised Aug 23, 2016
  * https://codepen.io/svinkle
  Switched from aria label to adding visually hidden "sub menu" text to the first item in the drop down, to ensure that all screen readers would accurately announce "sub menu"
  
  Fixed z-index of primary link & drop menu to ensure that the primary link always appeared on top of the drop menu
  
  Reveal drop menu on focus of primary link, to make it more apparent that a sub menu exists. suppose this could have been done with a down arrow on the primary link. but nope :) 
*/

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.