1

I am currently using Bootstrap and it is giving me quite the headache. I'm having a lot of trouble understanding it but feel determined to use it in order to be able to make my website responsive and therefore support mobile devices.

I am trying to centre my navbar content but am having the hardest time doing so. Everything I have tried it never moves and always stays attached to the right with a margin on either side. Could someone please point me in the right direction to fixing this?

* {
 box-sizing: border-box;
}

body {
 background-color: #393939
}



/*

-=-=- NAVIGATION BAR -=-=-

*/
#header-nav {
 background-color: #262626;
 font-family: 'Kanit', sans-serif;
 font-size: 1.1em;
 text-transform: uppercase;
}

.navlink {
 display: inline-block;
 color: #C9C9C9;
 padding: 10px;
 padding-bottom: 10px;
 background:
  linear-gradient(white, white)
  bottom
  /100% 0px
  no-repeat;
 transition: 0.2s all;
}

.navlink:hover {
 color: #FFF;
 text-decoration: none;
 background-size: 100% 4px;
}

.active {
 color: #FFF;
}



/*

-=-=- SOCIAL MEDIA -=-=-

*/

.social-icon {
 float: right;
 font-size: 1.5em;
 padding: 0;
}
<!DOCTYPE html>
<html>

 <head>

  <!-- Meta & Other -->
  <title>Infamous | Home</title>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="description" content="Infamous official website">
  <meta name="keywords" content ="Infamous, Minecraft, Server, Game, Gaming">
  <meta name="author" content="MrWardy">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <!-- CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <link rel="stylesheet" href="./Stylesheets/default.css">
  
  <!-- Fonts -->
  <script src="https://kit.fontawesome.com/35fad75205.js" crossorigin="anonymous"></script>
  <link href="https://fonts.googleapis.com/css2?family=Kanit&display=swap" rel="stylesheet">
 
 

 </head>



 <body>

  <header>
   <nav id="header-nav" class="navbar-nav">
    <div class="container">
     <a class="active navlink" href="#"><i class="fas fa-home"></i> Home</a>
     <a class="navlink" href="#rules"><i class="fas fa-book"></i> Rules</a>
     <a class="navlink" href="#vote"><i class="fas fa-vote-yea"></i> Vote</a>
     <a class="navlink" href="#store"><i class="fas fa-tags"></i> Store</a>
<!--     <a class="social-icon navbar-text" href="#discord"><i class="fab fa-discord"></i></a>-->
    </div>
   </nav>
  </header>

  <!-- JavsScript -->
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

 </body>

</html>
MrWardy
  • 41
  • 4

3 Answers3

0

The elements with .navlink class are inline elements which means they respond to the text-align: left|center|right css property on the parent container.

Bootstrap have a class to apply the css property text-align: center, it's text-center, apply this class to the container element and the nav items will center align*.

<div class="container text-center">
  <a class="active navlink" href="#"><i class="fas fa-home"></i> Home</a>
  <a class="navlink" href="#rules"><i class="fas fa-book"></i> Rules</a>
  <a class="navlink" href="#vote"><i class="fas fa-vote-yea"></i> Vote</a>
  <a class="navlink" href="#store"><i class="fas fa-tags"></i> Store</a>
</div>
cam
  • 3,179
  • 1
  • 12
  • 15
0

Since you are using bootstrap 4, you can simple use bootstrap 4's flex-box utility classes to center your navbar links, in your case you just have to add these classes to parent container of your links which is div with class container.

<nav id="header-nav" class="navbar-nav">
                <div class="container d-flex justify-content-center">
                    <a class="active navlink" href="#"><i class="fas fa-home"></i> Home</a>
                    <a class="navlink" href="#rules"><i class="fas fa-book"></i> Rules</a>
                    <a class="navlink" href="#vote"><i class="fas fa-vote-yea"></i> Vote</a>
                    <a class="navlink" href="#store"><i class="fas fa-tags"></i> Store</a>
<!--                    <a class="social-icon navbar-text" href="#discord"><i class="fab fa-discord"></i></a>-->
                </div>
            </nav>

in above code the d-flex class makes the div as a flex-box and justify-content-center aligns its content to center.

Hope it helps!

Gagandeep Singh
  • 975
  • 6
  • 11
0

As your navlink elements have display: inline-block by default, you can add a text-center class to the container div and those elements will be centered.

<div class="container text-center">
  ...
</div>
guizo
  • 2,594
  • 19
  • 26