How to center-align a menu made in Nav HTML+CSS

Question:

I try to align in all ways with text-align: center; but none works and when I space with left the sub-menus are out of line with the menu!

* {
  margin: 0;
  padding: 0;
}

.menu {
  width: 100%;
  height: 60px;
  background-color: black;
  font-family: Roboto;
  box-shadow: 0px 0px 100px 20px;
}

.menu ul {
  list-style: none;
  position: relative;
}

.menu ul li {
  width: 150px;
  padding: 0px 10px;
  float: left;
  line-height: 30px;
}

.menu ul li a {
  text-align: center;
  font-size: 19px;
}

.menu a {
  padding: 15px;
  display: block;
  text-align: center;
  text-decoration: none;
  color: #4d4d4d;
}

.menu ul ul {
  background-color: black;
  position: absolute;
  visibility: hidden;
}

.menu ul ul li {
  float: none;
  border-bottom: solid 1px #262626;
}

.menu ul li:hover ul {
  visibility: visible;
}

.menu a:hover {
  transition: all 0.4s ease;
  color: white;
  text-shadow: 3px 3px 20px;
}

.menu ul ul li {
  background-color: #1a1a1a;
}

.menu ul ul a {
  font-size: 17px;
}

.menu ul ul li a:hover ul ul ul {
  visibility: visible;
}
<body>
  <nav class="menu">
    <ul>
      <li><a href="#">5</a></li>
      <li><a href="#">4</a>
        <ul>
          <li><a href="#">1</a></li>
        </ul>
      </li>
      <li><a href="#">3</a>
        <ul>
          <li><a href="#">3</a></li>
          <li><a href="#">2</a></li>
          <li><a href="#">1</a></li>
        </ul>
      </li>
      <li><a href="#">2</a>
        <ul>
          <li><a href="#">5</a></li>
          <li><a href="#">4</a></li>
          <li><a href="#">3</a></li>
          <li><a href="#">2</a></li>
          <li><a href="#">1</a></li>
        </ul>
      </li>
      <li><a href="#">1</a>
        <ul>
          <li><a href="#">5</a></li>
          <li><a href="#">4</a></li>
          <li><a href="#">3</a></li>
          <li><a href="#">2</a></li>
          <li><a href="#">1</a></li>
        </ul>
      </li>
    </ul>
  </nav>
</body>

Answer:

Remove the default padding from <ul> and <li> . Add padding: 0; property padding: 0; in the .menu ul and .menu ul li classes. Do not use float: left because it prevents you from centering menu items by aligning them to the left. Instead, put display: inline-block; nas li and text-align: center; in the first ul of the menu.

Putting text-align: center; in ul pai, you don't need to put it on the children because they already inherit the property, avoiding repetition. In the CSS below I added comments of what was removed and what was added:

.menu ul{
text-align: center; /* adicionado */
list-style: none;
position: relative;
padding: 0; /* adicionado */
}

.menu ul li ul{
width: 100%; /* adicionado */
}

.menu ul li{
display:  inline-block; /* adicionado */
width: 150px;
padding: 0px 10px;
/*float: left;*/
line-height: 30px;
padding: 0; /* adicionado */
position: relative; /* adicionado */
}

.menu ul li a{
text-align: center;
font-size: 19px;
}

.menu a{
padding: 15px;
display: block;
/*text-align: center;*/
text-decoration: none;
color: #4d4d4d;
}

.menu ul ul{
background-color: black;
position: absolute;
visibility: hidden;
}

.menu ul ul li{
xxfloat: none;
display: block;
width: 100%;
border-bottom: solid 1px #262626;
}

.menu ul li:hover ul{
visibility: visible;
}

.menu a:hover{
transition: all 0.4s ease;
color: white;
text-shadow: 3px 3px 20px;
}

.menu ul ul li{
background-color: #1a1a1a;
}

.menu ul ul a{
font-size: 17px;
}

.menu ul ul li a:hover ul ul ul{
visibility: visible;
}
<nav class="menu">
        <ul>
            <li><a href="#">5</a></li>
            <li><a href="#">4</a>
                <ul>
                    <li><a href="#">1</a></li>
                </ul>
            </li>
            <li><a href="#">3</a>
                <ul>
                    <li><a href="#">3</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">1</a></li>
                </ul>
            </li>
            <li><a href="#">2</a>
                <ul>
                    <li><a href="#">5</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">1</a></li>
                </ul>
            </li>
            <li><a href="#">1</a>
                <ul>
                    <li><a href="#">5</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">1</a></li>
                </ul>
            </li>
        </ul>
    </nav>
Scroll to Top