Home > Enterprise >  Hamburger Responsive Navbar Alignment : CSS Flexbox
Hamburger Responsive Navbar Alignment : CSS Flexbox

Time:11-26

So this is a very focused question

  • First of all, I want you to execute the code in full screen
  • Please inspect the code and set it to a mobile width ( This is a problem with responsiveness )
  • Now, open the hamburger menu
  • As you can see in this image, The links of the navbar are not centered in the y axis
  • Even though I have clearly included justify content and align items in the below code

Code:

/* CSS Setup And Reset */

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");
@font-face {
  font-family: AstroSpace;
  src: url(/fonts/AstroSpace.ttf);
}

body {
  margin: 0;
  padding: 0;
  font-family: "Montserrat", sans-serif;
  background-color: #265eac;
  color: white;
}

header {
  background-color: #2c304b;
  position: sticky;
  top: 0;
  right: 0;
  width: 100%;
  margin-bottom: 20px;
}


/* Navbar */

.main-nav {
  height: 90px;
}

.logo {
  color: white;
  line-height: 90px;
  font-size: 30px;
  font-weight: 900;
  text-decoration: none;
  margin-left: 30px;
  font-family: "Roboto  ", sans-serif;
}

.navlinks {
  list-style: none;
  float: right;
  line-height: 90px;
  margin: 0;
  padding: 0;
}

.navlinks li {
  display: inline-block;
  margin: 0px 20px;
}

.navlinks li a {
  color: white;
  text-decoration: none;
  font-size: 18px;
  transition: all 0.3s linear 0s;
  text-transform: uppercase;
}

.navlinks li a:hover {
  color: #7ebcb9;
  padding-bottom: 7px;
  border-bottom: 2px solid #7ebcb9;
}

li a.contact {
  background-color: #00adb5;
  padding: 9px 20px;
  border-radius: 50px;
  transition: all 0.3s ease 0s;
  border-bottom: none;
}

li a.contact:hover {
  background-color: #047e85;
  color: white;
  border-bottom: none;
}

#check {
  display: none;
}

.menu-btn {
  font-size: 25px;
  color: white;
  float: right;
  line-height: 90px;
  margin-right: 40px;
  display: none;
  cursor: pointer;
}


/* Responsive Design */


/* As you can see, justify content and align items are included, but the links are not centere */

@media (max-width: 800px) {
  .navlinks {
    position: fixed;
    top: 90px;
    ;
    width: 100%;
    height: 100vh;
    transition: all 0.5s;
    right: -100%;
    background: #222831;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
  }
  .navlinks li {
    display: block;
  }
  .navlinks li a {
    font-size: 20px;
  }
  .navlinks li a:hover {
    border-bottom: none;
  }
  .menu-btn {
    display: block;
  }
  #check:checked~.navlinks {
    right: 0;
  }
  #check:checked~header {
    margin: 0;
  }
}

@media (max-width: 360px) {
  .logo {
    margin-left: 10px;
    font-size: 25px;
  }
  .menu-btn {
    margin-right: 10px;
    font-size: 25px;
  }
  .menu-btn:focus {
    color: blue;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Responsive Navbar</title>
  <link rel="stylesheet" href="styles/styles.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" />

  <link href="https://fonts.googleapis.com/css2?family=Open Sans:wght@400;800&family=Quicksand:wght@300&family=Roboto:wght@900&display=swap" rel="stylesheet">
  <style>

  </style>
</head>

<body>
  <header>
    <nav class="main-nav">
      <input type="checkbox" id="check" />
      <label for="check" class="menu-btn">
              <i class="fas fa-bars"></i>
            </label>
      <a href="index.html" class="logo">Nikita Gada</a>
      <ul class="navlinks">
        <li><a href="#" id="link1" onmousedown="myfun()" onmouseup="myfun2()">About Me</a></li>
        <li><a href="#" id="link3" onmousedown="myfun5()" onmouseup="myfun6()">Work</a></li>
        <li><a href="#" id="link2" onmousedown="myfun3()" onmouseup="myfun4()">Services</a></li>
        <li><a href="#" class="contact">Contact</a></li>
      </ul>
    </nav>
  </header>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

In this case, I would set the height dynamically with calc Css function, that should fix the problem. Just subtract the height of the navbar .main-nav: 90px to adjust, center the navLinks vertically like this height: calc(100vh - 90px). I hope You will be content ! Best regards ;-) Below my example with changes.

/* CSS Setup And Reset */

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");
@font-face {
  font-family: AstroSpace;
  src: url(/fonts/AstroSpace.ttf);
}

body {
  margin: 0;
  padding: 0;
  font-family: "Montserrat", sans-serif;
  background-color: #265eac;
  color: white;
}

header {
  background-color: #2c304b;
  position: sticky;
  top: 0;
  right: 0;
  width: 100%;
  margin-bottom: 20px;
}


/* Navbar */

.main-nav {
  height: 90px;
}

.logo {
  color: white;
  line-height: 90px;
  font-size: 30px;
  font-weight: 900;
  text-decoration: none;
  margin-left: 30px;
  font-family: "Roboto  ", sans-serif;
}

.navlinks {
  list-style: none;
  float: right;
  line-height: 90px;
  margin: 0;
  padding: 0;
}

.navlinks li {
  display: inline-block;
  margin: 0px 20px;
}

.navlinks li a {
  color: white;
  text-decoration: none;
  font-size: 18px;
  transition: all 0.3s linear 0s;
  text-transform: uppercase;
}

.navlinks li a:hover {
  color: #7ebcb9;
  padding-bottom: 7px;
  border-bottom: 2px solid #7ebcb9;
}

li a.contact {
  background-color: #00adb5;
  padding: 9px 20px;
  border-radius: 50px;
  transition: all 0.3s ease 0s;
  border-bottom: none;
}

li a.contact:hover {
  background-color: #047e85;
  color: white;
  border-bottom: none;
}

#check {
  display: none;
}

.menu-btn {
  font-size: 25px;
  color: white;
  float: right;
  line-height: 90px;
  margin-right: 40px;
  display: none;
  cursor: pointer;
}


/* Responsive Design */


/* As you can see, justify content and align items are included, but the links are not centere */

@media (max-width: 800px) {
  .navlinks {
    position: fixed;
    top: 90px;
    ;
    width: 100%;
    height: calc(100vh - 90px);
    /*     height: 100vh; */
    transition: all 0.5s;
    right: -100%;
    background: #222831;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
  }
  .navlinks li {
    display: block;
  }
  .navlinks li a {
    font-size: 20px;
  }
  .navlinks li a:hover {
    border-bottom: none;
  }
  .menu-btn {
    display: block;
  }
  #check:checked~.navlinks {
    right: 0;
  }
  #check:checked~header {
    margin: 0;
  }
}

@media (max-width: 360px) {
  .logo {
    margin-left: 10px;
    font-size: 25px;
  }
  .menu-btn {
    margin-right: 10px;
    font-size: 25px;
  }
  .menu-btn:focus {
    color: blue;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Responsive Navbar</title>
  <link rel="stylesheet" href="styles/styles.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" />

  <link href="https://fonts.googleapis.com/css2?family=Open Sans:wght@400;800&family=Quicksand:wght@300&family=Roboto:wght@900&display=swap" rel="stylesheet">
  <style>

  </style>
</head>

<body>
  <header>
    <nav class="main-nav">
      <input type="checkbox" id="check" />
      <label for="check" class="menu-btn">
        <i class="fas fa-bars"></i>
      </label>
      <a href="index.html" class="logo">Nikita Gada</a>
      <ul class="navlinks">
        <li><a href="#" id="link1" onmousedown="myfun()" onmouseup="myfun2()">About Me</a></li>
        <li><a href="#" id="link3" onmousedown="myfun5()" onmouseup="myfun6()">Work</a></li>
        <li><a href="#" id="link2" onmousedown="myfun3()" onmouseup="myfun4()">Services</a></li>
        <li><a href="#" class="contact">Contact</a></li>
      </ul>
    </nav>
  </header>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related