Skip to main content
emmad23117089
Known Participant
November 16, 2018
Answered

Dropdown menu only works on mobile (from hamburger) but not on desktop

  • November 16, 2018
  • 3 replies
  • 4581 views

Hi all, a little help if I may before I lose all of my hair!

I am a newbie to web design since this year.

I have a responsive dropdown menu that I got from W3 schools which has worked fine until now.

I originally designed the website poorly by altering the bootstrap css and now I know this was wrong as the page behaved badly when it must have updated. I have since copied and pasted all of my html code over onto new bootstrap templates on dreamweaver and not touched the CSS files, I have created a custom.css where I have added all the releavnt over-riding CSS and added the javascript from W3 schools.

I have effectively copied and pasted the code from W3 and just changed small details such as colour, nothing with media queries or anything else, but now the dropdown only works on mobile from hamburger but not on desktop.

Everything else is working fine just this one issue...

Any ideas?

Thanks Emma

This topic has been closed for replies.
Correct answer osgood_

Hi Preran,

Thanks for your message. No it's not resolved. I've heard nothing since I made the amendments to my website that Nancy kindly suggested.

Everything is now working beautifully other than the drop-down menu on larger screen sizes, still

It works fine from the hamburger though

Www.labyrinthdesigns.co.uk

Thanks

Emma


Are you able to copy and paste code?

If so why dont you just ditch the navigation you are currently using and deploy something that actually works.

I've included the complete navigation code way below (scroll down) but I'll go through what you need to do step by step;

Step 1:

Replace your current navigation code with:

<nav>

<div class="mobile-icon"><i class="fa fa-bars"></i></div>

<ul class="topnav">

<li><a href="index.html">Home</a></li>

<li><a href="index.html#About-Me">About Me</a></li>

<li><a href="Creative Services.html">Creative Services</a></li>

<li><a href="#">Portfolio <i class="fa fa-angle-down"></i>

</a>

<ul class="sub-menu">

<li><a href="#">Brand and Logo</a></li>

<li><a href="#">Artwork and Print</a></li>

</ul>

</li>

<li><a href="Creative Services.html#Pricing">Pricing</a></li>

<li><a href="index.html#Enquiry Form">Contact</a></li>

</ul>

<!-- end topnav -->

</nav>

<!-- end nav -->

Step 2:

Add the below css to your custom css stylesheet OR better still, as you are learning put the css in is own stylesheet called main-nav.css and link it to the page (you know how to do that right?):

Link:

<link rel="stylesheet" href="main-nav.css" type="text/css">

Actual css:

nav {

background-color: #7ab800;

border: 1px solid #772059;

}

/* TOP NAV */

.topnav {

display: flex;

justify-content: center;

flex-wrap: wrap;

list-style: none;

margin: 0;

padding: 0;

background-color: #7ab800;

}

.topnav li {

position: relative;

list-style: none;

margin: 0;

padding: 0;

}

@media screen and (max-width: 768px) {

.topnav li {

width: 100%;

}

}

.topnav a {

display: block;

text-decoration: none;

margin: 0;

padding: 20px;

color: #fff;

border-left: 1px solid #772059;

}

@media screen and (max-width: 768px) {

.topnav a {

border-left: none;

border-bottom: 1px solid #772059;

}

}

.topnav a:hover {

color: #772059;

}

.topnav li:last-child {

border-right: 1px solid #772059;

}

@media screen and (max-width: 768px) {

.topnav li:last-child {

border-right: none;

}

}

/* SUB MENU */

.sub-menu {

position: absolute;

border: 1px solid #772059;

top: 100%;

left: 0;

width: 250px;

background-color: #7ab800;

margin: 0;

padding: 0;

}

@media screen and (max-width: 768px) {

.sub-menu {

position: static;

width: 100%;

border: none;

}

}

.sub-menu a {

border-left: none;

}

.sub-menu li:last-child {

border-right: none;

border-top: 1px solid #772059;

}

@media screen and (max-width: 768px) {

.sub-menu li:last-child {

border-top: none;

}

}

/* MOBILE ICON */

.mobile-icon {

display: none;

text-align: right;

font-size: 25px;

padding: 10px 20px;

color: #fff;

}

@media screen and (max-width: 768px) {

.mobile-icon {

display: block;

}

}

.topnav li a i {

font-size: 18px;

}

Step 3:

Add the jQuery <script></script> below to your page directly before the closing </head> tag OR insert it in is own js file called main-nav.js (minus the <script></script> tags) and link it to the page (you know how to do that right?):

Link:

<script src="main-nav.js"></script>

Actual script:

<script>

$(document).ready(function(){

$('.sub-menu').hide();

// TOPNAV - SHOW SUB MENU

$('.topnav li a').css('cursor' , 'pointer').click(function(){

$(this).next('.sub-menu').slideToggle();

});

// MOBILE ICON - SHOW NAV

$('.mobile-icon').css('cursor' , 'pointer').click(function(){

$('.topnav').slideToggle();

});

// WINDOW RESIZE FUNCTION TO SET NAV DISPLAY OPTIONS

$(window).resize(function() {

//do something

var width = $(document).width();

if (width > 768) {

$('.topnav').css('display', 'flex')

}

else if (width < 768) {

$('.topnav, .sub-menu').css('display', 'none')

}

});

});

</script>

Full document:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

<title>Responsive Navigation</title>

<style>

body {

font-family: Consolas, 'Andale Mono', 'Lucida Console', 'Lucida Sans Typewriter', Monaco, 'Courier New', 'monospace';

margin: 0;

}

nav {

background-color: #7ab800;

border: 1px solid #772059;

}

/* TOP NAV */

.topnav {

display: flex;

justify-content: center;

flex-wrap: wrap;

list-style: none;

margin: 0;

padding: 0;

background-color: #7ab800;

}

.topnav li {

position: relative;

list-style: none;

margin: 0;

padding: 0;

}

@media screen and (max-width: 768px) {

.topnav li {

width: 100%;

}

}

.topnav a {

display: block;

text-decoration: none;

margin: 0;

padding: 20px;

color: #fff;

border-left: 1px solid #772059;

}

@media screen and (max-width: 768px) {

.topnav a {

border-left: none;

border-bottom: 1px solid #772059;

}

}

.topnav a:hover {

color: #772059;

}

.topnav li:last-child {

border-right: 1px solid #772059;

}

@media screen and (max-width: 768px) {

.topnav li:last-child {

border-right: none;

}

}

/* SUB MENU */

.sub-menu {

position: absolute;

border: 1px solid #772059;

top: 100%;

left: 0;

width: 250px;

background-color: #7ab800;

margin: 0;

padding: 0;

}

@media screen and (max-width: 768px) {

.sub-menu {

position: static;

width: 100%;

border: none;

}

}

.sub-menu a {

border-left: none;

}

.sub-menu li:last-child {

border-right: none;

border-top: 1px solid #772059;

}

@media screen and (max-width: 768px) {

.sub-menu li:last-child {

border-top: none;

}

}

/* MOBILE ICON */

.mobile-icon {

display: none;

text-align: right;

font-size: 25px;

padding: 10px 20px;

color: #fff;

}

@media screen and (max-width: 768px) {

.mobile-icon {

display: block;

}

}

.topnav li a i {

font-size: 18px;

}

</style>

<!-- jQuery library -->

<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<script>

$(document).ready(function(){

$('.sub-menu').hide();

// TOPNAV - SHOW SUB MENU

$('.topnav li a').css('cursor' , 'pointer').click(function(){

$(this).next('.sub-menu').slideToggle();

});

// MOBILE ICON - SHOW NAV

$('.mobile-icon').css('cursor' , 'pointer').click(function(){

$('.topnav').slideToggle();

});

// WINDOW RESIZE FUNCTION TO SET NAV DISPLAY OPTIONS

$(window).resize(function() {

//do something

var width = $(document).width();

if (width > 768) {

$('.topnav').css('display', 'flex')

}

else if (width < 768) {

$('.topnav, .sub-menu').css('display', 'none')

}

});

});

</script>

</head>

<body>

<nav>

<div class="mobile-icon"><i class="fa fa-bars"></i></div>

<ul class="topnav">

<li><a href="index.html">Home</a></li>

<li><a href="index.html#About-Me">About Me</a></li>

<li><a href="Creative Services.html">Creative Services</a></li>

<li><a href="#">Portfolio <i class="fa fa-angle-down"></i>

</a>

<ul class="sub-menu">

<li><a href="#">Brand and Logo</a></li>

<li><a href="#">Artwork and Print</a></li>

</ul>

</li>

<li><a href="Creative Services.html#Pricing">Pricing</a></li>

<li><a href="index.html#Enquiry Form">Contact</a></li>

</ul>

<!-- end topnav -->

</nav>

<!-- end nav -->

</body>

</html>

3 replies

emmad23117089
Known Participant
November 16, 2018

Even more strangely I am finding that everything works perfectly on the smaller screen, it is just the larger screens where the dropdown doesnt work and the styling for some of the website is not behaving correctly..

Nancy OShea
Community Expert
Community Expert
November 16, 2018

This is too complex to troubleshoot in a forum.  For quicker answers, you need to put your work online so we can see everything in context.   

Nancy O'Shea— Product User & Community Expert
emmad23117089
Known Participant
November 16, 2018

Ok here is my custom.css

body{

font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", "monospace";

}

.myForm {

font-family:

font-size: 0.8em;

width: 90em;

padding: 1em;

border: medium #772059;

}

.myForm * {

box-sizing: border-box;

}

.myForm fieldset {

border: none;

padding: 0;

}

.myForm legend,

.myForm label {

padding: 0;

font-weight: bold;

}

.myForm label.choice {

font-size: 0.9em;

font-weight: normal;

}

.myForm input[type="text"],

.myForm input[type="tel"],

.myForm input[type="email"],

.myForm input[type="datetime-local"],

.myForm select,

.myForm textarea {

display: block;

width: 100%;

border: 1px solid #ccc;

font-family:

font-size: 0.9em;

padding: 0.3em;

}

.myForm textarea {

height: 100px;

}

.myForm button {

padding: 1em;

border-radius: 0.5em;

background: #eee;

border: none;

font-weight: bold;

margin-top: 1em;

}

.myForm button:hover {

background: #ccc;

cursor: pointer;

}

.jumbotron {

  padding-top: 10px;

  padding-bottom: 10px;

margin-top: 10px;

  margin-bottom: 10px;

background-image: url("Woodland4.jpeg.jpg");

background-position: center;

background-size: cover;

height: 150px;

}

.container{

width: 100%;

    display: flex;

    justify-content: center;

    align-items: center;

}

div.gallery {

    margin: 5px;

    border: 1px solid #ccc;

    float: left;

    width: 180px;

}

div.gallery:hover {

    border: 1px solid #777;

}

div.gallery img {

    width: 100%;

    height: auto;

}

div.desc {

    padding: 15px;

    text-align: center;

}

.topnav {

  overflow: hidden;

  background-color: none;

}

.topnav a {

  float: left;

  display: block;

  color: white;

background-color: #7AB800;

  text-align: center;

  padding: 14px 16px;

  text-decoration: none;

  font-size: 17px;

border: 1px solid #772059;

}

.active {

  background-color:#772059;

  color: white;

border: 1px solid #772059;

}

.topnav .icon {

  display: none;

border: 1px solid #772059;

}

.dropdown {

    float: left;

    overflow: hidden;

}

.dropdown .dropbtn {

    font-size: 17px;  

color: white;

    outline: none;

    padding: 14px 16px;

    background-color:#7AB800;

    font-family: inherit;

    margin: 0;

border: 1px solid #772059;

}

.dropdown-content {

    display: none;

    position: absolute;

    background-color: rgba(122,184,0,1.00);

    min-width: 160px;

    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);

    z-index: 1;

}

.dropdown-content a {

    float: none;

    color: white;

    padding: 12px 16px;

    text-decoration: none;

font-size: 14px;

    display: block;

    text-align: left;

border: 1px solid #772059;

}

.topnav a:hover, .dropdown:hover .dropbtn {

  background-color: none;

  color: #772059;

}

.dropdown-content a:hover {

    background-color: none;

    color: #772059;

}

.dropdown:hover .dropdown-content {

    display: block;

}

@media screen and (max-width: 600px) {

  .topnav a:not(:first-child), .dropdown .dropbtn {

    display: none;

}

.topnav a.icon {

    float: right;

    display: block;

  background-color: #7AB800;

}

@media screen and (max-width: 600px) {

  .topnav.responsive {position: relative;}

  .topnav.responsive .icon {

    position: absolute;

    right: 0;

    top: 0;

  }

  .topnav.responsive a {

    float: none;

    display: block;

    text-align: left;

  }

  .topnav.responsive .dropdown {float: none;}

  .topnav.responsive .dropdown-content {position: relative;}

  .topnav.responsive .dropdown .dropbtn {

    display: block;

    width: 100%;

    text-align: left;

  }

}

.btn .btn-primary .btn-lg {

background-color: #772059;

color: #7AB800;

border: 1px solid #772059;

}

.btn .btn-primary .btn-lg a:hover {

background-color: #7AB800;

colour: #772059;

border: 1px solid #772059;

}

.btn {

  display: inline-block;

  font-weight: 400;

  text-align: center;

  white-space: nowrap;

  vertical-align: middle;

  -webkit-user-select: none;

  -moz-user-select: none;

  -ms-user-select: none;

  user-select: none;

border: 1px solid #772059;

background-color: #772059;

color: white;

  padding: 0.375rem 0.75rem;

  font-size: 1rem;

  line-height: 1.5;

  border-radius: 0.25rem;

  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;

}

.btn {

  display: inline-block;

  padding: 10px 10px;

  margin-bottom: 0;

margin-top: 8%;

  font-size: 14px;

  font-weight: normal;

border: 1px solid #772059;

color: white;

  line-height: 1.42857143;

  text-align: center;

  white-space: nowrap;

  vertical-align: middle;

  -ms-touch-action: manipulation;

      touch-action: manipulation;

  cursor: pointer;

  -webkit-user-select: none;

     -moz-user-select: none;

      -ms-user-select: none;

          user-select: none;

  background-image: none;

  border-radius: 4px;

}

.btn:hover, .btn:focus {

  text-decoration: none;

background-color: #7AB800;

colour: #772059;

border: 1px solid #772059;

}

.btn:focus, .btn.focus {

  outline: 0;

  box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);

}

}

/* Style the container with a rounded border, grey background and some padding and margin */

#container2 {

    border: 2px solid #772059;

    background-color:  #eee;

opacity: 0.8;

    border-radius: 5px;

    padding: 16px;

    margin: 16px 0;

}

/* Clear floats after containers */

#container2::after {

    content: "";

    clear: both;

    display: table;

}

/* Float images inside the container to the left. Add a right margin, and style the image as a circle */

#container2 img {

    float: left;

    margin-right: 20px;

    border-radius: 10%;

}

/* Increase the font-size of a span element */

#container2 span {

    font-size: 20px;

    margin-right: 10px;

text-align: center;

}

/* Add media queries for responsiveness. This will center both the text and the image inside the container */

@media (max-width: 500px) {

  #container2 {

    text-align: center;

  }

  #container2 img {

    margin: auto;

    float: none;

    display: block;

  }

}

.centered {

    position: absolute;

    top: 80%;

    left: 50%;

    transform: translate(-50%, -50%);

color:  #772059;

font-size: 30px;

My .btn styling has stopped working now too since I added the form styling, which leads me to believe it could the order I am putting it all in? I've just been moving things around to see if that helps and some styling has started working again.

Here is the html code for the menu and the java

<div class="topnav" id="myTopnav">

   <a href="index.html">Home</a>

  <a href="index.html#About-Me">About Me</a>

  <a href="Creative Services.html">Creative Services</a>

  <div class="dropdown">

  <button class="dropbtn">Portfolio

      <i class="fa fa-caret-down"></i>

    </button>

    <div class="dropdown-content">

      <a href="Brand and Logo.html">Brand and Logo</a>

  <a href="Artwork and Print.html">Artwork and Print</a>

    </div>

  </div>

<a href="Creative Services.html#Pricing">Pricing</a>

  <a href="index.html#Enquiry Form">Contact</a>

  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>

</div>

Obv at the bottom of the page...

<script>

function myFunction() {

    var x = document.getElementById("myTopnav");

    if (x.className === "topnav") {

        x.className += " responsive";

    } else {

        x.className = "topnav";

    }

}

</script>

  </body>

<link href="css/bootstrap-4.0.0.css" rel="stylesheet">

  <link rel="stylesheet" href="custom.css">

  <link rel="stylesheet" href="fontawesome-free-5.0.10/web-fonts-with-css/css/fontawesome-all.min.css">

Sorry I know theres a lot there

Thanks guys

Emma

Legend
November 16, 2018

Hi Emma,

Are you trying to achieve the hamburger menu on all screen sizes; mobiles, tablets, desktops etc or a standard navigation bar on larger screens and the hamburger on small screens?

Paul-M - Community Expert
Legend
November 16, 2018

Looking at your code, I'd say you need to change one of those media queries, the first line of this bit looks like it might be the culprit:

@media screen and (max-width: 600px) {

  .topnav.responsive {position: relative;}

  .topnav.responsive .icon {

    position: absolute;

    right: 0;

    top: 0;

  }

  .topnav.responsive a {

    float: none;

    display: block;

    text-align: left;

  }

  .topnav.responsive .dropdown {float: none;}

  .topnav.responsive .dropdown-content {position: relative;}

  .topnav.responsive .dropdown .dropbtn {

    display: block;

    width: 100%;

    text-align: left;

  }

}

Try commenting out the media query and see what happens:

/*  @media screen and (max-width: 600px) {  */

  .topnav.responsive {position: relative;}

  .topnav.responsive .icon {

    position: absolute;

    right: 0;

    top: 0;

  }

  .topnav.responsive a {

    float: none;

    display: block;

    text-align: left;

  }

  .topnav.responsive .dropdown {float: none;}

  .topnav.responsive .dropdown-content {position: relative;}

  .topnav.responsive .dropdown .dropbtn {

    display: block;

    width: 100%;

    text-align: left;

  }

/* } */

Paul-M - Community Expert
Legend
November 16, 2018

Hi Emma ,can you post a link to the website?

Paul-M - Community Expert
emmad23117089
Known Participant
November 16, 2018

Hi Energize

Thanks for your reply, its not live at the moment unfortuantely.

I'm now also having issues with the custom.css some of the styling has just stopped working but some havent

How can I let you see it do I need to get it live first?

Kind regards

Emma

Legend
November 16, 2018

Have you altered the default Bootstrap css file?

If not and your custom.css file is relatively short you could paste the html code and custom.css styles in the forum.

It's advisable to upload the page somewhere though so we can see it live.