无法居中我的导航栏并将底部边框添加到活动状态

时间:2015-09-25 06:45:44

标签: html css twitter-bootstrap nav

我一直试图将我的导航栏居中并在活动页面下添加一行,但它不起作用。任何意见,将不胜感激。我已将边框底部添加到活动元素并向{ul}显示inline-block,但它没有做到这一点,为什么会这样?

    <div id="custom-bootstrap-menu" class="navbar navbar-default " role="navigation">
<div class="container-fluid">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-menubuilder"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
        </button>
    </div>
    <div class="collapse navbar-collapse navbar-menubuilder">
        <ul class="nav navbar-nav navbar-left">
            <li><a href="#">HOME</a>
            </li>
            <li><a href="#">AGENDA</a>
            </li>
            <li><a href="#">DOCUMENT MANAGEMENT</a>
            </li>
            <li><a href="#">CASE MANAGEMENT</a>
            </li>
            <li><a href="#">SERVICE DIRECTORY</a>
            </li>
        </ul>
    </div>
</div>

CSS:

#custom-bootstrap-menu.navbar-default .navbar-brand {
    color: rgba(119, 119, 119, 1);

}

#custom-bootstrap-menu.navbar-default .navbar-nav ul{
   display: inline-block;
   list-style-type: none;
}
#custom-bootstrap-menu.navbar-default {
   font-size: small;
   font-family: 'Lato', sans-serif;
   background-color: rgba(228, 229, 230, 1);
   border-width: 0px;
   border-radius: 0px;
}
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a {
  color: rgba(119, 30, 101, 1);
  background-color: rgba(228, 229, 230, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a:focus {
  color: rgba(51, 51, 51, 1);
  background-color: rgba(228, 229, 230, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a,
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a:focus {
  color: rgba(119, 30, 101, 1);
  background-color: rgba(231, 231, 231, 1);
  border-bottom:1px solid #771e65;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle {
  border-color: #ddd;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle:hover,
#custom-bootstrap-menu.navbar-default .navbar-toggle:focus {
  background-color: #ddd;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle .icon-bar {
  background-color: #888;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle:hover .icon-bar,
#custom-bootstrap-menu.navbar-default .navbar-toggle:focus .icon-bar {
  background-color: #e4e5e6;
}

https://jsfiddle.net/judison/dk46p6pz/

3 个答案:

答案 0 :(得分:0)

/* Styles go here */

#custom-bootstrap-menu.navbar-default .navbar-brand {
  color: rgba(119, 119, 119, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav ul {
  display: inline-block;
  list-style-type: none;
}
#custom-bootstrap-menu.navbar-default {
  font-size: small;
  font-family: 'Lato', sans-serif;
  background-color: rgba(228, 229, 230, 1);
  border-width: 0px;
  border-radius: 0px;
}
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a {
  color: rgba(119, 30, 101, 1);
  background-color: rgba(228, 229, 230, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a:focus {
  color: rgba(51, 51, 51, 1);
  background-color: rgba(228, 229, 230, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a,
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a:focus {
  color: rgba(119, 30, 101, 1);
  background-color: rgba(231, 231, 231, 1);
  border-bottom: 1px solid #771e65;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle {
  border-color: #ddd;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle:hover,
#custom-bootstrap-menu.navbar-default .navbar-toggle:focus {
  background-color: #ddd;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle .icon-bar {
  background-color: #888;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle:hover .icon-bar,
#custom-bootstrap-menu.navbar-default .navbar-toggle:focus .icon-bar {
  background-color: #e4e5e6;
}
#custom-bootstrap-menu.navbar-default .navbar-nav li > a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav li > a:focus {
  border-bottom: 1px solid #771e65;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div id="custom-bootstrap-menu" class="navbar navbar-default " role="navigation">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-menubuilder"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
      </button>
    </div>
    <div class="collapse navbar-collapse navbar-menubuilder">
      <ul class="nav navbar-nav navbar-left text-center">
        <li><a href="#">HOME</a>
        </li>
        <li><a href="#">AGENDA</a>
        </li>
        <li><a href="#">DOCUMENT MANAGEMENT</a>
        </li>
        <li><a href="#">CASE MANAGEMENT</a>
        </li>
        <li><a href="#">SERVICE DIRECTORY</a>
        </li>
      </ul>
    </div>
  </div>
</div>

你可以看一下......

只需在text-center上添加<ul class="nav navbar-nav navbar-left">课程,例如<ul class="nav navbar-nav navbar-left text-center">

然后改变你的CSS

#custom-bootstrap-menu.navbar-default .navbar-nav li > a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav li > a:focus {
  border-bottom:1px solid #771e65;
}

答案 1 :(得分:0)

你真的很难...... 只需添加此

.collapse ul li{
  list-style-type: none;
  display: inline-block;
  margin: auto;
}
.collapse ul li a{
  text-decoration: none;
}
.collapse ul li a:hover{
  text-decoration: underline;
}
.collapse{
  text-align: center;
}

并删除此

#custom-bootstrap-menu.navbar-default .navbar-nav ul{
  display: inline-block;
  list-style-type: none;
}

您不必选择所有 ID ,只需选择主要版本。 显示不适合您,因为您只选择 ul 。并且 list-style-type 不起作用,因为您仍然只选择 ul ..对于list-style-type,您必须选择 li < /强>

答案 2 :(得分:0)

看起来你已经将active链接加下划线,你在这里的任何链接上都没有active类作为例子。

要使链接居中,请查看以下规则:并参阅工作示例。

/***Added CSS***/
 #custom-bootstrap-menu.navbar.navbar-default .navbar-nav {
    text-align: center;
}
@media (min-width: 768px) {
    #custom-bootstrap-menu.navbar.navbar-default .navbar-nav > li {
        float: none;
        display: inline-block;
    }
    #custom-bootstrap-menu.navbar.navbar-default .navbar-nav {
        width: 100%;
        text-align: center;
    }
}

#custom-bootstrap-menu.navbar-default .navbar-brand {
  color: rgba(119, 119, 119, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav ul {
  display: inline-block;
  list-style-type: none;
}
#custom-bootstrap-menu.navbar-default {
  font-size: small;
  font-family: 'Lato', sans-serif;
  background-color: rgba(228, 229, 230, 1);
  border-width: 0px;
  border-radius: 0px;
}
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a {
  color: rgba(119, 30, 101, 1);
  background-color: rgba(228, 229, 230, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav>li>a:focus {
  color: rgba(51, 51, 51, 1);
  background-color: rgba(228, 229, 230, 1);
}
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a,
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a:hover,
#custom-bootstrap-menu.navbar-default .navbar-nav>.active>a:focus {
  color: rgba(119, 30, 101, 1);
  background-color: rgba(231, 231, 231, 1);
  border-bottom: 1px solid #771e65;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle {
  border-color: #ddd;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle:hover,
#custom-bootstrap-menu.navbar-default .navbar-toggle:focus {
  background-color: #ddd;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle .icon-bar {
  background-color: #888;
}
#custom-bootstrap-menu.navbar-default .navbar-toggle:hover .icon-bar,
#custom-bootstrap-menu.navbar-default .navbar-toggle:focus .icon-bar {
  background-color: #e4e5e6;
}
/***Added CSS***/

#custom-bootstrap-menu.navbar.navbar-default .navbar-nav {
  text-align: center;
}
@media (min-width: 768px) {
  #custom-bootstrap-menu.navbar.navbar-default .navbar-nav > li {
    float: none;
    display: inline-block;
  }
  #custom-bootstrap-menu.navbar.navbar-default .navbar-nav {
    width: 100%;
    text-align: center;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div id="custom-bootstrap-menu" class="navbar navbar-default " role="navigation">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-menubuilder"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>

      </button>
    </div>
    <div class="collapse navbar-collapse navbar-menubuilder">
      <ul class="nav navbar-nav navbar-left">
        <li class="active"><a href="#">HOME</a>

        </li>
        <li><a href="#">AGENDA</a>

        </li>
        <li><a href="#">DOCUMENT MANAGEMENT</a>

        </li>
        <li><a href="#">CASE MANAGEMENT</a>

        </li>
        <li><a href="#">SERVICE DIRECTORY</a>

        </li>
      </ul>
    </div>
  </div>
</div>