skip to Main Content

(Jsfiddle for context: https://jsfiddle.net/hfwtq2c9/12/)

I’ve made a navbar and some links are in a dropdown menu for compactness. When the screen gets too small, it collapses all the links into a menu which can be accessed through a button. I want to make it so when it goes into the menu, all the links come out of the dropdowns and go into one list. I also have a problem where the links in my drop-down have some random whitespace on the left, and I can’t figure out why it is happening. Can this be done? Thanks

My code:

const primaryNav = document.querySelector(".primary-navigation");
const navToggle = document.querySelector(".mobile-nav-toggle");

navToggle.addEventListener("click", () => {
  const visibility = primaryNav.getAttribute("data-visible");

  if (visibility === "false") {
    primaryNav.setAttribute("data-visible", true);
    navToggle.setAttribute("aria-expanded", true);
  } else if (visibility === "true") {
    primaryNav.setAttribute("data-visible", false);
    navToggle.setAttribute("aria-expanded", false);
  }
});
@import url("https://fonts.googleapis.com/css2?family=Poppins&display=swap");

* {
  font-family: "Poppins", sans-serif;
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  background: radial-gradient(circle,
      rgba(0, 102, 215, 1) 0%,
      rgba(15, 0, 157, 1) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#0066d7", endColorstr="#0f009d", GradientType=1);
}

a {
  text-decoration: none;
}

.sr-only {
  display: none;
}

/* primary nav  */

.flex {
  display: flex;
  gap: 2rem;
}

.primary-header {
  align-items: center;
  justify-content: space-between;
  background: rgba(0, 0, 0, 0.4);
}

.primary-navigation {
  list-style: none;
  padding: 2rem;
}

.primary-navigation a {
  color: #ffffff;
  text-decoration: none;
  font-size: 1.5rem;
  transition: all 300ms ease-in-out;
  list-style: none;
}

.primary-navigation a:hover {
  color: #929292;
}

.mobile-nav-toggle {
  display: none;
}

@media (max-width: 50rem) {
  .primary-navigation {
    gap: 2rem;
    position: fixed;
    z-index: 1000;
    inset: 0 0 0 0;
    padding: 10rem 3rem;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 300ms ease-in-out;
    margin: 0;
    backdrop-filter: blur(1rem);
    background: rgba(0, 0, 0, 0.4);
  }

  .primary-navigation a {
    font-size: 1.75rem;
  }

  .primary-navigation[data-visible="true"] {
    transform: translateX(0%);
  }

  .primary-header .mobile-nav-toggle {
    display: block;
    position: absolute;
    z-index: 9999;
    background-image: url("./assets/hamburger.svg");
    background-color: hotpink;
    background-size: cover;
    background-repeat: no-repeat;
    width: 2.5rem;
    border: 0;
    aspect-ratio: 1;
    right: 2rem;
  }

  .primary-header .mobile-nav-toggle[aria-expanded="true"] {
    background-image: url(./assets/close.svg);
    position: fixed;
  }

  .primary-header {
    padding: 4rem;
  }
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: rgba(0, 0, 0, 0.5);
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  list-style: none;
}

.dropdown-content a {
  color: #ffffff;
  padding: 1rem;
  display: flex;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: flex;
  flex-direction: column;
}
    <header class="primary-header flex">
      <button class="mobile-nav-toggle" aria-controls="primary-navigation" aria-expanded="false">
        <span class="sr-only">Menu</span>
      </button>
      <nav>
        <ul data-visible="false" class="primary-navigation flex">
          <li><a href="index.html">Home</a></li>

          <li class="dropdown">
            <a href="about.html" class="dropbtn">About Me</a>
            <ul class="dropdown-content">
              <li><a href="projects.html">My previous Projects</a></li>
              <li><a href="quals.html">My Qualifications</a></li>
              <li><a href="cv.html">My CV/Resume</a></li>
              <li><a href="services.html">My Services</a></li>
            </ul>
          </li>
          <li class="dropdown">
            <a href="contact.html" class="dropbtn">Contact Me</a>
            <ul class="dropdown-content">
              <li><a href="social.html">Social Media</a></li>
            </ul>
          </li>
        </ul>
      </nav>
    </header>

2

Answers


  1. In order to rid off whitespace you can add padding-left: 0 to the .dropdown-content class.

    .dropdown-content {
     // ...
     padding-left: 0;
    }
    
    Login or Signup to reply.
  2. If I’m understanding your question correctly, you may need to make some adjustments to the style of your .dropdown-content. Here’s a solution that could work for you.

    As for the random whitespace on the left of your dropdown links is caused by padding.

    .dropdown-content {
      display: none;
      position: absolute;
    
      padding-left: 0; // This will remove the white space on left
    
      background-color: rgba(0, 0, 0, 0.5);
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
      z-index: 1;
      list-style: none;
    }
    
    @media (max-width: 50rem) {
    // This keeps your dropdown visible in a small screen
      .dropdown-content {
        display: block;
        position: static;
        padding-left: 10px; // This makes it easier to identify which children in the dropdown are linked to the parent.
      }
    }
    

    Hope this helps 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search