6

I want to make a fixed navigation bar. But now there is space above the menu. I tried anything I know to fix this but it did not work. I searched the whole code for something that creates space but I could not find it.

#nav {
  position: fixed;
  text-align: center;
  font-size: 22px;
  background-color: #222222;
  margin: 0 auto;
  width: 100%;
}
#nav ul li a {
  color: #ccc;
  display: block;
  padding: 10px;
  text-decoration: none;
}
#nav ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  position: relative;
}
#nav ul li {
  display: inline-block;
}
#nav ul li:hover {
  background: #333333;
}
#nav ul li a:hover {
  text-shadow: 3px 2px 3px #333333;
  text-decoration: none;
  position: relative;
  bottom: 5px;
  color: #fdde00;
}
html,
body {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}
<div id="nav">
  <ul>
    <li><a href="#">Test</a></li>
    <li><a href="#">Test</a></li>
    <li><a href="#">Test</a></li>
    <li><a href="#">Test</a></li>
  </ul>
</div>

<div>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. orem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. orem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam</p>
</div>
web-tiki
  • 99,765
  • 32
  • 217
  • 249
Gewoo
  • 437
  • 6
  • 19

3 Answers3

8

This behaviour is called collapsing margins. The gap at the top is created because the <p> element has a default margin.

3 workarounds :

  • remove the default margin on the p element with p{margin: 0;}
  • add top:0; to the .nav element
  • add overflow:hidden; to the second div

For more about how to prevent collapsing margins, see How to disable margin-collapsing?

Community
  • 1
  • 1
web-tiki
  • 99,765
  • 32
  • 217
  • 249
5

You can add top:0; to the fixed nav element to resolve this:

#nav {
    position: fixed;
    text-align: center;
    font-size: 22px;
    background-color: #222222;
    margin: 0 auto;
    width: 100%;
    top:0;
}

JSFiddle

APAD1
  • 13,509
  • 8
  • 43
  • 72
  • Any idea why this happens? =/ – godfrzero Dec 14 '15 at 17:50
  • 1
    It's because the fixed element has an attribute of `top:auto` which is not the same as having `top:0` – BuddhistBeast Dec 14 '15 at 17:52
  • @BuddhistBeast nvm, it's a stupid margin collapse issue. sigh. – godfrzero Dec 14 '15 at 17:55
  • 1
    As @web-tiki explains below, the white space you are seeing is due to the margin that is being applied to the `p` tag. By adding `top:0;` to the nav element, you are basically telling it to ignore the margin from the `p` and fix itself to the very top of the window. – APAD1 Dec 14 '15 at 17:55
0

Change this in your css:

#nav {
    position: fixed;
    text-align: center;
    font-size: 22px;
    background-color: #222222;
    margin: 0 auto;
    width: 100%;
    top:0;
}

#content{
    margin-top:50px;
}

And this in you html:

.....
</div>
<div id="content">
    <p>
    .....
Anish Silwal
  • 980
  • 1
  • 10
  • 24