Pavle Vukajlovic
Pavle Vukajlovic

Reputation: 45

Responsive navbar with dropdown menu that slides below dropdown

Im trying to make responsive navbar with dropdown menu. I want dropdown menu to open verticaly (slide) below dropdown menu container. I tried some things but wasnt able to achieve what I wanted. I would love it to be like on the picture.I would like to "Google Pixel 2" and "LG V30" slide down same as "Who We are" and "What We Do".

enter image description here

.navbar {
  background-color: #31577C;
  overflow: hidden;
  width: 100%;
}

ul {
  font-family: Arial;
  text-shadow: 0px 0px 20px black;
  list-style-type: none;
  margin: 0 auto;
  padding: 0;
  text-align: center;
}

li {
  float: left;
}

li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover,
.dropdown:hover {
  background-color: #97C026;
  transition: background-color 0.5s ease;
}

.dropbtn {
  background-color: #31577C;
}

li.dropdown {
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #31577C;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  color: white;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #97C026;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.active {
  background-color: #97C026;
}

@media only screen and (max-width: 768px) {
  ul {
    position: static;
    text-align: center;
  }
  li {
    margin-bottom: 1px;
  }
  ul li,
  li a {
    width: 100%;
  }
  .dropdown-content a {
    text-align: center;
  }
}
<div class="navbar">
  <ul align="center">
    <li><a href="Naslovna.html">Naslovna</a></li>
    <li class="dropdown"><a href="Uređaji.html" class="dropbtn">Uređaji</a>
      <div class="dropdown-content">
        <a href="Pixel.html">Google Pixel 2 </a>
        <a href="V30.html">LG V30</a>
      </div>
    </li>
    <li><a href="Softver.html">Softver</a></li>
    <li class="active"><a href="Registracija.html">Registracija </a> </li>
  </ul>
</div>

Upvotes: 0

Views: 173

Answers (1)

Lakindu Gunasekara
Lakindu Gunasekara

Reputation: 4271

Try this snippet. I have removed the hyperlink on Uređaji.html, otherwise you cannot work with the dropdown. Hope that is not an issue and had to change several css parts.

(function($) { // Begin jQuery
  $(function() { // DOM ready
    // If a link has a dropdown, add sub menu toggle.
    $('nav ul li a:not(:only-child)').click(function(e) {
      $(this).siblings('.nav-dropdown').toggle();
      // Close one dropdown when selecting another
      $('.nav-dropdown').not($(this).siblings()).hide();
      e.stopPropagation();
    });
    // Clicking away from dropdown will remove the dropdown class
    $('html').click(function() {
      $('.nav-dropdown').hide();
    });
    // Toggle open and close nav styles on click
    $('#nav-toggle').click(function() {
      $('nav ul').slideToggle();
    });
    // Hamburger to X toggle
    $('#nav-toggle').on('click', function() {
      this.classList.toggle('active');
    });
  }); // end DOM ready
})(jQuery); // end jQuery
@charset "UTF-8";
.navigation {
  height: 4rem;
  background: #31577C;
  margin-top: -0.5rem;
}

.nav-container {
  max-width: 1000px;
  margin: 0;
}

nav {
  float: left;
}

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

nav ul li {
  float: left;
  position: relative;
}

nav ul li a,
nav ul li a:visited {
  display: block;
  padding: 0 20px;
  line-height: 4rem;
  background: #31577C;
  color: #ffffff;
  text-decoration: none;
}

nav ul li a:hover,
nav ul li a:visited:hover {
  background: #97C026;
  color: #ffffff;
}

nav ul li a:not(:only-child):after,
nav ul li a:visited:not(:only-child):after {
  padding-left: 4px;
  content: ' ▾';
}

nav ul li ul li {
  min-width: 190px;
}

nav ul li ul li a {
  padding: 15px;
  line-height: 20px;
}

.nav-dropdown {
  position: absolute;
  display: none;
  z-index: 1;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
}


/* Mobile navigation */

.nav-mobile {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  background: #31577C;
  height: 4rem;
  width: 4rem;
}

@media only screen and (max-width: 798px) {
  .nav-mobile {
    display: block;
  }
  nav {
    width: 100%;
    padding: 70px 0 15px;
  }
  nav ul {
    display: none;
  }
  nav ul li {
    float: none;
  }
  nav ul li a {
    padding: 15px;
    line-height: 20px;
  }
  nav ul li ul li a {
    padding-left: 30px;
  }
  .nav-dropdown {
    position: static;
  }
}

@media screen and (min-width: 799px) {
  .nav-list {
    display: block !important;
  }
}

#nav-toggle {
  position: absolute;
  left: 18px;
  top: 22px;
  cursor: pointer;
  padding: 10px 35px 16px 0px;
}

#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
  cursor: pointer;
  border-radius: 1px;
  height: 5px;
  width: 35px;
  background: #ffffff;
  position: absolute;
  display: block;
  content: '';
  transition: all 300ms ease-in-out;
}

#nav-toggle span:before {
  top: -10px;
}

#nav-toggle span:after {
  bottom: -10px;
}

#nav-toggle.active span {
  background-color: transparent;
}

#nav-toggle.active span:before,
#nav-toggle.active span:after {
  top: 0;
}

#nav-toggle.active span:before {
  transform: rotate(45deg);
}

#nav-toggle.active span:after {
  transform: rotate(-45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section class="navigation">
  <div class="nav-container">
    <nav>
      <div class="nav-mobile"><a id="nav-toggle" href="#!"><span></span></a></div>
      <ul class="nav-list">

        <li><a href="Naslovna.html">Naslovna</a></li>
        <li><a href="#">Uređaji</a>

          <ul class="nav-dropdown">
            <li><a href="Pixel.html">Google Pixel 2 </a></li>
            <li><a href="V30.html">LG V30</a></li>
          </ul>

        </li>
        <li><a href="Softver.html">Softver</a></li>
        <li class="active"><a href="Registracija.html">Registracija </a> </li>
      </ul>
    </nav>
  </div>
</section>

Upvotes: 1

Related Questions