3

I am trying to prevent my menu from overlapping onto itself when the viewport gets small and the menu items get pushed to the next line. Seems like a simple problem but I have spent hours on this problem with no avail.

nav {
  position: absolute;
  width: 100%;
  text-align: center;
}
nav li {
  display: inline-block;
}
nav a {
  text-decoration: none;
  padding: 1em;
  margin: 1em;
  border: 2px solid black;
}
<body>
  <header>
    <nav>
      <ul>
        <li><a href="#">NavItem</a>
        </li>
        <li><a href="#">NavItem</a>
        </li>
        <li><a href="#">NavItem</a>
        </li>
        <li><a href="#">NavItem</a>
        </li>
        <li><a href="#">NavItem</a>
        </li>
      </ul>
    </nav>
  </header>
</body>

Now shrink your window and the items all overlap.

1 Answer 1

1

Change the nav a{} to nav li{}:

nav li {
  text-decoration: none;
  padding: 1em;
  margin: 1em;
  border: 2px solid black;
}

See the JSFiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

There’s already another rule for nav li, so in this case it’s better to merge them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.