/ Dropdown Menu

Dropdown Menu with pure CSS

Dropdown Menu with pure CSS

The hover menu is pure CSS. The toggle menu utilizes a minimal amount of JavaScript, but will degrade gracefully because it only uses JavaScript to close the menu when the user clicks outside of the menu.

Made with

Html
Css
JavaScript

html

<main>
  <h2>CSS Dropdown Menu</h2>
  <nav>
    <div class="dropdown hover">
      <a href="#">Hover Menu</a>
      <ul>
        <li><a href="#">Item</a></li>
        <li><a href="#">Product</a></li>
        <li><a href="#">Text</a></li>
        <li><a href="#">Page</a></li>
        <li><a href="#">Thing</a></li>
        <li><a href="#">Product</a></li>
        <li><a href="#">Text</a></li>
      </ul>
    </div>
    <div class="dropdown toggle">
      <input id="t1" type="checkbox" checked>
      <label for="t1">Toggle Menu</label>
      <ul>
        <li><a href="#">Item</a></li>
        <li><a href="#">Product</a></li>
        <li><a href="#">Text</a></li>
        <li><a href="#">Page</a></li>
        <li><a href="#">Thing</a></li>
      </ul>
    </div>
  </nav>
  <section>
    <h4>The hover menu is pure CSS.</h4>
    <h4>The toggle menu utilizes a minimal amount of javascript, but will degrade gracefully because it only uses javascript to close the menu when the user clicks outside of the menu.</h4>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam urna eros, aliquet id dui at, mollis molestie lorem. Donec consectetur lorem elit, nec cursus neque interdum a. Mauris molestie ut dui eget suscipit. Ut ante risus, rhoncus eget metus sit amet, euismod sodales dui. Suspendisse potenti. Aliquam risus magna, euismod quis sagittis quis, convallis id metus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </p>
    <p>
      Integer et lacinia erat, vel aliquam magna. Pellentesque sagittis commodo rutrum. Nam dignissim, est fermentum rutrum molestie, arcu odio tempus tortor, a rutrum mauris diam at sapien. Aliquam in enim convallis, feugiat lectus in, rutrum nibh. Vivamus sollicitudin odio vel velit pulvinar tincidunt. Aenean nulla velit, consectetur a risus eu, consequat dictum dui. In suscipit dolor magna, id mollis arcu interdum et. Nunc vel nunc ut velit vulputate imperdiet. Aenean nisl dui, pharetra et blandit quis, condimentum id est.
    </p>
		<p>
			An original pen by <a href="https://codepen.io/ejsado/">Eric</a>.
		</p>
  </section>
</main>

Css

@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700);

body {
  font-family: "Open Sans", "Arial";
  background: #0097B2;
}
h2 {
  color: #FFF;
}
h4 {
  color: #666;
}
p {
  color: #333;
  font-size: 13px;
}
main {
  margin: 50px auto;
  width: 500px;
}
section {
  margin-top: 30px;
  background: #E3F9F9;
  padding: 20px;
  box-shadow: 0 3px 5px rgba(0,0,0,0.15);
}
.dropdown {
  width: 230px;
  display: inline-block;
  margin-right: 10px;
  position: relative;
}
.dropdown.toggle > input {
  display: none;
}
.dropdown > a, .dropdown.toggle > label {
  border-radius: 2px;
  box-shadow: 0 6px 5px -5px rgba(0,0,0,0.3);
}
.dropdown > a::after, .dropdown.toggle > label::after {
  content: "";
  float: right;
  margin: 15px 15px 0 0;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 10px solid #CCC;
}
.dropdown ul {
  list-style-type: none;
  display: block;
  margin: 0;
  padding: 0;
  position: absolute;
  width: 100%;
  box-shadow: 0 6px 5px -5px rgba(0,0,0,0.3);
  overflow: hidden;
}
.dropdown a, .dropdown.toggle > label {
  display: block;
  padding: 0 0 0 10px;
  text-decoration: none;
  line-height: 40px;
  font-size: 13px;
  text-transform: uppercase;
  font-weight: bold;
  color: #999;
  background-color: #FFF;
}
.dropdown li {
  height: 0;
  overflow: hidden;
  transition: all 500ms;
}
.dropdown.hover li {
  transition-delay: 300ms;
}
.dropdown li:first-child a {
  border-radius: 2px 2px 0 0;
}
.dropdown li:last-child a {
  border-radius: 0 0 2px 2px;
}
.dropdown li:first-child a::before {
  content: "";
  display: block;
  position: absolute;
  width: 0;
  height: 0;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-bottom: 10px solid #FFF;
  margin: -10px 0 0 30px;
}
.dropdown a:hover, .dropdown.toggle > label:hover, .dropdown.toggle > input:checked ~ label {
  background-color: #EEE;
  color: #666;
}
.dropdown > a:hover::after, .dropdown.toggle > label:hover::after, .dropdown.toggle > input:checked ~ label::after {
  border-top-color: #AAA;
}
.dropdown li:first-child a:hover::before {
  border-bottom-color: #EEE;
}
.dropdown.hover:hover li, .dropdown.toggle > input:checked ~ ul li {          
  height: 40px;
}
.dropdown.hover:hover li:first-child, .dropdown.toggle > input:checked ~ ul li:first-child {
  padding-top: 15px;
}

JavaScript

// close the toggle menu if user clicks outside of the menu

$(document).click(function(event) {
  if(
    $('.toggle > input').is(':checked') &&
    !$(event.target).parents('.toggle').is('.toggle')
  ) {
    $('.toggle > input').prop('checked', false);
  }
})

Demo

See the Pen Dropdown Menu by Eric Sadowski (@ejsado) on CodePen.

CodeTea

A Nice collection of often useful examples done in HTML JavaScript CSS.

Read More