randomuser1
randomuser1

Reputation: 2803

Why are my menu items below the actual menu bar (CSS)?

I'm creating a very plain and simple menu in CSS. Currently I have the html code:

<div id="header">
    <ul id="navlist">
    <li id="active"><a href="#" id="current">Item one</a></li>
    <li><a href="#">Item two</a></li>
    <li><a href="#">Item three</a></li>
    <li><a href="#">Item four</a></li>
    <li><a href="#">Item five</a></li>
    </ul>
</div>

and my css:

#navlist li
{
display: inline;
list-style-type: none;
padding-right: 20px;
}

#header{
        position:fixed;
        height: 20px;
        display:block;
        width: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index:9;
        text-align:center;
        color: #f2f2f2;
        padding: 4px 0 0 0;
    }

    #header{
        top:0px;
    }

Here's the jsfiddle with my code, you can see that items are below actual dark bar - how can I change that? Thanks.

Upvotes: 2

Views: 139

Answers (3)

Alex Char
Alex Char

Reputation: 33238

Because ul element has default margin. You can remove it:

#navlist li {
  display: inline;
  list-style-type: none;
  padding-right: 20px;
}
#header {
  position: fixed;
  height: 20px;
  display: block;
  width: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9;
  text-align: center;
  color: #f2f2f2;
  padding: 4px 0 0 0;
}
#header {
  top: 0px;
}
#navlist {
  margin: 0;/*set margin to 0 to ul element*/
}
<div id="header">
  <ul id="navlist">
    <li id="active"><a href="#" id="current">Item one</a>
    </li>
    <li><a href="#">Item two</a>
    </li>
    <li><a href="#">Item three</a>
    </li>
    <li><a href="#">Item four</a>
    </li>
    <li><a href="#">Item five</a>
    </li>
  </ul>
</div>

References

typical default display properties for ul element

Upvotes: 5

Michael Giovanni Pumo
Michael Giovanni Pumo

Reputation: 14794

Because your ul tag (#navlist) has a margin on it by default, set by the browsers default stylesheet. Your bar also has a fixed height and this has the effect of pushing it down.

See a fix here: http://jsfiddle.net/6mk4kLj8/

I removed the margin from #navlist and removed the height from #header, to make it more flexible.

CSS

#navlist {

    margin: 0;

}

#navlist li {

    display: inline;
    list-style-type: none;
    padding-right: 20px;

}

#header {

    position:fixed;
    top: 0px;
    display:block;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index:9;
    text-align:center;
    color: #f2f2f2;
    padding: 4px 0 4px 0;

}

Upvotes: 0

Because of the margin of the ul:

#navlist {
    margin: 0;
}

Upvotes: 0

Related Questions