Jelle Botman Jelle Botman - 12 days ago 8
CSS Question

How can i center this menu?

I have this code for my menu
I got some list items i wanna center in the black box.
The 4 Li-items are floating left to get them inside the black box



nav {
display: block;
width: 900px;
overflow: hidden;
}
nav ul {
width: 900px;
padding: .7em;
float: left;
margin: 0px;
list-style: none;
background: #444;
}
nav li {
float: left;
}
nav a {
float: left;
padding: .8em 1.5em;
text-decoration: none;
color: #555;
text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
font: bold 1.1em/1'trebuchet MS', Arial, Helvetica;
letter-spacing: 1px;
text-transform: uppercase;
border-width: 1px;
border-style: solid;
border-color: #fff #ccc #999 #eee;
background: #c1c1c1;
background: linear-gradient(#f5f5f5, #c1c1c1);
}
nav a:hover,
nav a:focus {
outline: 0;
color: #fff;
text-shadow: 0 1px 0 rgba(0, 0, 0, .2);
background: #fac754;
background: linear-gradient(#fac754, #f8ac00);
}
nav a:active {
box-shadow: 0 0 2px 2px rgba(0, 0, 0, .3) inset;
}
nav li:first-child a {
border-left: 0;
border-radius: 4px 0 0 4px;
}

<nav>
<ul>
<div>
<li><a href='storingsoverzicht.php'>Storingen</a>
</li>
<li><a href='Contacten.php'>Contacten</a>
</li>
<li><a href='nieuwestoring.php'>Nieuwe Storing</a>
</li>
<li><a href='uitloggen.php'>Uitloggen</a>
</li>
</div>
</ul>
</nav>





I like to have the Li-items centered in the middle of the black box.
How do i do this?

Answer

Remove float from ul and li and add display: inline-block; and for parent nav add text-align: center; I just corrected css, have a look:

nav {
  display: block;
  width: 900px;
  overflow: hidden;
  text-align: center;
}
nav ul {
  width: 900px;
  padding: .7em;
  margin: 0px;
  list-style: none;
  background: #444;
  font-size: 0;
}
nav li {
  float: none;
  display: inline-block;
  font-size: 18px;
}
nav a {
  float: left;
  padding: .8em 1.5em;
  text-decoration: none;
  color: #555;
  text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
  font: bold 1.1em/1'trebuchet MS', Arial, Helvetica;
  letter-spacing: 1px;
  text-transform: uppercase;
  border-width: 1px;
  border-style: solid;
  border-color: #fff #ccc #999 #eee;
  background: #c1c1c1;
  background: linear-gradient(#f5f5f5, #c1c1c1);
}
nav a:hover,
nav a:focus {
  outline: 0;
  color: #fff;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .2);
  background: #fac754;
  background: linear-gradient(#fac754, #f8ac00);
}
nav a:active {
  box-shadow: 0 0 2px 2px rgba(0, 0, 0, .3) inset;
}
nav li:first-child a {
  border-left: 0;
  border-radius: 4px 0 0 4px;
}
<nav>
  <ul>
      <li><a href='storingsoverzicht.php'>Storingen</a>
      </li>
      <li><a href='Contacten.php'>Contacten</a>
      </li>
      <li><a href='nieuwestoring.php'>Nieuwe Storing</a>
      </li>
      <li><a href='uitloggen.php'>Uitloggen</a>
      </li>
  </ul>
</nav>