Aligning a navigation bar with a hamburger menu in the center

I recently implemented a hamburger menu with some cool animations into my site for mobile devices. Now, I am facing the challenge of centering the menu on desktop screens and it's proving to be tricky. The positioning is off, and traditional methods like justify-content are not doing the trick. I even attempted using a grid layout, but that didn't work either. I've included both my HTML and CSS below in hopes that someone can offer guidance or suggestions on how to achieve proper centering.

My goal is to have the menu appear as a hamburger icon on phones and transform into a standard navigation bar centered at the bottom of the screen for desktops, all while maintaining responsiveness.

I'm still relatively new to web development, so please bear with me if things seem a bit messy :)

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  overflow-x: hidden;
}

body {
  background-color: #262626;

  font-family: "Roboto Slab", serif;

  position: relative;
  overflow-x: hidden;
}

main {
  width: 100vw;
  height: 100vh;
}

#backdrop {
  width: 90%;
  height: 100%;

  margin: 0 auto;
  margin-top: 1rem;
  border-radius: 1rem 1rem 0 0;

  background-color: #727365;
}

#home-link {
  color: #f2f2e4;
  text-decoration: none;
  font-size: 1.7rem;
  font-family: "Space Mono", monospace;
  font-weight: 400;

  position: absolute;
  top: 1.95rem;
  left: 2.6rem;
}

/* Hamburger menu */
#menu a {
  text-decoration: none;
  color: #3f403b;
}

#menu a:hover {
  color: #0c0c0c;
}

#menu-toggle {
  display: block;

  position: absolute;
  top: 2.5rem;
  right: 3rem;

  z-index: 1;
}

#menu-checkbox {
  display: block;

  width: 40px;
  height: 32px;

  position: absolute;
  top: -7px;
  left: -5px;

  cursor: pointer;

  opacity: 0;
  z-index: 2;
}

#menu-toggle span {
  display: block;

  width: 33px;
  height: 4px;

  margin-bottom: 5px;
  position: relative;

  background: #f2f2e4;
  border-radius: 3px;

  z-index: 1;

  transform-origin: 4px 0px;
  transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1),
    opacity 0.55s ease;
}

#menu-toggle span:first-child {
  transform-origin: 0% 0%;
}

#menu-toggle span:nth-last-child(2) {
  transform-origin: 0% 100%;
}

#menu-checkbox:checked ~ span {
  opacity: 1;
  transform: rotate(45deg) translate(-2px, -1px);
  background: #3f403b;
}

#menu-checkbox:checked ~ span:nth-last-child(3) {
  opacity: 0;
  transform: rotate(0deg) scale(0.2, 0.2);
}

#menu-checkbox:checked ~ span:nth-last-child(2) {
  opacity: 1;
  transform: rotate(-45deg) translate(0, -1px);
}

#menu {
  width: 60vw;
  height: 70vh;

  position: absolute;
  right: -100px;

  margin: -100px 0 0 0;
  padding: 50px;
  padding-top: 125px;

  background-color: #bfbfae;
  list-style: none;

  transform-origin: 0% 0%;
  transform: translate(100%, 0);
  transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1);
}

#menu li {
  padding: 10px 0;
  font-size: 22px;
}

#menu-checkbox:checked ~ ul {
  transform: none;
  opacity: 1;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 13px;
}

::-webkit-scrollbar-thumb {
  border-radius: 100px;
  border: 3px solid transparent;
  background-clip: content-box;
  background-color: rgb(88, 88, 88);
}

::-webkit-scrollbar-thumb:hover {
  background-color: rgb(109, 109, 109);
}

/* Media queries */
@media only screen and (min-width: 600px) {
}

@media only screen and (min-width: 769px) {
  #backdrop {
    width: 98%;
    height: 95.5%;
    border-radius: 1rem;
  }

  #menu-checkbox {
    display: none;
  }

  #menu-toggle span {
    display: none;
  }

  #menu {
    -webkit-transition: none !important;
    -moz-transition: none !important;
    -o-transition: none !important;
    transition: none !important;
    transform: none !important;

    padding: 0 1rem 0 1rem;

    position: absolute;
    top: 97vh;
    right: 38.2vw;

    border-radius: 1rem;

    height: fit-content;
    width: max-content;
  }

  #menu li {
    display: inline-block;
  }

  #home-link {
    display: flex;
    justify-content: center;
    position: static;

    padding-top: 0.5rem;
  }
}
<!DOCTYPE html>
<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" />
    <title>OVERCASTING</title>
    <link rel="stylesheet" href="css/style.css" />
    <script src="script/js.js"></script>
    <link
      href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@200;300;400&display=swap"
      rel="stylesheet"
    />
    <link
      href="https://fonts.googleapis.com/css2?family=Space+Mono&display=swap"
      rel="stylesheet"
    />
  </head>
  <body>
    <main>
      <div id="backdrop">
        <header id="wrapper">
          <nav>
            <a href="index.html" id="home-link">OVERCASTING</a>
            <div id="menu-toggle">
              <input id="menu-checkbox" type="checkbox" />
              <span class="hamburger-span"></span>
              <span class="hamburger-span"></span>
              <span class="hamburger-span"></span>
              <ul id="menu">
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
                <li><a href="#">Link 4</a></li>
              </ul>
            </div>
          </nav>
        </header>
      </div>
    </main>
  </body>
</html>

Answer №1

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  overflow-x: hidden;
}

body {
  background-color: #262626;

  font-family: "Roboto Slab", serif;

  position: relative;
  overflow-x: hidden;
}

main {
  width: 100vw;
  height: 100vh;
}

#backdrop {
  width: 90%;
  height: 100%;

  margin: 0 auto;
  margin-top: 1rem;
  border-radius: 1rem 1rem 0 0;

  background-color: #727365;
}

#home-link {
  color: #f2f2e4;
  text-decoration: none;
  font-size: 1.7rem;
  font-family: "Space Mono", monospace;
  font-weight: 400;

  position: absolute;
  top: 1.95rem;
  left: 2.6rem;
}

/* Hamburger menu */
#menu a {
  text-decoration: none;
  color: #3f403b;
}

#menu a:hover {
  color: #0c0c0c;
}

#menu-toggle {
  display: block;

  position: absolute;
  top: 2.5rem;
  right: 3rem;

  z-index: 1;
}

#menu-checkbox {
  display: block;

  width: 40px;
  height: 32px;

  position: absolute;
  top: -7px;
  left: -5px;

  cursor: pointer;

  opacity: 0;
  z-index: 2;
}

#menu-toggle span {
  display: block;

  width: 33px;
  height: 4px;

  margin-bottom: 5px;
  position: relative;

  background: #f2f2e4;
  border-radius: 3px;

  z-index: 1;

  transform-origin: 4px 0px;
  transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1),
    opacity 0.55s ease;
}

#menu-toggle span:first-child {
  transform-origin: 0% 0%;
}

#menu-toggle span:nth-last-child(2) {
  transform-origin: 0% 100%;
}

#menu-checkbox:checked ~ span {
  opacity: 1;
  transform: rotate(45deg) translate(-2px, -1px);
  background: #3f403b;
}

#menu-checkbox:checked ~ span:nth-last-child(3) {
  opacity: 0;
  transform: rotate(0deg) scale(0.2, 0.2);
}

#menu-checkbox:checked ~ span:nth-last-child(2) {
  opacity: 1;
  transform: rotate(-45deg) translate(0, -1px);
}

#menu {
  width: 60vw;
  height: 70vh;

  position: absolute;
  right: -100px;

  margin: -100px 0 0 0;
  padding: 50px;
  padding-top: 125px;

  background-color: #bfbfae;
  list-style: none;

  transform-origin: 0% 0%;
  transform: translate(100%, 0);
  transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1);
}

#menu li {
  padding: 10px 0;
  font-size: 22px;
}

#menu-checkbox:checked ~ ul {
  transform: none;
  opacity: 1;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 13px;
}

::-webkit-scrollbar-thumb {
  border-radius: 100px;
  border: 3px solid transparent;
  background-clip: content-box;
  background-color: rgb(88, 88, 88);
}

::-webkit-scrollbar-thumb:hover {
  background-color: rgb(109, 109, 109);
}

/* Media queries */
@media only screen and (min-width: 600px) {
}

@media only screen and (min-width: 769px) {
  #backdrop {
    width: 98%;
    height: 95.5%;
    border-radius: 1rem;
  }

  #menu-checkbox {
    display: none;
  }

  #menu-toggle span {
    display: none;
  }

  #menu {
    -webkit-transition: none !important;
    -moz-transition: none !important;
    -o-transition: none !important;
    transition: none !important;
    transform: none !important;

    padding: 0 1rem 0 1rem;

    position: absolute;
    top: 97vh;
    right: 38.2vw;

    border-radius: 1rem;

    height: fit-content;
    width: max-content;
  }

  #menu li {
    display: inline-block;
  }

  #home-link {
    display: flex;
    justify-content: center;
    position: static;

    padding-top: 0.5rem;
  }
}

@media only screen and (min-width: 769px) {
    #menu-toggle {
        right: unset;
        position: relative;
        top: -30px;
    }
}

@media only screen and (min-width: 769px){
    #menu {
        margin-left: auto;
        margin-right: auto;
        position: relative;
        right: unset;
    }
}
<!DOCTYPE html>
<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" />
    <title>OVERCASTING</title>
    <link rel="stylesheet" href="css/style.css" />
    <script src="script/js.js"></script>
    <link
      href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@200;300;400&display=swap"
      rel="stylesheet"
    />
    <link
      href="https://fonts.googleapis.com/css2?family=Space+Mono&display=swap"
      rel="stylesheet"
    />
  </head>
  <body>
    <main>
      <div id="backdrop">
        <header id="wrapper">
          <nav>
            <a href="index.html" id="home-link">OVERCASTING</a>
            <div id="menu-toggle">
              <input id="menu-checkbox" type="checkbox" />
              <span class="hamburger-span"></span>
              <span class="hamburger-span"></span>
              <span class="hamburger-span"></span>
              <ul id="menu">
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
                <li><a href="#">Link 4</a></li>
              </ul>
            </div>
          </nav>
        </header>
      </div>
    </main>
  </body>
</html>

How does this look?

I've included additional CSS at the end to customize the appearance further.

@media only screen and (min-width: 769px) {
    #menu-toggle {
        right: unset;
        position: relative;
        top: -30px;
    }
}

@media only screen and (min-width: 769px){
    #menu {
        margin-left: auto;
        margin-right: auto;
        position: relative;
        right: unset;
    }
}

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

Experiencing difficulties with loading Facebook wall feed JSON data

Struggling to integrate a Facebook wall feed using jQuery on my website's client side. Utilizing this URL for the Facebook request: Attempted approaches so far: 1. $.getJSON('http://www.facebook.com/feeds/page.php?format=json&id=407963083 ...

Why does Javascript execute in this specific order?

I'm puzzled as to why JavaScript behaves in a certain way, or if I made an error in my code. How is it that the code after $.getJSON runs before the callback completes? window.ratingCallback = function(data){ if(data[0].code == 3){ ratin ...

Switch Bootstrap Tab

I have successfully implemented a bootstrap tab on my webpage and it is functioning as intended. Now, I am interested in adding an additional feature to the tabs. My question is, is it possible to toggle the content area if the same tab is clicked again? ...

When Jquery JSON DOM fails to load data

I've been trying to implement this functionality on my website by following various tutorials, but I am facing an issue where the data is not displaying anywhere. Initially, it was showing the return value of json_encode, but now it's not even do ...

Ways to specify distinct css styles according to varying number of elements

I need the Button element to always take up 100% of the container, no matter how many elements are inside. Check out my Fiddle link here: Js Fiddle Link <div class="inputFieldBoolean buttonSeries" data-type="Boolean"><button class="true" data-va ...

Both of the radio buttons in Material-UI have been selected

I have a unique document that showcases an implementation of RadioGroup, FormControlLabel, and FormControl. Take a look at the code snippet below for reference. import React from 'react'; import PropTypes from 'prop-types'; import Radio ...

Receive information from the server and display it on the front-end

Hello! I'm currently in the process of developing a video uploader for my website. So far, I've successfully been able to upload videos from the front-end (built with React.js) to the back-end public folder (using Node.js) through the POST method ...

Here's how you can arrange a list starting with the first item and then searching for a specific string using md-autocomplete in

As a newcomer to angularJs, I am looking for ways to filter search results more efficiently. Check out this example here: https://codepen.io/anon/pen/mpJyKm I am trying to customize the search result by filtering based on query input. Specifically, I wan ...

steps for setting up an endless scroll masonry grid

I have incorporated masonry js to display content on my website. Now, I am interested in adding infinite scrolling functionality using this script. I came across a demo that showcases this feature. However, I am struggling to figure out how to implement ...

Animating the left and right positioning of a single element using CSS transitions

I am currently working with the following CSS code: #masthead { transition: left linear 0.25s; -moz-transition: left linear 0.25s; -o-transition: left linear 0.25s; -webkit-transition: left linear 0.25s; -ms-transition: left linear 0.2 ...

Loop through each item using the .each() method, then make an AJAX

I need the "done." text to show up only after all the tasks within my each() function have completed. Despite attempting to use a delay, it seems that they are running asynchronously and the "done." message displays too early. Additionally, I am sending ...

Encountering the React.Children.only error while trying to use the <Link> component in Next.js

I'm encountering an issue with the code below: Error: React.Children.only expected to receive a single React element child. While trying to troubleshoot, I noticed that it only allows me to have one header under the link. <div className="co ...

Issue arised while trying to open Bootstrap modal window due to conflict with surrounding elements

I'm facing a challenge with getting the bootstrap modal window to pop up on my website. Despite trying various solutions and troubleshooting steps, I haven't been able to resolve the issue. Even after eliminating all scripts except for the boots ...

Issue 1068: Attribute not found within angular 2 (Ahead of Time Compilation)

I am currently learning Angular 2 and trying to create a "User Register" form. However, I encountered an error stating "Property does not exist on type" during Phone number validation. I am using both JIT and AOT compilers. With the JIT compiler, my user ...

Is it better to specify one root element or render the entire layout in ReactDOM.render()?

Currently in the process of learning React.js and I have a question. Is it more beneficial to keep my ReactDOM.render() as it is currently set up: ReactDOM.render( <div className="container"> <div className="row"> <div className ...

Is it possible to perform ECDH Key Exchange using public keys of varying lengths?

Currently, I am working on implementing an ECDH key-exchange using the P-384 curve. While other curves could be used, I believe the implementations should be fairly similar in nature. The goal is for the Client (Javascript) and Server(Java) to establish a ...

Utilize a script to interact with CasperJS

When it comes to running my CasperJS script, I typically do so from the command line interface using this command: casperjs --ignore-ssl-errors=true --ssl-protocol=any scrape.js In order to fully automate the process, I am now looking into calling the sc ...

Determining the dimensions of form elements in an inline layout using Bootstrap 3.1

Currently in the process of upgrading my application from version 3.0 to 3.1. The application is still in its initial phase, resembling more of a prototype with only a few pages created. Therefore, I didn't anticipate encountering many issues during t ...

Avoid page refreshing when modifying app.js in React

Today is only my second day using React and I started by creating a React app with "npx create-react-app." However, when I tried to make changes to the app.js file, they didn't appear on the page even after refreshing the browser multiple times. (My n ...

What is the best way to create a clickable background for a modal window?

I am looking to add a chatbox feature to my website and have been utilizing Bootstrap Modal for this purpose. My goal is to keep the modal open even when the user clicks outside of it, while still allowing them to interact with the background of the websi ...