joshbigguns joshbigguns - 2 months ago 6
CSS Question

How do horizontally center a fixed navigation bar?

I have looked around and cannot find out how to horizontally center my fixed navigation bar.

If you can could you show me how to and explain why.

Thanks.

HTML Code:

<ul id="nav">
<li>stack</li>
<li>over</li>
<li>flow</li>
</ul>


CSS Code:

#nav{
list-style: none;
position: fixed;
margin-top: -10px;
width: 100%;
font-family: Product Sans;
font-size: 0;
}
#nav li{
width: 200px;
height: 50px;
display: inline-block;
text-align: center;
padding-top: 5px;
background-color: white;
font-size: 30px;
}

Answer

You have to center align the li items inside the ul

so the CSS will be

<style>

#nav{
  list-style: none;
  position: fixed;
  margin-top: -10px;
  width: 100%;
  font-family: Product Sans;
  font-size: 0;
  text-align:center;
  padding-left:0px;
}
#nav li{
  width: 200px;
  height: 50px;
  display: inline-block;
  text-align: center;
  padding-top: 5px;
  background-color: white;
  font-size: 30px;
}


</style>

And keep the html without any change