Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there space above this navigation bar

Tags:

html

css

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>
like image 235
Gewoo Avatar asked Dec 14 '15 17:12

Gewoo


People also ask

How do I get rid of the extra space in my navigation bar?

The gap you see, is because of the top-margin of the h1 in the header. So you can set the top margin of that h1 to 0 to remove the gap.

How do I fix navigation To top of page?

Use position absolute and set the top value to the number of pixels you want the Nav bar to be from the top of the browser.


2 Answers

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?

like image 133
web-tiki Avatar answered Oct 07 '22 02:10

web-tiki


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

like image 44
APAD1 Avatar answered Oct 07 '22 02:10

APAD1