Positioning the navbar in a fixed location

My goal is to create a responsive navbar with absolute positioning and have the menu items float right. However, when I add the position property, the menu items end up floating left instead. Additionally, applying absolute positioning to the nav element causes the hamburger icon for small screens to float left, and the closing icon disappears when it opens.

I need help finding the correct solution to make the nav element absolute without affecting the menu items and the hamburger icon.

Please test the code snippet on both desktop and mobile screens

    document.getElementById("hamburger").addEventListener("click", function() {
      this.classList.toggle("active");
      document.querySelector(".mobile-menu").classList.toggle("active");
    });
body {
  background-color: rgb(0, 0, 0);
  margin: 0;
  overflow-x: hidden;
  cursor: pointer;
}

img {
  margin-top: 100px;
}

@media (max-width: 767px) {
  img {
    margin-top: 40px;
    height: 80px;
  }
}

*,
*:before,
*:after {
  box-sizing: inherit;
  padding: 0;
  margin: 0;
  list-style: none;
  text-decoration: none;
}


/* Navigation Menu */

nav {
  height: 70px;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
}

nav #menu {
  display: flex;
  height: 100%;
}

nav #menu li {
  padding: 0px 30px;
  line-height: 70px;
  transition: all 0.3s ease-out;
}

nav #menu li a {
  color: #fff;
}

nav #menu li a:hover {
  color: rgb(238, 215, 12);
}

@media (max-width: 767px) {
  nav #menu {
    display: none;
  }
}


/* Mobile Menu */

#hamburger {
  position: absolute;
  float: right;
  right: 10px;
  top: 14px;
  z-index: 999;
  width: 40px;
  height: 40px;
  cursor: pointer;
  transition: all 0.3s ease-out;
  visibility: hidden;
  opacity: 0;
}

#hamburger .line {
  height: 5px;
  background: rgb(238, 215, 12);
  margin: 5px auto;
  backface-visibility: hidden;
}

#hamburger.active #one {
  transform: rotate(45deg) translateX(6px) translateY(6px);
}

#hamburger.active #two {
  opacity: 0;
}

#hamburger.active #three {
  transform: rotate(-45deg) translateX(10px) translateY(-12px);
}

@media (max-width: 767px) {
  #hamburger {
    visibility: visible;
    opacity: 1;
  }
}

.mobile-menu {
  z-index: 1;
  position: absolute;
  top: 0px;
  background: black;
  width: 100%;
  height: 100%;
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s ease-out;
  display: table;
}

.mobile-menu .mobile-menu__items {
  height: 50px;
  display: table-cell;
  vertical-align: middle;
}

.mobile-menu .mobile-menu__items li {
  display: block;
  text-align: center;
  padding: 20px 0;
  text-align: center;
  font-size: 35px;
  min-height: 50px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease-out;
}

.mobile-menu .mobile-menu__items li:hover {
  color: rgb(238, 215, 12);
}

.mobile-menu .mobile-menu__items li:hover a {
  transition: all 0.3s ease-out;
  color: rgb(238, 215, 12);
}

.mobile-menu .mobile-menu__items li a {
  color: white;
}

.mobile-menu.active {
  visibility: visible;
  opacity: 0.99;
}

@media (min-width: 768px) {
  .mobile-menu {
    visibility: hidden !important;
  }
}


/* Main */

h1 {
  z-index: 1;
  color: #fff;
  left: 0;
  text-align: center;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>About</title>
  <link rel="apple-touch-icon" sizes="180x180" href="images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="images/favicon-16x16.png">
  <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>
  <!-- Navigation Menu -->
  <header>
    <nav>
      <a href="home"><img src="images/logo.svg" alt="logo"></a>
      <ul id="menu">
        <li><a href="home">HOME</a></li>
        <li><a href="work">WORK</a></li>
        <li><a href="about">ABOUT</a></li>
        <li><a href="javascript:;">CONTACT</a></li>
      </ul>

      <div id="hamburger">
        <div class="line" id="one"></div>
        <div class="line" id="two"></div>
        <div class="line" id="three"></div>
      </div>
    </nav>
    <!-- Mobile Menu -->
    <div class="mobile-menu">
      <ul class="mobile-menu__items">
        <li><a href="home">HOME</a></li>
        <li><a href="work">WORK</a></li>
        <li><a href="about">ABOUT</a></li>
        <li><a href="javascript:;">CONTACT</a></li>
      </ul>
    </div>
  </header>
  <!-- Main -->
  <main>

    <h1>hello</h1>
  </main>


</body>

</html>

Answer №1

Include the following code in your CSS file to modify the menu appearance:

 nav ul li a {display: block; width: 100%; float: right; text-align: right;}

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

Adjust the Font Size of Bootstrap 3 across different breakpoints

I am currently using Bootstrap 3 and I have a requirement to adjust the font-size at different breakpoints across my website. My goal is to easily modify the font size in one place and have it automatically apply to all Bootstrap components. The desired b ...

Send data to a different page using an iframe form

I am trying to send a value to another page form from an iframe in the following way. <input form="form1" type="search><input form="form1" type="submit"> <iframe src="anotherdomain"> <form action="foo.php" method="post" i ...

Using HTML5 and CSS transitions to scale an image within a 960 grid layout

Having some trouble achieving a smooth transition for the height and width of images on my webpage. I believe if you take a look at my page, not much explanation is needed: Click here to see the image transition The goal is to enlarge the image to double ...

Using jQuery to send a POST request with a multidimensional form array

I developed a form that compiles data from various inputs into a multidimensional array. By using the standard form submission function, I can easily navigate to the target page where a PHP script interprets the multidimensional array correctly and generat ...

Is there a way to disable responsiveness on a website?

Currently, I am in the process of developing a responsive style sheet that is live on our website. However, it seems to display poorly on non-desktop devices due to being a work in progress. I am considering using JavaScript to enforce the desktop layout ...

Manipulating the DOM with PHP, jQuery, and JavaScript

In my project, I have a dynamic list that is populated from the database and sorted by dates using the time field in ascending order. To achieve this, I am utilizing a PHP foreach loop. <li id="events"> Time: <?php echo $venue['time']; ...

display the HTML form when the page is clicked

Trying to implement an onclick method for selecting form types. I have multiple form types available - example here Clicking on one submenu should open up the desired form type directly below the menu How can I dynamically load a form based on the select ...

Why does my PHP script execute before receiving any input?

As a newcomer to PHP from C#, I am currently developing a whois module for a website. However, I'm encountering an issue where my PHP page automatically runs and displays the message "invalid input" before I even click the button. Can anyone explain w ...

Padding beneath font only seen in Apple devices

My horizontal navigation bar and footer appear perfectly on my PC, but when I tested it on a Mac, the font seemed to be raised about 30px above its intended position in the navigation bar. After attempting various CSS resets and adjustments to the line-he ...

Incorporate a font-awesome icon directly within the input field

My div element contains a span element, which in turn includes an i tag. However, the i element extends outside of both the span and div elements. I am attempting to place an eye icon inside an input field. Here is my current code - what adjustments shou ...

Optimal strategies for designing navigation bars on contemporary websites

Struggling to find a solid answer for this dilemma. I have multiple html pages and a single navigation bar that I want to include on all pages for consistency. The idea of duplicating the html code for the navigation bar goes against everything I've l ...

Create an interactive HTML table featuring objects as the selected values

I have been attempting to generate an HTML table that is filled with objects. The goal was to make the table selectable by row (via hover), triggering a function when the row is hovered over. Here are the table headers stored in an array: var topTitles ...

Incorporating an image into a list of checkboxes post-connection

Looking for a way to populate a checkbox list through datasource/databind and then integrate an image with each checkbox? I can do both separately but struggling to combine them successfully. My task is to create a checkbox list of students for teachers t ...

What is the best way to create spacing between images in a CSS image slider?

UPDATE: I've made significant progress by modifying the code provided in the link below. I am very close to achieving my desired result, but there are parts of it that still puzzle me. The transition now has spacing on both sides and the hidden space ...

jQuery UI DatePicker: without any styling, no picking dates

I'm struggling to configure a jQuery UI DatePicker. I moved the development-bundle folder contents to the directory res/jqueryui and initialized two datepickers as shown below: <script src="res/jquery-2.1.0.min.js"></script> <script sr ...

Activate the toggle feature on the navigation bar using Bootstrap

Below is the code I am using: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8defe2e2f9fef9ffecfdcdb8a3bda3bf">[email protected]</a>/dist/css/bootstrap.min.css" rel ...

Is there a way to bring a child of an element with a lower z-index to the front, even if it is behind a child of another element with a higher

I'm facing a situation where I have the following layout: <div style="z-index: 10"> <div>Whatever</div> </div> <div style="z-index: 9"> <div><div>Haaaleluia</div></div> </div> ...

augmentable form that can be expanded flexibly

Perhaps I'm missing something really simple here, but I just can't seem to figure this out. I'm attempting to create a form that can dynamically extend. The issue I'm facing is that I can't get this particular code to function: & ...

The sample Ajax XML code provided by W3Schools is ineffective

While studying xml parsing in ajax on w3schools.com, I came across an example that functioned perfectly online. However, when I saved it to my desktop as abc.html and note.xml, the XML values weren't displaying. Even though the HTML elements were vis ...

Incantation within ng-include | src involving a series of characters

Is there a way to create a URL in ng-include|src by combining an expression and a constant string? I've attempted the code below, but it doesn't seem to be working. <aside ng-include src="{{staticPath}} + 'assets/tpl/products-feed-histor ...