简体   繁体   中英

Removing navigation bar in responsive website layout

I am trying to remove a navigation bar on the mobile site for my website. however, the display: none; doesn't seem to do anything. I am not sure if the javascript I have used is affecting this as I can't stop the javascript from running at the screen width breakpoint. to remove the elements in javaScript, I tried using screen.width

I have linked the code below:

 // When the user scrolls down 50px from the top of the document, resize the header's font size window.onscroll = function() {scrollFunction()}; function scrollFunction() { if (document.body.scrollTop > 30 || document.documentElement.scrollTop > 30) { document.getElementById("navbar").style.fontSize = "30px"; document.getElementById("logo").style.width = "100px"; document.getElementById("nav").style.paddingTop = "20px"; } else { document.getElementById("navbar").style.fontSize = "90px"; document.getElementById("logo").style.width = "200px"; document.getElementById("nav").style.paddingTop = "100px"; document.getElementById("navbar").style.backgroundColor = 'green' ; } }
 li a{ color: black; } li a:visited{ color: black;} li a:hover{ color: #cebea1;} /*for Desktop*/ @media only screen and (min-width: 600px) { .mobileNav{display: none;} #navbar { background-color: white; padding-top: 25px; color: black; text-align: center; font-size: 90px; font-weight: bold; position: fixed; top: 0; left: 0; width: 100%; transition: 1s ease-in-out; } .nav{ font-size: 20px; border-width:1px 0; list-style:none; margin:0; text-align:center; } .nav li{ display:inline; } .nav a{ display:inline-block; padding:10px; padding-right: 20px; padding-top: 45px; text-decoration: none; font-size: 15px; font-family: 'Quicksand', sans-serif; } #logo{ padding-left: 10px; height: auto; width: 200px; transition: 1s ease-in-out; } #navSpace{ margin-top:260px; content: '.'; visibility: hidden; } /*For Mobile*/ @media only screen and (max-width: 600px) { #navbar{display: none;} .nav{display: none;} .nav li{display: none;} .nav a{display: none;} #logo{display: none;} #navSpace{ margin-top:0px; content: '';} .mobileNav li { float: left; } .mobileNav li a { padding: 20px 30px; } .mobileNav .menu { clear: none; float: right; max-height: none; } .mobileNav .menu-icon { display: none; } }
 <!doctype html> <html> <head> <meta charset="utf-8"> <title>home</title> <meta meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <link href="css/normalize.css" rel="stylesheet" type="text/css"> <link href="css/navbar.css" rel="stylesheet" type="text/css"> <link href="css/about.css" rel="stylesheet" type="text/css"> <link href="https://fonts.googleapis.com/css?family=Merienda+One&display=swap" rel="stylesheet"> <script src="js/navbar.js"></script> </head> <!--navigation bar--> <!-- desktop version--> <div id="navbar"> <a href="page.html"><img id="logo" src="images/logoBlack.png"></a> <ul class="nav"> <li><a href="/">About</a></li> <li><a href="/work/">Work</a></li> <li><a href="/media.html">Media</a></li> <li><a href="/contact">Contact</a></li> </ul></div> <!--movile version--> <header class="mobileNav"> <a href="" class="logo">CSS Nav</a> <input class="menu-btn" type="checkbox" id="menu-btn" /> <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label> <ul class="menu"> <li><a href="#about">About</a></li> <li><a href="#work">Work</a></li> <li><a href="#media">Media</a></li> <li><a href="#contact">Contact</a></li> </ul> </header> <!-- space under desktop nevigation bar--> <div id="navSpace"></div> <!-- end of navigation bar--> <body> </body> </html>

Your HTML structure is not between the <body> tags. Also there is a "}" missing on #navbar.

/*for Desktop*/
@media only screen and (min-width: 600px) { 
   ...
  #navbar {
    background-color: white;
    ...
    transition: 1s ease-in-out;
  } <- missing

}

'all nested css Inside @media screen and (min-width: 600px) will only going to fire if width < 600px . And that's what makes your another media queries which is @media only screen and (max-width: 600px) nested inside of it will never going to fire.

mobile first design is which I think the most easiest way to solve this problem.

CSS code:

li a {
  color: black;
}

li a:visited {
  color: black;
}

li a:hover {
  color: #cebea1;
}

#navbar {
  display: none;
}

#navSpace {
  margin-top: 0px;
  content: "";
}
.mobileNav li {
  float: left;
}
.mobileNav li a {
  padding: 20px 30px;
}
.mobileNav .menu {
  clear: none;
  float: right;
  max-height: none;
}
.mobileNav .menu-icon {
  display: none;
}

@media only screen and (min-width: 600px) {
  .mobileNav {
    display: none;
  }

  #navbar {
    background-color: white;
    padding-top: 25px;
    color: black;
    text-align: center;
    font-size: 90px;
    font-weight: bold;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    transition: 1s ease-in-out;
    display: block;
  }

  .nav {
    font-size: 20px;
    border-width: 1px 0;
    list-style: none;
    margin: 0;
    text-align: center;
  }
  .nav li {
    display: inline;
  }
  .nav a {
    display: inline-block;
    padding: 10px;
    padding-right: 20px;
    padding-top: 45px;
    text-decoration: none;
    font-size: 15px;
    font-family: "Quicksand", sans-serif;
  }

  #logo {
    padding-left: 10px;
    height: auto;
    width: 200px;
    transition: 1s ease-in-out;
  }

  #navSpace {
    margin-top: 260px;
    content: ".";
    visibility: hidden;
  }
}

check codesandbox for demo:

https://codesandbox.io/s/jovial-sea-ojwee?file=/style.css:0-1265

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM