'How to automatically adjust ul position based on number of li's in css?

I have a set of "Unordered lists" and the display will show 6 of them when the user is logged into the site. When the user logs out only 3 of them will show up. I want to align the ULs to center and auto adjust based on number of LIs. My css is as below:

@media (min-width: 64em)
.usa-header--basic .usa-nav {
    flex-direction: row;
    align-items: center;
    justify-content: flex-end;
    display: flex;
    padding: 0 0 0.25rem 0.5rem;
    width: 100%;
}
@media (min-width: 64em)
.usa-nav {
    float: right;
    position: relative;
}    
ul.usa-nav__primary.usa-accordion {
        width: 100%;
        padding-top: 6rem; //intentional padding
        position: initial
    }

This CSS works as below when the user is logged in:

enter image description here

However, when the user logs out only 3 menu item will show up. So with the same above css, the screen looks like below:

enter image description here

How to fix this so the menu items align automatically to the center?

HTML for the menu portion:

<nav class="usa-nav" role="navigation">
   <ul class="usa-nav__primary usa-accordion" role="navigation">
      <li class="usa-nav__primary-item">
         <a href="/" class="usa-nav__link">
         <span>Menu1</span>
         </a>
      </li>
      <li class="usa-nav__primary-item">
         <button class="usa-accordion__button usa-nav__link " aria-expanded="false" aria-controls="basic-nav-section-2">
         <span>Menu2</span>
         </button>
         <ul id="basic-nav-section-2" class="usa-nav__submenu" hidden="">
            <li class="usa-nav__submenu-item">
               <a href="/#">
               <span>Submenu1</span>
               </a>
            </li>
            <li class="usa-nav__submenu-item">
               <a href="/#">
               <span>Submenu2</span>
               </a>
            </li>
         </ul>
      </li>
      <li class="usa-nav__primary-item">
         <button class="usa-accordion__button usa-nav__link " aria-expanded="false" aria-controls="basic-nav-section-3">
         <span>Menu3</span>
         </button>
      </li>
      <li class="usa-nav__primary-item">
         <a href="/#" class="usa-nav__link">
         <span>Menu4</span>
         </a>
      </li>
      <li class="usa-nav__primary-item">
         <a href="/#" class="usa-nav__link">
         <span>Menu5</span>
         </a>
      </li>
      <li class="usa-nav__primary-item">
         <a href="/#" class="usa-nav__link">
         <span>Menu6</span>
         </a>
      </li>
   </ul>
</nav>


Solution 1:[1]

Here is a potential solution for you. :) Just use flex on the list itself.

display: flex;
justify-content: center;

And then simply give some margin to li elements.

https://codepen.io/Juka99/pen/QWaXZER

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Just Alex