Bootstrap3药丸,用CSS悬停li项目

时间:2018-05-04 10:20:09

标签: css twitter-bootstrap

<li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu 1
    <span class="caret"></span></a>
    <ul class="dropdown-menu">
      <li><a href="#">Submenu 1-1</a></li>
      <li><a href="#">Submenu 1-2</a></li>
      <li><a href="#">Submenu 1-3</a></li> 
    </ul>
  </li>

使用CSS悬停(子菜单)时是否可以更改li项目的背景颜色?

2 个答案:

答案 0 :(得分:1)

我无法评论SO所以我需要发布一个正常答案。如果您可以为每个菜单级别提供自己的级别,请查看:

&#13;
&#13;
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;  
}

nav, .content {
  max-width:960px; /* an arbitrary width */
  margin: 0 auto; /* center the elements */
}

nav {
  margin-top:20px; /* just a little bit of margin */
  background: #292929;  
}

.top-nav {
  font-size:0; /* eliminates whitespace from inline-block*/
}

.top-nav li {
  display:inline-block; /* blocks just line up without floats */
  width:20%; /* Just a number */
  background: #292929;  
  position:relative; /* sets positioning context for 2nd level menu */
}

.top-nav > li.active > a {
  color:green; /* just an active class - not required */
}

.top-nav > li a {
  text-decoration:none; /* removes underline */
  font-size:24px; /* re-establish a font-size */
  line-height:1.5em; /* centers the text vertically*/
  text-align:center; /* centeres the text horizontally */
  display:block; /* links now fill the block*/
  color:lightgrey; 
  
}


.top-nav > li > a {
  /* groove type border effect */
  border-left: 1px solid black; 
  border-right: 1px solid #3c3c3c;
  border-bottom: 1px solid black;
  border-top: 1px solid #3c3c3c;
}


.top-nav > li > a:hover,
.sub-nav > li > a:hover,
.tert-nav > li > a:hover{
  /* hover color for links */
  color:red;
}

.sub-nav { /* just a class name for 2nd level menus */
  position:absolute;
/* positions the menu UNDER the list item*/
  width:100%; 
/* makes the width of the menu the same as the list item */
  visibility:hidden; 
/* hides the menu until needed */
  
}

.top-nav li:hover .sub-nav {
  visibility:visible; 
/* shows the submenu when the list item is hovered */
  
}

.sub-nav > li, .sub-nav > li a{
  display:block;
  width:100%;
  position:relative; 
/* sets positioning context for 3rd level menu */
  background:lightblue; 
/* something different for example */
  color:black;
}

.sub-nav > li > a {
  /* jsut some styling */
  border: 1px solid #3c3c3c;
  border-top:none;
}

.sub-nav > li:last-child a {
  border-bottom: 1px solid #3c3c3c;
}

.tert-nav {
  position:absolute; /* positions the 3rd level menu */
  width:100%;
  visibility:hidden; /* hides the menu until required */
}

.sub-nav li:hover .tert-nav {
  visibility:visible; /* shows the menu when list item is hovered */
  display:block;
  width:100%;
  position:absolute;  /* positions the list items of 3rd level menu */
  top:0; /* lines up the menu with the list item */
  left:100%; /* pushes the list item over to the right side */
  margin-top:-1px; /* aligning after borders */
  border-left:1px solid transparent; /* just add a gap */
}

.tert-nav > li, .tert-nav > li > a {
  display:block;
  width:100%;
  background:#bada55; /* another level, another color */
}

.tert-nav > li > a {
  /* jsut some styling */
  border: 1px solid #3c3c3c;
  border-bottom:none;
}

.tert-nav > li:last-child a {
  border-bottom: 1px solid #3c3c3c;
}

.content {
  height:250px;
  background:lightgrey;
  padding:10px;
}
&#13;
<nav>
  <ul class="top-nav">
    <li class="active"><a href="#">Home</a></li>
    <li><a href="#">About</a>
      <ul class="sub-nav">
        <li><a href="#">Item 1</a></li>
        <li><a href="#">Item 2</a></li>
        <li><a href="#">Item 3</a></li>
        <li><a href="#">Item 4</a></li>
      </ul>
    </li>
    <li><a href="#">Clients</a>
      <ul class="sub-nav">
        <li><a href="#">Item 1</a>
          <ul class="tert-nav">
            <li><a href="#">Sub-Item 1.1</a></li>
            <li><a href="#">Sub-Item 1.2</a></li>
            <li><a href="#">Sub-Item 1.3</a></li>
            <li><a href="#">Sub-Item 1.4</a></li>
          </ul>      
        </li>
        <li><a href="#">Item 2</a>
          <ul class="tert-nav">
            <li><a href="#">Sub-Item 2.1</a></li>
            <li><a href="#">Sub-Item 2.2</a></li>
            <li><a href="#">Sub-Item 2.3</a></li>
            <li><a href="#">Sub-Item 2.4</a></li>
          </ul> 
        </li>
        <li><a href="#">Item 3</a></li>
        <li><a href="#">Item 4</a></li>
      </ul>
    </li>
    <li><a href="#">Contact Us</a></li>
  </ul>
</nav>
<div class="content">
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>  
</div>
&#13;
&#13;
&#13;

希望这对你有所帮助!

答案 1 :(得分:1)

您需要覆盖css样式。

<li class="dropdown my-class">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu 1
    <span class="caret"></span></a>
    <ul class="dropdown-menu">
      <li><a href="#">Submenu 1-1</a></li>
      <li><a href="#">Submenu 1-2</a></li>
      <li><a href="#">Submenu 1-3</a></li> 
    </ul>
  </li>


.my-class .dropdown-menu li  a:hover{
  background:blue;
  color:white;
}

使用jsfiddle链接:https://jsfiddle.net/5Lhmcuab/