Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Menu moves other objects on hover.

When I hover "sites" link in the meny it moves everything to the side. How to prevent it?

#nav li {
  list-style: none;
  margin-left: 1em;
  float: left;  
}

/* Hidden menu */
#nav ul { 
  overflow: hidden
  position: absolute; */
  display: none;
  padding:0;

}

#nav ul li {    
  float: none;
  margin: 0;
  padding: 0;
}

#nav a {
  padding: 5px;
  display: block;
  text-decoration: none;
}

#nav a:hover {
    background: #f8f8f8;
}

#nav li:hover > ul {
    display: block; /* show dropdown on hover */
}
    <ul id="nav">
      <li><a href="#">Home</a></li>
          <li><a href="#">Sites
            </a>
            <ul>
              <li><a href="http://webdesignerwall.com">Web Designer Wall</a></li>
              <li><a href="http://themify.me">Themify</a></li>
              <li><a href="http://icondock.com">IconDock</a></li>
              <li><a href="http://ndesign-studio.com">N.Design Studio</a></li>
              <li><a href="http://bestwebgallery.com">Best Web Gallery</a></li>
            </ul>
          </li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
          <li><a href="#">Another Button</a></li>
    </ul>

2 Answers

Liam Maclachlan
Liam Maclachlan
22,805 Points

Don't know if this will help but I have edited the above CSS a bit. Let me know :)

#nav li {
  list-style: none;
  margin-left: 1em;
  float: left;  
}

/* Hidden menu */
#nav ul { 
  overflow: hidden;
  position: absolute; */
  display: none;
  padding:0;

}

#nav ul li {    
  float: none;
  margin: 0;
  padding: 0;
}

#nav li > ul {
  display: none;
}

#nav a {
  padding: 5px;
  display: block;
  text-decoration: none;
}

#nav a:hover {
    background: #f8f8f8;
}

#nav li:hover > ul {
    display: block; /* show dropdown on hover */
}

Oh! and try this and see if it makes a difference

* {
box-sizing: border-box;
}

It will stop minus any padding to the total width of the element, rather than adding it to the total width. That piece of code should be at the top of every project :)

I've put your code in a CodePen and it looks to be behaving as required:

http://codepen.io/jmbarlow/pen/yyxpaw

(Added a semi-colon, and removed the comment closing tag in #nav ul )