I need help to align my navbar bar to center Here is my code What's wrong with it? It does not align the menu to the center.
#nav {
position: absolute;
left: 0px;
height: 40px;
background-color: #2C64B4;
width: 100%;
margin: 0 auto;
}
#nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
#nav ul li {
margin: 0;
padding: 0;
float: left;
}
#nav ul li a {
text-decoration: none;
padding: 10px 20px;
display: block;
color: #FFF;
text-align: center;
}
<div id="nav">
<ul>
<li><a href="#">Home</a>
</li>
<li><a href="#">About Us</a>
</li>
<li><a href="#">Blah</a>
</li>
<li><a href="#">exampl</a>
</li>
</ul>
</div>
One way would be to set the display
of #nav ul
to inline-block
. Then add text-align: center
to the parent element in order to center the child ul
:
#nav {
position: absolute;
left: 0px;
height: 40px;
background-color: #2C64B4;
width: 100%;
text-align: center;
}
#nav ul {
margin: 0;
padding: 0;
display: inline-block;
}
Alternatively, you could also set the display
of the parent element, #nav
to flex
, and then add justify-content: center
to center the child element horizontally.
#nav {
position: absolute;
left: 0px;
height: 40px;
background-color: #2C64B4;
width: 100%;
display: flex;
justify-content: center;
}