Bootstrap 3 - 扩展时的Navbar颜色

时间:2017-08-01 10:39:37

标签: css twitter-bootstrap twitter-bootstrap-3

我在bootstrap 3中创建了一个导航栏。导航栏为蓝色,打开菜单时(鼠标悬停时),我想显示背景颜色为绿色的子菜单。

这是我的小提琴

https://jsfiddle.net/8jj2g4ym/2/



li.dropdown:hover > .dropdown-menu {
    display: block;
}

.navbar-default {
  background-color: blue;
  border-color: #e7e7e7;
}

@media (min-width: 768px) {
.navbar-nav>li>a {
    padding-top: 15px;
    padding-bottom: 4px;
}}


.navbar-default .navbar-nav>li>a {
    color:white;
    FONT-SIZE: 12pt;
    TEXT-DECORATION: none;
    FONT-FAMILY: Arial;
    WHITE-SPACE: nowrap;
    DISPLAY: block;
}

.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
  color: #333;
  background-color: transparent;
}

.navbar-nav .open .dropdown-menu > li > a:hover,
  .navbar-nav .open .dropdown-menu > li > a:focus {
    background-color: green;
  }

.navbar-nav > li > a{
  padding-left:2px;
  padding-right:2px;
  margin-left:2px;
  margin-right:2px;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default"> 
  <div class="container-fluid" style="padding-left:2px;">
    <div class="navbar-header">
     
    
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu0 <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#" style="cursor:pointer">Menu1</a></li>
            <li><a href="#">Menu2</a></li>
            <li><a href="#">Menu3</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav" >
       <li ><a></a></li>
      </ul> 
      <ul class="nav navbar-nav">
        <li><a href="#">Menu4</a></li>
      
        
      </ul>
      
    </div>
    
  </div>
</nav>
&#13;
&#13;
&#13;

虽然导航栏按预期显示为蓝色,但打开的项目背景颜色为白色(我希望为绿色)。

我为此添加了一种风格,但它似乎没有影响。

.navbar-nav .open .dropdown-menu > li > a:hover,
  .navbar-nav .open .dropdown-menu > li > a:focus {
    background-color: green;
  }

5 个答案:

答案 0 :(得分:0)

以下是解决方案:

更改一些css规则以满足您的需求:

li.dropdown:hover > .dropdown-menu {
    display: block;
    background: #bacd63 !important;
}

.navbar-nav .dropdown-menu > li > a:hover,
  .navbar-nav .dropdown-menu > li > a:focus {
    background: green !important;
  }

这是小提琴:

https://jsfiddle.net/8jj2g4ym/3/

这里是代码段:

&#13;
&#13;
li.dropdown:hover > .dropdown-menu {
    display: block;
    background: #bacd63 !important;
}

.navbar-default {
  background-color: blue;
  border-color: #e7e7e7;
}

@media (min-width: 768px) {
.navbar-nav>li>a {
    padding-top: 15px;
    padding-bottom: 4px;
}}


.navbar-default .navbar-nav>li>a {
    color:white;
    FONT-SIZE: 12pt;
    TEXT-DECORATION: none;
    FONT-FAMILY: Arial;
    WHITE-SPACE: nowrap;
    DISPLAY: block;
}

.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
  color: #333;
  background-color: transparent;
}

.navbar-nav .dropdown-menu > li > a:hover,
  .navbar-nav .dropdown-menu > li > a:focus {
    background: green !important;
  }

.navbar-nav > li > a{
  padding-left:2px;
  padding-right:2px;
  margin-left:2px;
  margin-right:2px;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default"> 
  <div class="container-fluid" style="padding-left:2px;">
    <div class="navbar-header">
     
    
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu0 <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#" style="cursor:pointer">Menu1</a></li>
            <li><a href="#">Menu2</a></li>
            <li><a href="#">Menu3</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav" >
       <li ><a></a></li>
      </ul> 
      <ul class="nav navbar-nav">
        <li><a href="#">Menu4</a></li>
      
        
      </ul>
      
    </div>
    
  </div>
</nav>
&#13;
&#13;
&#13;

希望这有帮助。

答案 1 :(得分:0)

将此添加到您的CSS:

.navbar-nav > li > .dropdown-menu{background-color:green!important;}

答案 2 :(得分:0)

添加了

li.dropdown:hover > .dropdown-menu {
    display: block;
}

.navbar-default {
  background-color: blue;
  border-color: #e7e7e7;
}

@media (min-width: 768px) {
.navbar-nav>li>a {
    padding-top: 15px;
    padding-bottom: 4px;
}}


.navbar-default .navbar-nav>li>a {
    color:white;
    FONT-SIZE: 12pt;
    TEXT-DECORATION: none;
    FONT-FAMILY: Arial;
    WHITE-SPACE: nowrap;
    DISPLAY: block;
}

.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
  color: #333;
  background-color: transparent;
}

.navbar-nav .open .dropdown-menu > li > a:hover,
  .navbar-nav .open .dropdown-menu > li > a:focus {
    background-color: green;
  }

.navbar-nav > li > a{
  padding-left:2px;
  padding-right:2px;
  margin-left:2px;
  margin-right:2px;
}

.navbar-toggle:hover {
    background-color: green !important;
}

&#13;
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default"> 
  <div class="container-fluid" style="padding-left:2px;">
    <div class="navbar-header">
     
    
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu0 <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#" style="cursor:pointer">Menu1</a></li>
            <li><a href="#">Menu2</a></li>
            <li><a href="#">Menu3</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav" >
       <li ><a></a></li>
      </ul> 
      <ul class="nav navbar-nav">
        <li><a href="#">Menu4</a></li>
      
        
      </ul>
      
    </div>
    
  </div>
</nav>
&#13;
    <asp:TextBox ID="TextBoxName" name="TextBoxName" runat="server" ValidationGroup="g1"></asp:TextBox>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

而不是:

.navbar-nav .open .dropdown-menu > li > a:hover,
.navbar-nav .open .dropdown-menu > li > a:focus {
   background-color: green;
}

您应该删除.open选择器,如下所示:

.navbar-nav .dropdown-menu > li > a:hover,
.navbar-nav .dropdown-menu > li > a:focus {
   background-color: green;
}

这是因为在ul元素(navbar-nav类)下没有类open的元素,所以这就是为什么不应用绿色背景。< / p>

jsFiddle

中的工作代码

答案 4 :(得分:0)

试试这个代码dude ....

以下是演示Codepen

&#13;
&#13;
    $(function() {
        $(".navbar-toggle").hover(
            function(){ $('.collapse').addClass('in') }
            
        );
    });

$(function() {
        $(".dropdown").hover(
            function(){ $(this).addClass('open') },
            function(){ $(this).removeClass('open') },
          function(){ $('.collapse').removeClass('in') }
        );
    });
$(function() {
        $("html").click(
          function(){ $('.collapse').removeClass('in') }
        );
    });
&#13;
.navbar-default .navbar-toggle:focus, .navbar-default .navbar-toggle:hover > .collapse {
    display: block;
}

.navbar-default {
  background-color: blue;
  border-color: #e7e7e7;
}


.navbar-nav>li>a {
    padding-top: 15px;
    padding-bottom: 4px;
}


.navbar-default .navbar-nav>li>a {
    color:white;
    FONT-SIZE: 12pt;
    TEXT-DECORATION: none;
    FONT-FAMILY: Arial;
    WHITE-SPACE: nowrap;
    DISPLAY: block;
}

.navbar-collapse{
  background:green;
}

.navbar-nav > li > a{
  padding-left:2px;
  padding-right:2px;
  margin-left:2px;
  margin-right:2px;
}

.navbar-nav{
  margin:0px 0px;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>


<body>
<nav class="navbar navbar-default"> 
  <div class="container-fluid" style="padding-left:2px;">
    <div class="navbar-header">
     
    
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu0 <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#" style="cursor:pointer">Menu1</a></li>
            <li><a href="#">Menu2</a></li>
            <li><a href="#">Menu3</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav" >
       <li ><a></a></li>
      </ul> 
      <ul class="nav navbar-nav">
        <li><a href="#">Menu4</a></li>
      
        
      </ul>
      
    </div>
    
  </div>
</nav>
</body>
&#13;
&#13;
&#13;