5

I am creating a navbar using Bootstrap but the text is stationed on the left. I simply cannot figure out how to get the text to be in the center of the navbar and stay there when resizing and viewing the site on different devices. I have tried bypassing the float: left; with the below code but I still can't figure it out.

body {
    font-family: century gothic;
}

.nav {
    display: inline-block;
    float: none;
}

.li {
    text-align: center;
}

.footer {
    background-color: #263238;
    height: 5%;
}

.footer-text {
    color: white;
    font-size: 20px;
    text-align: center;
    margin-top: 12px;
}
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>My Website</title>
      <link href="css/bootstrap.min.css" rel="stylesheet">
      <link rel="stylesheet" type="text/css" href="styles.css">
   </head>
   <body>
      <nav class="navbar navbar-default">
      <div class="container-fluid-nav">
         <div>
            <ul class="nav navbar-nav">
               <li><a href="#">CSGO</a>
               </li>
               <li><a href="#">ARMA III</a>
               </li>
               <li><a href="#">PUBG</a>
               </li>
               <li><a href="#">Other</a>
               </li>
               <li><a href="#">About</a>
               </li>
               <li><a href="#">External Links</a>
               </li>
            </ul>
         </div>
      </div>
      <div class="footer navbar-fixed-bottom">
         <p class="footer-text">Copyright 2017 ©</p>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="script.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="js/bootstrap.min.js"></script>
   </body>
</html>

Pretty new to this stuff so sorry if it's staring me straight in the face.

Thanks.

Farhad
  • 4,119
  • 8
  • 43
  • 66

4 Answers4

5

This should help you out. In my opinion, you are using Bootstrap Navbar wrong. You are missing the main classes and hierarchy. Please go through the bootstrap navbar docs. https://getbootstrap.com/docs/3.3/components/#navbar

.container-fluid-nav div{
  display: flex;
justify-content: space-around;}

Add this to your css and it should work as required.

Aslam
  • 9,204
  • 4
  • 35
  • 51
2

add text-center class to <div class="container-fluid-nav text-center">

Sangsom
  • 437
  • 2
  • 6
  • 16
  • Thanks so much! One more thing though, is it possible to make the contents span the whole width of the navbar? –  Aug 15 '17 at 10:51
1

Using flexbox for the menu and to center on the page. Adding a bit of margin for the list elements.

body {
  font-family: century gothic;
}

.nav {
  display: flex;
  justify-content: center;
list-style: none;
}

li {
  text-align: center;
}

li:not(:last-child) {
margin-right: 1em;
}

.footer {
  background-color: #263238;
  height: 5%;
}

.footer-text {
  color: white;
  font-size: 20px;
  text-align: center;
  margin-top: 12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<nav class="navbar navbar-default">
  <div class="container-fluid-nav">
    <div>
      <ul class="nav navbar-nav">
        <li><a href="#">CSGO</a>
        </li>
        <li><a href="#">ARMA III</a>
        </li>
        <li><a href="#">PUBG</a>
        </li>
        <li><a href="#">Other</a>
        </li>
        <li><a href="#">About</a>
        </li>
        <li><a href="#">External Links</a>
        </li>
      </ul>
    </div>
  </div>
  <div class="footer navbar-fixed-bottom">
    <p class="footer-text">Copyright 2017 ©</p>
  </div>
</nav>
Gerard
  • 15,418
  • 5
  • 30
  • 52
0

You are not closing tag <nav> and your are targeting a .li which you don't have, that's why it's not centered.

You can do like this:

    body {
    font-family: century gothic;
}

ul li {
  text-align: center;
  list-style: none;
}

.footer {
    background-color: #263238;
    height: 5%;
}

.footer-text {
    color: white;
    font-size: 20px;
    text-align: center;
    margin-top: 12px;
}
<body>
      <nav class="navbar navbar-default">
      <div class="container-fluid-nav text-center">
            <ul class="nav navbar-nav">
               <li><a href="#">CSGO</a>
               </li>
               <li><a href="#">ARMA III</a>
               </li>
               <li><a href="#">PUBG</a>
               </li>
               <li><a href="#">Other</a>
               </li>
               <li><a href="#">About</a>
               </li>
               <li><a href="#">External Links</a>
               </li>
            </ul>
      </div>
      </nav>
      <div class="footer navbar-fixed-bottom">
         <p class="footer-text">Copyright 2017 ©</p>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="script.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="js/bootstrap.min.js"></script>
   </body>
Farhad
  • 4,119
  • 8
  • 43
  • 66
RasmusGlenvig
  • 765
  • 6
  • 18