1

I'm trying to clone a website for learning purposes with W3CSS and plain javascript. Here is the stuff I already created. My problem is, that the div with id #menu is not visible on the screen after clicking the hamburger icon on small screens, however, it is present in the devtools of chrome and ff when it is toggled. I have checked lot's of things in the css like display:, z-index:, background-color:, overflow:, whatsoever, even tried to print stuff with js to console regarding computed style and whatnot, but nothing led me to any solution. When I checked the above pen on my mobile phone with chrome, the toggle button (the one with the hamburger icon) was not positioned in the middle of the menu bar and when clicked it, to open the dropdown, the first menu item became visible (actually the top part of the <a> element which happend to fit in the top navigation bar). I'll post the complete html && css && js code here too, bc of this annoying red exclamation mark.

html

function toggleMenu() {
  let toggler = document.getElementById('menu-toggler');
  let menu = document.getElementById(toggler.dataset.menu);

  if (hasClass(menu, 'w3-hide-small')) {
    rmClass(menu, 'w3-hide-small');
    console.log(window.getComputedStyle(menu).backgroundColor);
    let children = menu.childNodes;
    let height = 0;
    for (let child of children) {
      if (child instanceof Element) {
        height += child.offsetHeight;
      }
    }
    menu.style.height = height + 'px';
  } else {
    menu.style.height = '';
    // 500 is the length of the transition
    window.setTimeout(() => {
      addClass(menu, 'w3-hide-small');
    }, 500);
  }
}

function hasClass(elem, cl) {
  if (elem.className.includes(cl)) {
    return true;
  }
  return false;
}

function addClass(elem, cl) {
  if (!hasClass(elem, cl)) {
    elem.className += ' ' + cl;
  }
}

function rmClass(elem, cl) {
  if (hasClass(elem, cl)) {
    elem.className = elem.className.replace(' ' + cl, '');
  }
}
.w3-hover-flat-emerald:hover {
  color: #fff;
  background-color: #2ecc71 !important;
}

nav {
  height: 80px;
}

nav .w3-hide-small a {
  height: 80px;
  line-height: 64px;
}

nav .w3-hide-small a:hover {
  color: #2ecc71 !important;
  background-color: transparent !important;
}

nav img {
  height: 80px;
}

nav .w3-content {
  position: relative;
}

nav button {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translate(0, -50%);
}

#menu {
  transition: height 0.5s linear 0.1s;
  overflow: hidden;
}

@media screen and (max-width: 600px) {
  #menu {
    height: 0;
  }
  nav .w3-hide-small a {
    height: auto;
  }
}
<!doctype html>
<html>

<head>
  <title>copy_lesson</title>

  <meta name="viewport" content="width=device-width, intial-scale=1">
  <meta charset="utf-8">

  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://www.w3schools.com/lib/w3-colors-camo.css">
  <link rel="stylesheet" href="../w3.css/w3-colors-flat.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">

  <link rel="stylesheet" href="style/css/main.css">
</head>

<body>
  <nav class="w3-bar w3-camo-black w3-xlarge">
    <div class="w3-content">
      <a href="#">
        <img class="w3-padding" src="http://www.hhtrans.sk/img/logo.png" alt="logo">
      </a>
      <button onclick="toggleMenu()" id="menu-toggler" data-menu="menu" type="button" class="w3-hover-flat-emerald w3-margin-right w3-button w3-hide-medium w3-hide-large w3-right">
                    <i class="fas fa-bars"></i>
                </button>
      <div id="menu" class="w3-mobile w3-right w3-hide-small w3-large">
        <a href="#" class="w3-button w3-bar-item w3-mobile">Home</a>
        <a href="#" class="w3-button w3-bar-item w3-mobile">About</a>
        <a href="#" class="w3-button w3-bar-item w3-mobile">Contacts</a>
      </div>
    </div>
  </nav>

  <script src="style/js/main.js"></script>
</body>

</html>

Any would be appreciated. Thanks in advance :)

Barmar
  • 741,623
  • 53
  • 500
  • 612
YoFreakinLo
  • 15
  • 1
  • 6
  • Can you please make a public Stack Snippet or using JSBin or something? – Praveen Kumar Purushothaman Oct 12 '18 at 16:26
  • 1
    Why aren't you using `.classList.add` and `.classList.remove`? – Barmar Oct 12 '18 at 16:28
  • Did you inspect the element while the dev tools were open? Did you look at the "Computed Styles" tab? What about the `height` and/or `width` of the element? The dev tools will tell you what you need to know. – Scott Marcus Oct 12 '18 at 16:28
  • @PraveenKumarPurushothaman He made a codepen, isn't that "or something"? – Barmar Oct 12 '18 at 16:29
  • @Barmar Ha, didn't notice that. The here was so misleading... `:(` – Praveen Kumar Purushothaman Oct 12 '18 at 16:31
  • Sorry for not responding yesterday. @Barmar i don't use them because I didn't know about them. – YoFreakinLo Oct 13 '18 at 16:39
  • @ScottMarcus I did inspect a lot of thing through the dev tools and by logging them to the console, as I pointed it out in the question. – YoFreakinLo Oct 13 '18 at 16:39
  • 1
    @PraveenKumarPurushothaman I'm sory it was misleading to you. I thought it was common practice to hide links behind regular text like that :) – YoFreakinLo Oct 13 '18 at 16:41
  • @YoFreakinLo I was surprised you know about `classList.includes()` but not the others. – Barmar Oct 13 '18 at 16:41
  • @Barmar yeah I was using some knowledge from the [W3.CSS](https://www.w3schools.com/w3css/w3css_dropdowns.asp) website I studied before (that's why I used className) and the intellisense or however it's called of my editor (VSCode) where the className had only indexof and includes functions which seemed to be all right to use for my problem :) anyways thank you for letting me know there is other ways to perform this special task :) – YoFreakinLo Oct 13 '18 at 16:50
  • @YoFreakinLo Na, it was me. I was a bit blind... `:P` – Praveen Kumar Purushothaman Oct 14 '18 at 08:13

1 Answers1

0

The problem was with this .w3-bar has overflow: hidden and you are using it there. Also, another thing is, your fonts are all white, so with the white background, you won't be able to see anything.

  1. Remove the w3-bar from <nav>.
  2. Add some background colour to #menu.

Snippet

function toggleMenu() {
  let toggler = document.getElementById('menu-toggler');
  let menu = document.getElementById(toggler.dataset.menu);

  if (hasClass(menu, 'w3-hide-small')) {
    rmClass(menu, 'w3-hide-small');
    console.log(window.getComputedStyle(menu).backgroundColor);
    let children = menu.childNodes;
    let height = 0;
    for (let child of children) {
      if (child instanceof Element) {
        height += child.offsetHeight;
      }
    }
    menu.style.height = height + 'px';
  } else {
    menu.style.height = '';
    // 500 is the length of the transition
    window.setTimeout(() => {
      addClass(menu, 'w3-hide-small');
    }, 500);
  }
}

function hasClass(elem, cl) {
  if (elem.className.includes(cl)) {
    return true;
  }
  return false;
}

function addClass(elem, cl) {
  if (!hasClass(elem, cl)) {
    elem.className += ' ' + cl;
  }
}

function rmClass(elem, cl) {
  if (hasClass(elem, cl)) {
    elem.className = elem.className.replace(' ' + cl, '');
  }
}
.w3-hover-flat-emerald:hover {
  color: #fff;
  background-color: #2ecc71 !important;
}

nav {
  height: 80px;
}

nav .w3-hide-small a {
  height: 80px;
  line-height: 64px;
}

nav .w3-hide-small a:hover {
  color: #2ecc71 !important;
  background-color: transparent !important;
}

nav img {
  height: 80px;
}

nav .w3-content {
  position: relative;
}

nav button {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translate(0, -50%);
}

#menu {
  transition: height 0.5s linear 0.1s;
  overflow: hidden;
}

@media screen and (max-width: 600px) {
  #menu {
    height: 0;
    background: #333;
  }
  nav .w3-hide-small a {
    height: auto;
  }
}
<!doctype html>
<html>

<head>
  <title>copy_lesson</title>

  <meta name="viewport" content="width=device-width, intial-scale=1">
  <meta charset="utf-8">

  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://www.w3schools.com/lib/w3-colors-camo.css">
  <link rel="stylesheet" href="../w3.css/w3-colors-flat.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">

  <link rel="stylesheet" href="style/css/main.css">
</head>

<body>
  <nav class="w3-camo-black w3-xlarge">
    <div class="w3-content">
      <a href="#">
        <img class="w3-padding" src="http://www.hhtrans.sk/img/logo.png" alt="logo">
      </a>
      <button onclick="toggleMenu()" id="menu-toggler" data-menu="menu" type="button" class="w3-hover-flat-emerald w3-margin-right w3-button w3-hide-medium w3-hide-large w3-right">
                    <i class="fas fa-bars"></i>
                </button>
      <div id="menu" class="w3-mobile w3-right w3-hide-small w3-large">
        <a href="#" class="w3-button w3-bar-item w3-mobile">Home</a>
        <a href="#" class="w3-button w3-bar-item w3-mobile">About</a>
        <a href="#" class="w3-button w3-bar-item w3-mobile">Contacts</a>
      </div>
    </div>
  </nav>

  <script src="style/js/main.js"></script>
</body>

</html>

Preview

working

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    thank you for finding the problem, which I should have been able to find by myself :) I was struggling with this issue for a long time, several days now. The background color was there in my code though I was tinkering with the pen through mobile when I couldn't use my computer, maybe I deleted it and forgot to put it back or something. Anyways, your answer was very helpful – YoFreakinLo Oct 13 '18 at 16:53
  • @YoFreakinLo Why don't you use Bootstrap? AFAIK, W3Schools and their products are the worst ever in this market. – Praveen Kumar Purushothaman Oct 14 '18 at 08:12
  • well first of all I didn't know about w3css being that bad, bc, even in universities their websites are recommended to learn from, so I thought they are pretty capable to create something worthy to use. Second of all, w3.css and it's classes are (for now at least) closer to me, i.e. I can learn it more easily, and it's not as robust as bootstrap. What I mean by that is, that it is a much lower level or simpler thing than bootstrap. But I'm trying to clone that website I mentioned with both tools and I'm about to decide which one is more convenient for me to use – YoFreakinLo Oct 15 '18 at 11:11
  • @YoFreakinLo Sure. I guessed it. Check out [W3Fools](https://www.w3fools.com/) and educate others too... `:)` Have a nice day. – Praveen Kumar Purushothaman Oct 15 '18 at 14:17
  • @PraveenKumarPurushothaman W3Schools is a great resource - as is MDN. The site you linked is now basically defunct and the authors have issued a retraction of their former reviews and criticisms. – CramerTV Jan 18 '23 at 20:00
  • @CramerTV The comment was given 4 to 5 years ago, definitely it might be outdated. – Praveen Kumar Purushothaman Jan 18 '23 at 22:35