Tips for creating a responsive navbar with a hamburger icon that functions correctly without exceeding 100vw

Hey, I was in the middle of my project and really wanted to create a responsive navbar with a hamburger icon on the right side. I followed a tutorial on YouTube to achieve this.

Everything was going smoothly until I tested the code on my mobile device after deploying it online. I encountered an issue. Please check out the demo of the project here and visit the actual site at: (try viewing it in mobile view)

Whenever I double-tap the screen, it zooms out and looks weird as the height extends beyond 100vh, and the same happens with the width too.

What changes should be made in the following code to make it work correctly?

Index.html

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="style.css">
        <title>Document</title>
    </head>
    <body>
        <nav>
            <div class="logo">
                <h3>College Facemash</h3>
            </div>
                <ul class="nav-links">
                    <li><a href="">Home</a></li>
                    <li><a href="">About</a></li>
                    <li><a href="">Login / Signup</a></li>
                </ul>
           <div class="burger">
               <div class="line1"></div>
               <div class="line2"></div>
               <div class="line3"></div>
           </div>
        </nav>

        <script>
const navslide = () =>{
    const burger = document.querySelector('.burger')
    const nav = document.querySelector('.nav-links')
    const navLinks = document.querySelectorAll('.nav-links li')
    burger.addEventListener('click', () => {
        nav.classList.toggle('nav-active')

        navLinks.forEach((link, index) => {
            if(link.style.animation){

                link.style.animation = '';
            }else{
                link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`;

            }
        });

        burger.classList.toggle('toggle')

    })


}

navslide();
</script>

    </body>
    </html>

and Style.css

 @import url('https://fonts.googleapis.com/css?family=Poppins&display=swap');

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
nav{
    display:flex;
    justify-content: space-around;
    align-items: center;
    min-height: 8vh;
    font-family: 'Poppins', sans-serif;
    background-color:#512da8;

}

.logo{
    color:white;
    font-size: 1.5rem;
}
.nav-links{
    display:flex;
    justify-content:space-around;
    width: 35%;
}
.nav-links li{
    list-style: none;
}
.nav-links a{
    text-decoration: none;
    color: white;
    font-weight:bold;
    font-size: 1rem;
}
.burger{
    display:none
}
.burger div{
    width:25px;
    height: 2px;
    margin:5px;
    background-color: white;
    transition: all 0.3s ease;
}

@media screen and (max-width: 1024px){
    .nav-links{
        width: 40%;
    }
}

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

    body{
        overflow-x: hidden;

    }

    .nav-links{

        position:absolute;
        right: 0px;
        height: 92vh;
        top:8vh;
        background-color: #512da8;
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 50%;
        transform: translateX(100%);
        transition: transform 0.5s ease-out;

    }

    .nav-links li{
       opacity: 0;
    }
    .burger{
        display: block;
        cursor: pointer;
    }

    .nav-active{
        transform: translateX(0%);
    }

    @keyframes navLinkFade {
        from {
            opacity: 0;
            transform: translateX(50px);
        }
        to{
            opacity: 1;
            transform: translateX(0px);
        }

    }

    .toggle .line1{
        transform: rotate(-45deg) translate(-5px,5px);


    }
    .toggle .line2{
        opacity: 0
    }
    .toggle .line3{
        transform: rotate(45deg) translate(-5px,-5px);

    }

}

Answer №1

To control the viewport, use the <meta /> tag. Prevent zooming on the page by setting the minimum and maximum scales to 1.00, along with user-scalable=no and target-densityDpi=device-dpi (to prevent browser issues on high-density screens like retina displays). Replace your meta tag with the following:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densityDpi=device-dpi" />

Please note: While this feature is widely supported on mobile platforms, there may be limitations on certain platforms regarding minimum/maximum boundaries.

You can also refer to the viewport CSS descriptor, which includes support for min-zoom and max-zoom based on a working draft of css device adaptation. However, the reliability of this method may be lower compared to the former approach.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble with Font Display in React App

Currently, I have implemented a process to import fonts from Google Fonts and have successfully applied these fonts to all elements on the index page. However, there seems to be an issue with applying these fonts correctly. Within index.html: <link h ...

What is the process of transforming this jQuery script into AngularJS code?

Currently I am facing a situation where I am utilizing Angular1.x for a project. On a specific subpage, there are small clickable images along with some additional content below these images. The requirement is that only the images should be visible init ...

Tips on creating a PDF with a table that includes correctly placed page breaks

My PHP project is built with Laravel and I am trying to generate PDFs from a database using DOM PDF. Initially, everything was working fine when only one page was shown. However, as soon as I tried to generate multiple pages with page breaks, the display b ...

Access several different dynamic URLs through a single Ajax request

I have a scenario where I am showing multiple dynamic links within the same div. The first link works perfectly fine with ajax loading the content, but the subsequent links do not work. How can I make it so that the content of all links can be loaded withi ...

Tips for optimizing HTML5 markup elements to render effectively in Internet Explorer

How can I ensure that HTML5 markup elements such as <header>, <section>, and <footer> are displayed properly in Internet Explorer? I've noticed that when I apply CSS to these elements, the styling doesn't always work as expecte ...

Obtaining a Variable Element through Selector

When working with Puppeteer, I am faced with the challenge of clicking on a web button that has a dynamic id like: #product-6852370-Size. Typically, I would use the following code: page.click('#product-6852370-Size'); However, the number withi ...

I am looking to showcase a series of icons linked together by connecting lines

I have successfully designed the layout and added icons, but I am facing difficulty in creating connecting lines between them. I attempted to utilize CSS borders and pseudo-elements, yet I cannot achieve the desired outcome. If anyone could offer a CSS-ba ...

Place the application bar at the bottom of the screen

https://i.sstatic.net/0B0Ad.png I am working on a project aimed at monitoring employees, and I have developed a user interface that allows for modifications within the site. However, there is an issue with the "bottom App Bar" section at the bottom of thi ...

Issues with Mouse Hover/Image Replacement

In this particular example, I am attempting to achieve a simple functionality where an image changes when the mouse hovers over it, and reverts back to the original image when the mouse is no longer hovering. However, despite my efforts, there seems to be ...

Validation does not occur when the submit button has an ng-click event attached to it

This is the form I created: <form action="" ng-controller="forgotCtrl as forgot" name="forgotForm"> <div class="form-group row"> <label for="email" class="col-sm-2 form-control-label">Email address</label> ...

Retrieving the first child in a tree via URL parameters using JavaScript, rather than the currently selected one

I'm currently facing a specific issue with my webpage and I will elaborate on what's happening. Within my page, I have created a list containing multiple sections. Here is an example of how it looks: <ul class="menu__list--nested active& ...

Tips for creating a deepCss selector for an input Textbox in Protractor

When I attempt to use sendKeys in an input textbox with Protractor, the element is located within a shadow-root and there are three separate input textboxes. ...

Formatting Specific Labels in HighCharts: A Guide

I'm trying to display the label values within a Highcharts bar chart by utilizing the dataLabels.Formatter function. However, I've noticed that when the bar size is inconsistent, the labels are sometimes displayed inside the bar, which looks goo ...

Conceal certain elements from being printed by the client side on a website

Imagine logging into social media but not wanting to see the number of "likes" on posts as the page loads. For example, when you open a photo from a friend and underneath it shows "Somefriend, someotherfriend, and 123123 persons like this", you may want t ...

How can I make Bootstrap fit my Custom Grid?

I have received specific measurements from a customer for a customized grid layout, and I am unsure of how to implement it. Here are the measurements provided: **Width: 320px** Sides: (26px) (2 sides) Column: (52px) (4 columns) Gutter: (20px) (3 gutter ...

Tips for positioning an inner div within an outer div

Code Snippet: <div style="width: 300px; position: absolute; right: 25%; top: 5%; -webkit-box-shadow: 2px 4px 40px 0px rgba(0,0,0,0.75); -moz-box-shadow: 2px 4px 40px 0px rgba(0,0,0,0.75);box-shadow: 2px 4px 40px 0px rgba(0,0,0,0.75);"> <secti ...

Ensure alignment of gradients between two divs, even if their widths vary

Among my collection of 10 inline divs, each one boasts a unique width and color gradient. While the 45-degree lines are uniform across all the divs, is there a way to ensure that these gradients match seamlessly? I've shared my CSS code snippet below ...

Creating a custom backdrop for your kaboom.js webpage

I created a kaboom.js application and I'm having trouble setting a background for it. I've searched online extensively and attempted different methods on my own, but nothing seems to be working. (StackOverflow flagged my post as mostly code so I ...

Incorporate the functionality of Drag and Drop for vertically sorting rows

Looking for a way to implement DRAG and DROP functionality on rows in my HTML code. <table> <tr class="table-header"> <th>Title</th> <th>Name</th> <th>Modified By</th> < ...

Unable to shift to the side while in full flexibility mode

I ran into an issue with my code, shown in the image below. I am trying to position 1 on the left and 2 on the right, but something seems off. Can you spot the problem? <div className="mt-5 mx-5 py-3 shadow bg-white"> &l ...