Search code examples
javascripthtmlcssmobilehamburger-menu

Hamburger button is not on the right place


I made a website, which is mobile responsive with a sticky header. But on Iphones the hamburger button is not in the right place, and also doesn't move with the sticky header perfectly. I don't know it is because of the Safari, however, I did not meet with this problem on other tools, only on iPhones. If I change the place of the Hamburger button in one place it won't look good on another phone.

I don't use Bootstrap. My question is, how could I make my hamburger button to look good on every mobile?

window.onscroll = function() {myFunction()};
            
var header = document.getElementById("myHeader");
var sticky = header.offsetTop;

function myFunction() {
  if (window.pageYOffset > sticky) {
  header.classList.add("sticky");
  } else {
  header.classList.remove("sticky");
  }
}
* {
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
}

body {
    font-family: 'Montserrat', sans-serif;
    margin: 0;
    padding: 0;
        
    }
  
  
header {
    display: flex;
    height: 20vh;
    margin: auto;
    align-items: center;
    border-bottom: 1px solid var(--clr-accent);
}

.logo-container,
.nav-links {
    display: flex;
}

.logo-container {
    flex: 1;
    position: relative;
    left: 5%;
}

.logo {
    font-weight: 400;
    margin: 5px;
}

#myLogo{
    max-width: 120px;
    max-height: 120px;
}


.logo-container img{
    max-width: 120px;
    max-height: 120px;
}



/* Logo container JS*/
.logo-container { display: 'none' }
.logo-container.open { display: 'block' }



nav {
    flex: 2;
    display: flex; /* Make nav a flexbox container , also this makes a new problem*/
     

}
.nav-links {
    margin-left:15%;
    margin-right: 15%;
    justify-content: center;
    justify-content: space-between;
    list-style: none;
    flex: 1; /* Let it occupy rest of the container */
    align-items: center; /* Align to the vertical center because logo is bigger. */
}


.nav-link {
    color: var(--clr-dark);
    font-size:20px;
    text-decoration: none;
    font-weight: 600;
}

.sticky {
    position: fixed;
    top: 10;
    height: 20vh;
    width:100%;
    align-items: center;
    background: rgba(255, 255, 255, 1);

  }
@keyframes drop {
    0% {
        opacity: 0;
        transform: translateY(-80px);
    }
    100% {
        opacity: 1;
        transform: translateY(0px);
    }
}

    @media screen and (max-width: 767px){
        /* Logo container JS*/
        .logo-container { display: 'block' }


        .line{
            width: 30px;
            height: 3px;
            background: var(--clr-accent);
            margin: 5px;
        }

        header{
            background: white;
        }

        nav{
            position: relative;
            
        }

        .hamburger{
            position: fixed; /*this was absolute*/
            cursor: pointer;
            right: 5%;
            top: 20%;
            transform: translate(-5%, -280%); /*this was 200 with absolute*/
            z-index: 2;
        }

        .nav-links
        {   margin-left:0%;
            margin-right: 0%;
            justify-content: space-evenly;
            background-color: var(--clr-light);
            position: fixed;
            height: 100vh;
            width:100%;
            flex-direction: column;
            clip-path: circle(0px at 57% 10%);
            -webkit-clip-path: circle(0px at 57% 10%);
            transition: all 1s ease-out;
            pointer-events: none;
            text-align: center;
            z-index: 1;
        }

        .nav-links.open{
            clip-path: circle(1000px at 57% 10%);
            -webkit-clip-path: circle(1000px at 57% 10%);
            pointer-events:all;
        }

        .nav-links li{
            opacity: 0;
            
        }

        .navlinks li a{
            font-size: 25px;
            

        }

        .nav-links li:nth-child(1){
            transition: all 0.5s ease 0.2s;

        }

        .nav-links li:nth-child(2){
            transition: all 0.5s ease 0.4s;
            
        }


        .nav-links li:nth-child(3){
            transition: all 0.5s ease 0.6s;
            
        }

        li.fade{
            opacity: 1;
        }

        .nav-link {
             color: var(--clr-dark);
            font-size: 18px;
        }

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


    .cta-select {
        border: 2px solid #f48024;
        background: transparent;
        color: #f48024;
        width: 100px;
        height: 35px;
        font-size: 12px;
    }

    

    .cta-add {
        background: #f48024;
        width: 100px;
        height: 35px;
        font-size: 12px;
        margin: 30px 0px 0px 10px;
    }
    
    .cover img {
        height: 65%;
        padding: 15px; /*safari*/
    }
    .small-circle,
    .medium-circle,
    .big-circle {
        opacity: 0.25;

    }
    .nav-link {
        
        /* font-size:10px; */
        text-decoration: none;
        font-weight: 600;
    }


    .logo-container {
        left: 2%;
    
    }

    .logo-container img{
        max-width: 65px;
        max-height: 65px;
    }

    .calendar {
        right: 2%;
        visibility: hidden;
        
    }

    .logo-click{
        display: none;
        visibility: hidden;
    }



    .header {
        top: 10;
        height: 20vh;
        width:100%;
        align-items: center;
    }


      .hamburger{
        transform: translate(0%, -285%); /*this was 200 with absolute*/
        z-index: 2;
    }
        
    
      }
    
    
    @media screen and (max-width: 420px) {
    
        .hamburger{
            transform: translate(0%, -330%); 
            z-index: 2;
        }

        .cover img {
            height: 56%;
            margin: 5px;
            padding: 5px;
        }





        }


        

        @media screen and (max-width: 415px) {
    
            .hamburger{
                transform: translate(0%, -300%); /*this was 200 with absolute*/
                z-index: 2;
            }
    
            .cover img {
                height: 56%;
                margin: 5px; /*Safari*/
                padding: 5px;
            }
    
    
    
    
            }

        

        @media screen and (max-width: 376px) {
    
            .hamburger{
                transform: translate(0%, -320%); /*this was 200 with absolute*/
                z-index: 2;
            }
    
            .cover img {
                height: 56%;
                margin: 5px;
                padding: 5px;
            }
    
    
    
            }




        @media screen and (max-width: 361px) {
    
            .hamburger{
                transform: translate(0%, -270%); /*this was 200 with absolute*/
                z-index: 2;
            }
    
            .cover img {
                height: 56%;
                margin: 5px;
                padding: 5px;
            }
    
    
    
            }


            @media screen and (max-width: 321px) {
    
                .hamburger{
                    transform: translate(0%, -245%); /*this was 200 with absolute*/
                    z-index: 2;
                }
        
                .cover img {
                    height: 56%;
                    border-radius: 50px;
                    margin: 5px;
                    padding: 5px;
                }

                }
<body>

        <header class="header" id="myHeader">
            <script src="https://www.google.com/recaptcha/api.js"></script>
            <nav role="navigation">
                <div class="logo-container" id="myLogo"> 
                    <a href="#"><img src="./img/logo.png"  alt="logo"/> </a>
                    </div>

                <div class="hamburger" id="hamburgerID">
                    <div class="line"></div>
                    <div class="line"></div>
                    <div class="line"></div>

                </div>
                <ul class="nav-links">
                    <li><a class="nav-link" href="#details"> DETAILS</a></li>
                    <li><a class="nav-link" href="#description">DESCRIPTION</a></li>
                    <li><a class="nav-link" href="#aboutus">ABOUT US</a></li>
            
                </ul>
            </nav>

        </header>


Solution

  • First Method

    add align-items: center; to nav tag. that is

    nav{align-items: center;}
    

    Also remove top:20%; in hamburger class. That is

    .hamburger{top:20%}
    

    position:fixed element having atleast assign one of the top, left, bottom, right.

    So, here right:5% is only enough.

    window.onscroll = function() {myFunction()};
                
    var header = document.getElementById("myHeader");
    var sticky = header.offsetTop;
    
    function myFunction() {
      if (window.pageYOffset > sticky) {
      header.classList.add("sticky");
      } else {
      header.classList.remove("sticky");
      }
    }
    * {
      margin: 0px;
      padding: 0px;
      box-sizing: border-box;
      --clr-accent:#111;
    }
    
    body {
      font-family: 'Montserrat', sans-serif;
      margin: 0;
      padding: 0;
    }
    
    header {
      display: flex;
      height: 20vh;
      margin: auto;
      align-items: center;
      border-bottom: 1px solid var(--clr-accent);
    }
    
    .logo-container,
    .nav-links {
      display: flex;
    }
    
    .logo-container {
      flex: 1;
      position: relative;
      left: 5%;
    }
    
    .logo {
      font-weight: 400;
      margin: 5px;
    }
    
    #myLogo{
      max-width: 120px;
      max-height: 120px;
    }
    
    
    .logo-container img{
      max-width: 120px;
      max-height: 120px;
    }
    
    /* Logo container JS*/
    .logo-container { display: 'none' }
    .logo-container.open { display: 'block' }
    
    nav {
      flex: 2;
      display: flex; /* Make nav a flexbox container , also this makes a new problem*/
      align-items: center;
    }
    
    .nav-links {
      margin-left:15%;
      margin-right: 15%;
      justify-content: center;
      justify-content: space-between;
      list-style: none;
      flex: 1; /* Let it occupy rest of the container */
      align-items: center; /* Align to the vertical center because logo is bigger. */
    }
    
    .nav-link {
      color: var(--clr-dark);
      font-size:20px;
      text-decoration: none;
      font-weight: 600;
    }
    
    .sticky {
      position: fixed;
      top: 10px;
      height: 20vh;
      width:100%;
      align-items: center;
      background: rgba(255, 255, 255, 1);
    }
    
    @keyframes drop {
      0% {
        opacity: 0;
        transform: translateY(-80px);
      }
      100% {
        opacity: 1;
        transform: translateY(0px);
      }
    }
    
    @media screen and (max-width: 767px){
      /* Logo container JS*/
      .logo-container { display: 'block' }
    
      .line{
        width: 30px;
        height: 3px;
        background: var(--clr-accent);
        margin: 5px;
      }
      header{
        background: white;
      }
      nav{
        position: relative;
      }
    
      .hamburger{
        position: fixed; /*this was absolute*/
        cursor: pointer;
        right: 5%;
        z-index: 2;
      }
    
      .nav-links
      {   
        margin-left:0%;
        margin-right: 0%;
        justify-content: space-evenly;
        background-color: var(--clr-light);
        position: fixed;
        height: 100vh;
        width:100%;
        flex-direction: column;
        clip-path: circle(0px at 57% 10%);
        -webkit-clip-path: circle(0px at 57% 10%);
        transition: all 1s ease-out;
        pointer-events: none;
        text-align: center;
        z-index: 1;
      }
      .nav-links.open{
        clip-path: circle(1000px at 57% 10%);
        -webkit-clip-path: circle(1000px at 57% 10%);
        pointer-events:all;
      }
      .nav-links li{
        opacity: 0;
    
      }
      .navlinks li a{
        font-size: 25px;
      }
      .nav-links li:nth-child(1){
        transition: all 0.5s ease 0.2s;
      }
      .nav-links li:nth-child(2){
        transition: all 0.5s ease 0.4s;
      }
      .nav-links li:nth-child(3){
        transition: all 0.5s ease 0.6s;
      }
      li.fade{
        opacity: 1;
      }
      .nav-link {
       color: var(--clr-dark);
       font-size: 18px;
     }
    
    }
    
    
    @media screen and (max-width: 1024px) {
    
      .cta-select {
        border: 2px solid #f48024;
        background: transparent;
        color: #f48024;
        width: 100px;
        height: 35px;
        font-size: 12px;
      }
      .cta-add {
        background: #f48024;
        width: 100px;
        height: 35px;
        font-size: 12px;
        margin: 30px 0px 0px 10px;
      }
      .cover img {
        height: 65%;
        padding: 15px; /*safari*/
      }
      .small-circle,
      .medium-circle,
      .big-circle {
        opacity: 0.25;
      }
      .nav-link {
        /* font-size:10px; */
        text-decoration: none;
        font-weight: 600;
      }
    
      .logo-container {
        left: 2%;
      }
    
      .logo-container img{
        max-width: 65px;
        max-height: 65px;
      }
      .calendar {
        right: 2%;
        visibility: hidden;
      }
      .logo-click{
        display: none;
        visibility: hidden;
      }
      .header {
        top: 10;
        height: 20vh;
        width:100%;
        align-items: center;
      }
      .hamburger{
        z-index: 2;
      }
    }
    
    
    @media screen and (max-width: 420px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px;
        padding: 5px;
      }
    }
    
    @media screen and (max-width: 415px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px; /*Safari*/
        padding: 5px;
      }
    }
    
    @media screen and (max-width: 376px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px;
        padding: 5px;
      }
    
    }
    
    @media screen and (max-width: 361px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px;
        padding: 5px;
      }
    }
    
    @media screen and (max-width: 321px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        border-radius: 50px;
        margin: 5px;
        padding: 5px;
      }
    
    }
    
    .scrolling{color:#fff;background:orange;height:800px;padding:30px;}
    <body>
    
      <header class="header" id="myHeader">
        <script src="https://www.google.com/recaptcha/api.js"></script>
        <nav role="navigation">
          <div class="logo-container" id="myLogo"> 
            <a href="#"><img src=""  alt="logo"/> </a>
          </div>
          <div class="hamburger" id="hamburgerID">
            <div class="line"></div>
            <div class="line"></div>
            <div class="line"></div>
          </div>
          <ul class="nav-links">
            <li><a class="nav-link" href="#details"> DETAILS</a></li>
            <li><a class="nav-link" href="#description">DESCRIPTION</a></li>
            <li><a class="nav-link" href="#aboutus">ABOUT US</a></li>
          </ul>
        </nav>
      </header>
    
      <div class="scrolling">Content Here</div>
      
    </body>

    Second Method

    We can align Hamburger icon to center by without translate instead of adding

    top:20%; 
    

    to calculated by

    top: calc((20vh - 30px) / 2); // header height = 20vh and hamburger height = 30px makes half of both to align middle of header
    

    window.onscroll = function() {myFunction()};
                
    var header = document.getElementById("myHeader");
    var sticky = header.offsetTop;
    
    function myFunction() {
      if (window.pageYOffset > sticky) {
      header.classList.add("sticky");
      } else {
      header.classList.remove("sticky");
      }
    }
    * {
      margin: 0px;
      padding: 0px;
      box-sizing: border-box;
      --clr-accent:#111;
    }
    
    body {
      font-family: 'Montserrat', sans-serif;
      margin: 0;
      padding: 0;
    }
    
    header {
      display: flex;
      height: 20vh;
      margin: auto;
      align-items: center;
      border-bottom: 1px solid var(--clr-accent);
    }
    
    .logo-container,
    .nav-links {
      display: flex;
    }
    
    .logo-container {
      flex: 1;
      position: relative;
      left: 5%;
    }
    
    .logo {
      font-weight: 400;
      margin: 5px;
    }
    
    #myLogo{
      max-width: 120px;
      max-height: 120px;
    }
    
    
    .logo-container img{
      max-width: 120px;
      max-height: 120px;
    }
    
    /* Logo container JS*/
    .logo-container { display: 'none' }
    .logo-container.open { display: 'block' }
    
    nav {
      flex: 2;
      display: flex; /* Make nav a flexbox container , also this makes a new problem*/
    }
    
    .nav-links {
      margin-left:15%;
      margin-right: 15%;
      justify-content: center;
      justify-content: space-between;
      list-style: none;
      flex: 1; /* Let it occupy rest of the container */
      align-items: center; /* Align to the vertical center because logo is bigger. */
    }
    
    .nav-link {
      color: var(--clr-dark);
      font-size:20px;
      text-decoration: none;
      font-weight: 600;
    }
    
    .sticky {
      position: fixed;
      top: 10px;
      height: 20vh;
      width:100%;
      align-items: center;
      background: rgba(255, 255, 255, 1);
    }
    
    @keyframes drop {
      0% {
        opacity: 0;
        transform: translateY(-80px);
      }
      100% {
        opacity: 1;
        transform: translateY(0px);
      }
    }
    
    @media screen and (max-width: 767px){
      /* Logo container JS*/
      .logo-container { display: 'block' }
    
      .line{
        width: 30px;
        height: 3px;
        background: var(--clr-accent);
        margin: 5px;
      }
      header{
        background: white;
      }
      nav{
        position: relative;
      }
    
      .hamburger{
        position: fixed; /*this was absolute*/
        cursor: pointer;
        right: 5%;
        top: calc((20vh - 30px) / 2); // header height = 20vh and hamburger height = 30px makes half of both to align middle of header
        z-index: 2;
      }
    
      .nav-links
      {   
        margin-left:0%;
        margin-right: 0%;
        justify-content: space-evenly;
        background-color: var(--clr-light);
        position: fixed;
        height: 100vh;
        width:100%;
        flex-direction: column;
        clip-path: circle(0px at 57% 10%);
        -webkit-clip-path: circle(0px at 57% 10%);
        transition: all 1s ease-out;
        pointer-events: none;
        text-align: center;
        z-index: 1;
      }
      .nav-links.open{
        clip-path: circle(1000px at 57% 10%);
        -webkit-clip-path: circle(1000px at 57% 10%);
        pointer-events:all;
      }
      .nav-links li{
        opacity: 0;
    
      }
      .navlinks li a{
        font-size: 25px;
      }
      .nav-links li:nth-child(1){
        transition: all 0.5s ease 0.2s;
      }
      .nav-links li:nth-child(2){
        transition: all 0.5s ease 0.4s;
      }
      .nav-links li:nth-child(3){
        transition: all 0.5s ease 0.6s;
      }
      li.fade{
        opacity: 1;
      }
      .nav-link {
       color: var(--clr-dark);
       font-size: 18px;
     }
    
    }
    
    
    @media screen and (max-width: 1024px) {
    
      .cta-select {
        border: 2px solid #f48024;
        background: transparent;
        color: #f48024;
        width: 100px;
        height: 35px;
        font-size: 12px;
      }
      .cta-add {
        background: #f48024;
        width: 100px;
        height: 35px;
        font-size: 12px;
        margin: 30px 0px 0px 10px;
      }
      .cover img {
        height: 65%;
        padding: 15px; /*safari*/
      }
      .small-circle,
      .medium-circle,
      .big-circle {
        opacity: 0.25;
      }
      .nav-link {
        /* font-size:10px; */
        text-decoration: none;
        font-weight: 600;
      }
    
      .logo-container {
        left: 2%;
      }
    
      .logo-container img{
        max-width: 65px;
        max-height: 65px;
      }
      .calendar {
        right: 2%;
        visibility: hidden;
      }
      .logo-click{
        display: none;
        visibility: hidden;
      }
      .header {
        top: 10;
        height: 20vh;
        width:100%;
        align-items: center;
      }
      .hamburger{
        z-index: 2;
      }
    }
    
    
    @media screen and (max-width: 420px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px;
        padding: 5px;
      }
    }
    
    @media screen and (max-width: 415px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px; /*Safari*/
        padding: 5px;
      }
    }
    
    @media screen and (max-width: 376px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px;
        padding: 5px;
      }
    
    }
    
    @media screen and (max-width: 361px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        margin: 5px;
        padding: 5px;
      }
    }
    
    @media screen and (max-width: 321px) {
      .hamburger{
        z-index: 2;
      }
      .cover img {
        height: 56%;
        border-radius: 50px;
        margin: 5px;
        padding: 5px;
      }
    
    }
    
    .scrolling{color:#fff;background:orange;height:800px;padding:30px;}
    <body>
    
      <header class="header" id="myHeader">
        <script src="https://www.google.com/recaptcha/api.js"></script>
        <nav role="navigation">
          <div class="logo-container" id="myLogo"> 
            <a href="#"><img src=""  alt="logo"/> </a>
          </div>
          <div class="hamburger" id="hamburgerID">
            <div class="line"></div>
            <div class="line"></div>
            <div class="line"></div>
          </div>
          <ul class="nav-links">
            <li><a class="nav-link" href="#details"> DETAILS</a></li>
            <li><a class="nav-link" href="#description">DESCRIPTION</a></li>
            <li><a class="nav-link" href="#aboutus">ABOUT US</a></li>
          </ul>
        </nav>
      </header>
    
      <div class="scrolling">Content Here</div>
      
    </body>