<ul> in Bootstrap can't be centered when justify-content-center is applied to <ul> when <ul> is within a row

Viewed 85

Here is my code (only body)

  <body>
    
    <div class="container-fluid">

      <div class="row">

          <ul class="nav justify-content-center nav-tabs"> 
            <li class="nav-item"> 
              <a class="nav-link" href="/home.php">Home</a> 
            </li> 
            <li class="nav-item"> 
              <a class="nav-link" href="/login.php">Log In</a> 
            </li> 
            <li class="nav-item"> 
              <a class="nav-link" href="/logout.php">Log Out</a> 
            </li> 
          </ul>
     

      </div>
    </div>


  </body>

Display :

enter image description here

I can however , center ul when i place ul within a column. Is there a specific reason why my above code could not center ul

EDIT :

If i remove row from the code, I could use justify-content-center on ul.My question however still stands as I could not explain the behavior when row is present

<body>
    
    <div class="container-fluid">

  
 
          <ul class="nav justify-content-center nav-tabs"> 
            <li class="nav-item"> 
              <a class="nav-link" href="/home.php">Home</a> 
            </li> 
            <li class="nav-item"> 
              <a class="nav-link" href="/login.php">Log In</a> 
            </li> 
            <li class="nav-item"> 
              <a class="nav-link" href="/logout.php">Log Out</a> 
            </li> 
          </ul>
     

    </div>

</body>

enter image description here

3 Answers

Try this instead,

First Method

please add flex-basis:100%; to for align <ul> center.

ul{
  flex-basis:100%;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<body>
    <div class="container-fluid">
      <div class="row">

        <ul class="nav nav-tabs justify-content-center"> 
          <li class="nav-item"> 
            <a class="nav-link" href="/home.php">Home</a> 
          </li> 
          <li class="nav-item"> 
            <a class="nav-link" href="/login.php">Log In</a> 
          </li> 
          <li class="nav-item"> 
            <a class="nav-link" href="/logout.php">Log Out</a> 
          </li> 
        </ul>

      </div>
    </div>
  </body>

Second Method

please add justify-content-center to row class to align <ul> center instead of adding to <ul> tag.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<body>
    <div class="container-fluid">
      <div class="row justify-content-center">

        <ul class="nav nav-tabs"> 
          <li class="nav-item"> 
            <a class="nav-link" href="/home.php">Home</a> 
          </li> 
          <li class="nav-item"> 
            <a class="nav-link" href="/login.php">Log In</a> 
          </li> 
          <li class="nav-item"> 
            <a class="nav-link" href="/logout.php">Log Out</a> 
          </li> 
        </ul>

      </div>
    </div>
  </body>

The ul itself is not centered, so using .justify-content-center will center its child li elements only inside the ul, while it itself remains on the left. Add .justify-content-center to the ul's parent element (the row div) and it should work.

try this,

<div class="row">

    <ul class="nav justify-content-center nav-tabs" style="display: flex; justify-content: center;">
        <li class="nav-item">
            <a class="nav-link" href="/home.php">Home</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/login.php">Log In</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/logout.php">Log Out</a>
        </li>
    </ul>

</div>
Related