How come the navigation menu is still visible on the screen when its width is supposed to be set to zero?

I encountered an issue with the code where the navigation items, previously links inside a div, now wrapped in UL and LI tags do not disappear off-screen to the right as intended.

Additionally, I am exploring the possibility of enclosing this UL within a div and incorporating other elements above and below the disappearing menu. Is this feasible?

<!DOCTYPE html>
<html>
<head>

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceaca1a1babdbabcafbe8efbe0ffe0fd">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="74161b1b00070006150434415a455a47">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Additional CSS styles -->
<style>
body {
  font-family: "Lato", sans-serif;
}

#main-menu-container{
  right:0;
}

.sidenav {
  height: 100%;
  margin:0;
  width: 500px;
  position: fixed;
  z-index: 1;
  top: 0px;
  right: 0;
  background: radial-gradient(#2b5d87, #264c6d);
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 160px;
  text-align:center;
  list-style: none;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  text-transform: uppercase;
  color:white;
  font-weight:bold;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 20px;
  font-size: 36px;
  margin-left: 50px;
  color:#ffdc00;
}

#main {
  transition: margin-right .5s;
  padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
</style>
</head>


<body>
<span style="font-size:30px;cursor:pointer; position:absolute; top:0; right:20px; color:#ffdc00;" onclick="openNav()">&#9776;</span>
<div id="main-menu-container">
    <p>testing where this will go</p>
    <ul id="mySidenav" class="sidenav">
        <li><a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Clients</a></li>
        <li><a href="#">Contact</a></li>
      </div>
</div>


<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  
</div>

<script>
function openNav() {
  document.getElementById("mySidenav").style.width = "500px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}
</script>
   
</body>
</html> 

Answer №1

Ensure that padding-left is set to 0.

body {
  font-family: "Lato", sans-serif;
}

#main-menu-container{
  right:0;
}

.sidenav {
  height: 100%;
  margin:0;
  width: 500px;
  position: fixed;
  z-index: 1;
  top: 0px;
  right: 0;
  background: radial-gradient(#2b5d87, #264c6d);
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 160px;
  text-align:center;
  list-style: none;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  text-transform: uppercase;
  color:white;
  font-weight:bold;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 20px;
  font-size: 36px;
  margin-left: 50px;
  color:#ffdc00;
}

#main {
  transition: margin-right .5s;
  padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
<!DOCTYPE html>
<html>
<head>

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1914140f080f091a0b3b4e554a5548">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3818c8c979097918293a3d6cdd2cdd0">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<meta name="viewport" content="width=device-width, initial-scale=1">

</head>


<body>
<span style="font-size:30px;cursor:pointer; position:absolute; top:0; right:20px; color:#ffdc00;" onclick="openNav()">&#9776;</span>
<div id="main-menu-container">
    <p>testing where this will go</p>
    <ul id="mySidenav" class="sidenav">
        <li><a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Clients</a></li>
        <li><a href="#">Contact</a></li>
      </div>
</div>


<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  
</div>

<script>
function openNav() {
  document.getElementById("mySidenav").style.width = "500px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
   document.getElementById("mySidenav").style.paddingLeft = "0";
}
</script>
   
</body>
</html>

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

Issues with the Bootstrap Grid System not functioning correctly when inter-component communication is needed in Angular from parent to

This is the code from app.component.html file: <div class="container"> <div class="row" id="ads"> <div class="col-xs-4"> <app-card *ngFor="let car of cars" [carNotifyBadge]="car.carNotifyBadge" [ca ...

Utilizing Superscripts and Subscripts within HTML forms

Currently, I am in the process of developing a JavaScript web application that involves working with chemical formulas. One key feature I need to implement is allowing users to input superscripts easily. Ideally, I would like the text editor to automatic ...

Guide to customizing margins at specific breakpoints in Bootstrap 4 without using javascript

Despite exploring numerous solutions and trying them out, I have yet to achieve the desired results. I am looking to customize margins for various breakpoints in Bootstrap. For example: View for laptops, Tablet view The issue may be related to defined w ...

Sticky positioning with varying column widths

How can I create HTML and CSS columns that stick together without manually specifying the "left:" parameter every time? The current example in the fiddle achieves what I want, but requires manual setting of the "left:" value. Is there a way to make this m ...

JQuery receives the data returned by AJAX

Here is my ajax call: jQuery.ajax({ type: "POST", // Choosing between HTTP POST or GET url: 'inc/functions.php?func=get_usr&id='+usr, // The location for making Ajax calls success:function(data){ if (data. ...

Encountering the error code 'ERR_EMPTY_RESPONSE' while utilizing an AJAX-powered live search feature

My website features a live AJAX search bar that retrieves records from a MySQL database. However, when users repeatedly conduct searches by modifying the search criteria, some web browsers display an error message stating 'ERR_EMPTY_RESPONSE'. ...

place some words above the radio selection option

Looking to add text above a checkbox? Consider the following HTML code: <div class="radio"> <input type="radio" name="optradio" value="public" checked>Public </div> I am trying to create a CSS class that formats elements like this: ...

How should props be properly passed to NavItem components?

Although my code is functional, it produces an error. I am seeking advice on how to prevent this error from occurring in the future. I am eager to improve my coding skills and write accurate code. This is a snippet of my current code: export const Aut ...

Making modifications to the database will trigger real-time updates in the web interface. How is it possible to achieve this seamlessly?

Can someone please help me understand how to automatically insert news or new events from a Facebook page as an event in a webpage? I am a programmer and currently, the data is retrieved from the database using AJAX without reloading the page. However, I ...

Dynamic navigation bar that fades in and out while scrolling

My goal is to create a navigation bar that is initially hidden when the page loads, but appears when you scroll down to the second section. I have managed to make it work, however, there is an issue when scrolling up and down within the home section - the ...

Experimenting with Extjs compatibility using Selenium

I am currently in the process of testing an extjs application using Selenium. I am utilizing Selenium IDE for Firefox and Javascript with Eclipse on Chrome. Despite spending countless hours searching for a solution to my problem, I have been unable to iden ...

Encountered a problem with watchify while attempting to run the npm watch command

Hello there, I am currently following a tutorial on scotch.io where I am attempting to create a music player using electron and react. However, when I try to run 'npm run watch' I encounter the following error message: [email protected] watc ...

My method for updating form input properties involves switching the disable attribute from "false" to "true" and vice versa

I have a form that uses Ajax to submit data. Once the user submits the form, the text is updated to indicate that the data was sent successfully, and then the form is displayed with the fields filled out. I want to display the form but prevent users from r ...

What is the best way to include a drop-right menu within a dropdown menu?

Hey there! I'm working on a page that features a dropdown navbar. My goal is to create a secondary dropdown that appears to the right when hovering over an li element within the initial dropdown. Any tips on how to accomplish this? Thanks in advance! ...

Avoid scrolling when the element changes size

I've created an angular directive that conceals element a when the user scrolls on element b. The functionality works smoothly, but I'm encountering a peculiar behavior that has me puzzled: https://i.sstatic.net/NadtZ.gif Although it may not be ...

Unable to replicate the functionality of the tab key using jQuery for the enter key

How can I focus on the first input ('Qtd on the table') after pressing enter on the 'Buscar' input? I've tried various solutions like $(this).nextAll('input').first().focus(); $(this).next('input:text').focus ...

Optimizing workflow with express.js, backbone.js, and connect-assets for maximum efficiency

As a newcomer to node.js, I'm embarking on the challenge of setting up an application that utilizes Backbone.js on the client-side while being supported by express.js and node.js for server-side extensibility. The lack of online examples showcasing a ...

Creating a visually appealing webpage by utilizing Bootstrap and HTML for the layout

I am in the process of learning html and Bootstrap for CSS. Below is the code I currently have: <div class="container"> <div class="row text-center mb-4 mt-2"> <div class="col-md-12"> <div cla ...

Outside drop shadows in CSS3 creates an interesting visual effect by giving

I am currently using the following CSS styling for a list of li items. border: 1px solid black; border-radius:10px; box-shadow: 8px 8px 4px #666; -o-box-shadow: 10px 10px 5px #888; -icab-box-shadow: 10px 10px 5px #888; -khtml-box-shadow: 10px 10px 5px #8 ...

Incorporate the select2 plugin into your AngularJS project for enhanced functionality

In my AngularJS application, I am utilizing the select2 plugin to showcase a list of entities (tags). This snippet shows part of my template: select.ddlTags(ui-select2="select2Options", multiple, ng-model="link.tags") option(ng-repeat="tag in tags", ...