Max Fell
Max Fell

Reputation: 31

CSS menu moving contents on hover

I'm attempting to create a navigation menu that shows a border when an item is hovered over. While it currently looks fine, the other menu items are pushed out of the way when one item is hovered over. How would I go about making them stay put?

I've tried a few methods that were answered here but none of them have worked.
I've included a fiddle below.

nav {
  float: left;
  width: 100%;
}

ul {
  float: left;
  list-style:none;
  padding: 0;
  position: relative;
  left: 50%;
  text-align: center;
}

ul li {
  display: block;
  float: left;
  list-style: none;
  position: relative;
  right: 50%;
  margin: 0px 0px 0px 1px;
  padding: 5px 40px;
  color: white;
  line-height: 1.3em;
}

li a:hover {
  border: solid 1px black;
  padding: 5px 10px;
}

a {
  color: black;
  font-family: 'Quicksand', sans-serif;
  text-decoration: none;
}
<nav>
  <ul>
    <li><a href="#">HOME</a></li>
    <li><a href="#">ABOUT</a></li>
    <li><a href="#">MUSIC</a></li>
    <li><a href="#">STORE</a></li>
    <li><a href="#">LIVE</a></li>
    <li><a href="#">CONTACT</a></li>
  </ul>
</nav>

View on JSFiddle

Upvotes: 3

Views: 2269

Answers (2)

nish
nish

Reputation: 1221

Move the padding property from 'li a: hover' to 'a'.

a {
   color: black;
   font-family: 'Quicksand', sans-serif;
   text-decoration: none;
   padding: 5px 10px;
  }

Upvotes: 3

Nawed Khan
Nawed Khan

Reputation: 4401

Because you are adding padding on hover. Move the padding from hover to anchor.

li a:hover {
            border: solid 1px black;
}
a {
    color: black;
    font-family: 'Quicksand', sans-serif;
    text-decoration: none;
    border: solid 1px transparent;
    padding: 5px 10px;
}

Here is the fiddle.

Upvotes: 6

Related Questions