user6731422 user6731422 - 1 month ago 14
CSS Question

Trying to create a horizontal centered menu

I am trying to create a horizontal centered menu but it appears vertically centered after adding bootsrap css codes.

this is the css code of my application



@media (max-width: @screen-xs) {
body {
font-size: 10px;
}
}
@media (max-width: @screen-sm) {
body {
font-size: 14px;
}
}
h2 {
font-size: 300%;
margin-bottom: 0px;
clear: both;
margin-left: 7px;
}
h5 {
margin-top: 0px;
padding: 0px;
margin-left: 15px;
color: #fff;
margin-bottom: 1px;
clear: both;
}
hr {
margin: 0px;
}
.container {
width: auto;
margin-left: 200px;
margin-right: 200px;
height: 500px;
max-height: 500px !important;
padding-left: 0px;
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<div style="background-color:#191919;" class="container">
<h2 style="color: #32CD32; font-family: Copperplate; align:left;">
Header
</h2>
<h5>... caption</h5>
<hr style="width:101.6%;">


<div class="col-lg-12">
<nav id="main_menu">
<div align="center" class="menu_wrap">
<ul class="nav sf-menu">
<li class="sub-menu"><a href="#"><small>Mission</small> </a>
</li>
<li class="sub-menu"><a href="#"><small>About Us</small></a>
</li>
<li class="sub-menu"><a href="#"><small>Grants</small></a>
</li>
<li class="sub-menu"><a href="#"><small>News</small></a>
</li>

</ul>
</div>
</nav>
</div>

</div>





Please how can I horizontally align the menus to the center

Answer

Try this css, li is taking display:block if you want to set all li in a line you need to set display: inline-block;

@media (max-width: @screen-xs) {
  body {
    font-size: 10px;
  }
}
@media (max-width: @screen-sm) {
  body {
    font-size: 14px;
  }
}
h2 {
  font-size: 300%;
  margin-bottom: 0px;
  clear: both;
  margin-left: 7px;
}
h5 {
  margin-top: 0px;
  padding: 0px;
  margin-left: 15px;
  color: #fff;
  margin-bottom: 1px;
  clear: both;
}
hr {
  margin: 0px;
}
.container {
  width: auto;
  margin-left: 200px;
  margin-right: 200px;
  height: 500px;
  max-height: 500px !important;
  padding-left: 0px;
}
.nav>li {
    position: relative;
    display: inline-block!important;
 }
.nav>li>a:focus, .nav>li>a:hover {
    text-decoration: none;
    background-color:transparent!important;
  color:red;
}
.nav>li>a {
    padding: 10px 8px!important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>

<body style="background-color:#1f5060;">
  <div style="background-color:#191919;" class="container">
    <h2 style="color: #32CD32; font-family: Copperplate; align:left;">
         Header
      </h2>
    <h5>... caption</h5> 
    <hr style="width:101.6%;">

    <div class="col-lg-12">
      <nav id="main_menu">
        <div align="center" class="menu_wrap">
          <ul class="nav sf-menu">
            <li class="sub-menu"><a href="#"><small>Mission</small> </a>
            </li>
            <li class="sub-menu"><a href="#"><small>About Us</small></a>
            </li>
            <li class="sub-menu"><a href="#"><small>Grants</small></a>
            </li>
            <li class="sub-menu"><a href="#"><small>News</small></a>
            </li>
          </ul>
        </div>
      </nav>
    </div>
  </div>