Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 )