The navigation menu is obstructing the website content in mobile view

I am facing an issue with the navigation menu on my website. When I switch to mobile resolution to test responsiveness, the menu ends up overlapping the website content. How can I fix this problem? Below is a screenshot of the current situation. Screenshot HTML Code:

<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" />
    <link rel="stylesheet" href="css/navbar.css" />
    <title>Navigation bar</title>
  </head>
  <body>
    <header>
      <div id="brand"><a href="main.php">Cactus Soup</a></div>
      <nav>
        <ul>
          <li><a href="/home">Top Filmes</a></li>
          <li><a href="/products">Top Series</a></li>
          <li><a href="/about">About Us</a></li>
          <li id="login"><a href="/login" >Login</a></li>
          <li id="signup"><a href="/signup">Sign Up</a></li>
        </ul>
      </nav>
      <div id="hamburger-icon" onclick="toggleMobileMenu(this)">
        <div class="bar1"></div>
        <div class="bar2"></div>
        <div class="bar3"></div>
        <ul class="mobile-menu">
          <li><a href="/home">Top Films</a></li>
          <li><a href="/products">Top Series</a></li>
          <li><a href="/about">About Us</a></li>
          <li id="login"><a href="/login" >Login</a></li>
          <li id="signup"><a href="/signup">Sign Up</a></li>
        </ul>
      </div>
    </header>
    <script src="index.js"></script>
    asjjajjgassssssssssssssssssssssssssssssssssssssssssssssssssssssssssssa
  </body>
</html>

CSS Code:

@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap");

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  background-color: #353836;
  color: white;
  font-family: "Poppins", sans-serif;
}

header a {
  text-decoration: none;
}

header {
  padding: 0 20px;
  background-color: #1d1f1d;
  height: 50px;
  display: flex;
  justify-content: space-between;
}

#brand {
  font-weight: bold;
  font-size: 18px;
  display: flex;
  align-items: center;
}

#brand a {
  color: #09c372;
}

ul {
  list-style: none;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

ul a {
  color: white;
}

ul li {
  padding: 5px;
  margin-left: 10px;
}

ul li:hover {
  transform: scale(1.1);
  transition: 0.3s;
}

#login,
#signup {
  border-radius: 5px;
  padding: 5px 8px;
}

#login {
  border: 1px solid #498afb;
}

#signup {
  border: 1px solid #ff3860;
}

#signup a {
  color: #ff3860;
}

#login a {
  color: #498afb;
}

#hamburger-icon {
  margin: auto 0;
  display: none;
  cursor: pointer;
}

#hamburger-icon div {
  width: 35px;
  height: 3px;
  background-color: white;
  margin: 6px 0;
  transition: 0.4s;
}

.open .bar1 {
  -webkit-transform: rotate(-45deg) translate(-6px, 6px);
  transform: rotate(-45deg) translate(-6px, 6px);
}

.open .bar2 {
  opacity: 0;
}

.open .bar3 {
  -webkit-transform: rotate(45deg) translate(-6px, -8px);
  transform: rotate(45deg) translate(-6px, -8px);
}

.open .mobile-menu {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

.mobile-menu {
  display: none;
  position: absolute;
  top: 50px;
  left: 0;
  height: calc(100vh - 50px);
  width: 100%;
  background-color: black;
}

.mobile-menu li {
  margin-bottom: 10px;
}

@media only screen and (max-width: 600px) {
  header nav {
    display: none;
  }

  #hamburger-icon {
    display: block;
  }
}

JavaScript Code:

function toggleMobileMenu(menu) {
    menu.classList.toggle('open');
}

If anyone could assist me with resolving this issue, I would greatly appreciate it!

Answer №1

For responsive design, it's recommended to utilize @media queries and adjust padding or margin as needed.

@media only screen and (max-width: 600px) {
  body {
    padding-top: 20px;
  }
} 

Check out this resource for more details: https://www.w3schools.com/cssref/css3_pr_mediaquery.asp

Answer №2

From my understanding, the issue you are facing occurs when the Menu is open and you adjust the resolution, causing the menu to remain visible by default. To resolve this, you can include a `resize` eventListener that will automatically close the menu if it is open and the screen size exceeds `600`. Below is an illustration:

const menu = document.getElementById("hamburger-icon");

window.addEventListener("resize", () => {
    // Do nothing if window size is smaller than 600 or menu is not open
    if (window.innerWidth <= 600 || !menu.classList.contains("open"))
        return;

    // Remove "open" class if condition is not met
    menu.classList.remove("open");
});

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

Double trigger caused by selection and modification events

I'm struggling to find a solution for this particular issue. I have implemented a textbox-like control that utilizes .select and .change events. As the user types, the textbox provides options to select from. The issue arises when selecting a value t ...

React scroll not triggering class changes

In the function handleScroll, I am attempting to add a class of red when scrolling down to a specific limit, otherwise applying a class of blue. However, it seems that it is only entering the else statement and also logging undefined for e.target.scrollTop ...

Trigger a popup notification with JavaScript when

Check out this snippet of code: <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/ ...

Incorporating Progressive Web App Support into a JavaServer Faces Web Application

Exploring the possibility of integrating Progressive Web App support into a JavaServerFaces web application has become a focal point for our team. As our JSF app continues to evolve, we foresee the need to provide offline access to certain parts of the app ...

Transition delays in Tailwind CSS when dark mode is activated

My dark mode toggle is functioning with the following CSS: :root { --primary: var(--white); } .dark { --primary: var(--black); } However, when I include the following code: * { @apply transition-colors duration-700; } The background colors of ...

Tips for locking the position of a table header and allowing only the table body to scroll

I have designed a table with tbody consisting of 2 tr elements. One tr is used for hiding/showing data. I am trying to keep the Table header fixed while only scrolling the table body. However, as I am using angular js ng-repeat on the tbody, I am encounte ...

Tips on incorporating live data into charts using MVC?

Recently I started working with ASP.NET MVC on my own project and have encountered some difficulties. One particular challenge I am facing is how to create a line chart using data from a database. Let me explain the issue in more detail: Within my databa ...

Complicated alignment of third-party (Facebook "like" and Google +1) buttons with CSS styling

What is the optimal arrangement for positioning the Google+1 and Facebook Like buttons to ensure they line up neatly? Currently, the initial element in my body is this heading, which appears at the top of all pages (lightly modified after logging in): &l ...

Create an Android application using Node.js

While developing my mobile application, I am utilizing html5 with node.js to create a chat box for users connected on the same wireless network. However, I encounter an issue when coding on my desktop using node.js software. How can I overcome this challen ...

What is the best way to show two icons next to each other within a React component?

For my school project, I am working on developing an app and encountering a challenge. I have been trying to display two icons side by side on a screen using React (I've been learning React for 5 months but still consider myself a beginner). However, ...

AngularJS view does not wait for the completion of $http.get request

Within my controller, the code snippet below is present... $scope.products = dataService.getJsonData(); console.log($scope.products); The corresponding code in my dataservice is as follows: .service('dataService', function ($http) { t ...

Tips for creating an adjustable background image size

Is it possible to adjust the height of a section to match the background using CSS? Currently, the height is determined by the content within the section. CSS: .content-block { background-size: cover; background-position: center center; } HTML: &l ...

Unable to transfer Google API geocoded information to a Django form

I am currently working with Django and the Google Maps API to geocode an address. The goal is to retrieve the results and display them in a form that will then be forwarded to another view. After entering the address into the form input (form.user_entered ...

Is there a way to retrieve the aria-label attribute value from this specific WebElement?

When utilizing Java Selenium, I have access to a WebElement object that corresponds to the following HTML node: <td role="gridcell" class="mat-calendar-body-cell ng-star-inserted" style="width: 25%; padding-top: 7.14286%; paddin ...

The React Vite application encountered an issue: There is no loader configured for ".html" files at ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

**Encountered errors in a React Vite web app** ** ✘ [ERROR] No loader is configured for ".html" files: ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html ../server/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:86 ...

What is the best way to manage the output of Angular UI TinyMCE?

I have been working on an AngularJS frontend app that sends all content to the server through JSON requests. One of the features of the app requires the use of a WYSIWYG editor, so I decided to go with the ui-tinymce directive. Everything was running smo ...

Change the color of the text in a shiny dashboard

While exploring shiny dashboard, I came across this link that explains how to modify colors in the sidebar and header. However, I'm struggling to change the font color for sidebar items. The default white font color becomes unreadable when using light ...

Customize expanded and collapsed icons in PrimeNG treeTable component

I am currently utilizing the treeTable component from primeNG. My query is regarding how to customize the default expand and collapse icons. Although I attempted to make adjustments, I am uncertain if my approach is correct. What I aim to achieve is simpl ...

Arrange four Divs next to each other with flexbox styling

I've been struggling with aligning my cards side by side. They are a series of divs nested in lists under a <ul> Changing the positioning is not resolving the issue, and I'm hesitant to alter the display as it's crucial for responsive ...

Tips for obtaining the total height of an SVG illustration

As a novice, I am currently attempting to convert a PSD file into HTML. My main goal is to achieve the full height of the background image within the .hero class. What steps should I take to accomplish this successfully? * { margin:2px; } .container{ ma ...